浏览代码

[food] Fix error in calorie aggregation

Colin Powell 2 周之前
父节点
当前提交
1d813e4643
共有 1 个文件被更改,包括 4 次插入3 次删除
  1. 4 3
      vrobbler/apps/scrobbles/utils.py

+ 4 - 3
vrobbler/apps/scrobbles/utils.py

@@ -396,11 +396,12 @@ def get_daily_calories_for_user_by_day(user_id: int, date: date| str) -> int:
 
     try:
         qs = base_scrobble_qs(user_id).filter(day=date)
-    except AttibuteError as e:
+        agg = qs.aggregate(total_calories=models.Sum("calories_int"))
+    except AttributeError as e:
         logger.warning(f"Can't generate calorie total: {e}")
-    agg = qs.aggregate(total_calories=models.Sum("calories_int"))
+        agg = {}
 
-    return agg["total_calories"] or 0
+    return agg.get("total_calories") or 0
 
 def get_daily_calorie_dict_for_user(user_id: int) -> dict[date, int]:
     """Return {day: total_calories} for all days with scrobbles, in one query."""