From 68acfc8875ce5f8386ca5dd97760ee2bd96e7920 Mon Sep 17 00:00:00 2001 From: Kumi Date: Thu, 28 Nov 2024 16:59:31 +0100 Subject: [PATCH] fix: Fix mood count calculation and query formatting Improves calculation of average mood by counting valid mood entries (moodcount) separately, ensuring division occurs only on valid mood data. Additionally, refines queryset management for better readability and consistency, ensuring accurate date range filters. This change enhances data accuracy and code maintainability. --- mood/views.py | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/mood/views.py b/mood/views.py index 049fd35..7605045 100644 --- a/mood/views.py +++ b/mood/views.py @@ -45,20 +45,18 @@ class StatusListView(LoginRequiredMixin, ListView): return context def get_queryset(self): - status_list = Status.objects.filter(user=self.request.user).order_by("timestamp") + status_list = Status.objects.filter(user=self.request.user).order_by( + "timestamp" + ) if "from" in self.request.GET: from_timestamp = datetime.strptime(self.request.GET["from"], "%Y-%m-%d") - status_list = status_list.filter( - timestamp__gte=from_timestamp - ) + status_list = status_list.filter(timestamp__gte=from_timestamp) if "to" in self.request.GET: to_timestamp = datetime.strptime(self.request.GET["to"], "%Y-%m-%d") to_timestamp = to_timestamp.replace(hour=23, minute=59, second=59) - status_list = status_list.filter( - timestamp__lte=self.request.GET["to"] - ) + status_list = status_list.filter(timestamp__lte=self.request.GET["to"]) return status_list @@ -586,12 +584,13 @@ class MoodCountHeatmapJSONView(LoginRequiredMixin, View): for entry in data: date = entry.timestamp.strftime("%Y-%m-%d") - if "date" not in output: - output[date] = {"count": 0, "total": 0} + if date not in output: + output[date] = {"count": 0, "total": 0, "moodcount": 0} if entry.mood: output[date]["total"] += entry.mood.value - + output[date]["moodcount"] += 1 + output[date]["count"] += 1 output = [ @@ -599,7 +598,9 @@ class MoodCountHeatmapJSONView(LoginRequiredMixin, View): "date": key, "count": value["count"], "average": ( - (value["total"] / value["count"]) if value["count"] > 0 else 0 + (value["total"] / value["moodcount"]) + if value["moodcount"] > 0 + else 0 ), } for key, value in output.items()