From ca2f3eda3a769c7ba15bd5047f91f89021e28aea Mon Sep 17 00:00:00 2001 From: Kumi Date: Mon, 18 Nov 2024 18:47:27 +0100 Subject: [PATCH] fix: Enhances date handling for weekly summaries Sets the end date to the current day's end time for weekly calculations, ensuring accuracy when calculating weekly averages and most common activities. Improves code readability by reformatting long conditionals and query filter conditions for better clarity. --- mood/templatetags/mood_stats.py | 35 +++++++++++++++++++++++++++------ 1 file changed, 29 insertions(+), 6 deletions(-) diff --git a/mood/templatetags/mood_stats.py b/mood/templatetags/mood_stats.py index 1f987e6..aa9480b 100644 --- a/mood/templatetags/mood_stats.py +++ b/mood/templatetags/mood_stats.py @@ -5,10 +5,12 @@ from collections import Counter register = template.Library() + @register.simple_tag(takes_context=True) def total_moods(context): return len(context["user"].status_set.all()) + @register.simple_tag(takes_context=True) def current_streak(context): status_list = context["user"].status_set.all().order_by("-timestamp") @@ -17,7 +19,11 @@ def current_streak(context): for status in status_list: if not timestamp: - if not (status.timestamp.date() == timezone.now().date() or status.timestamp.date() == (timezone.now() - timezone.timedelta(days=1)).date()): + if not ( + status.timestamp.date() == timezone.now().date() + or status.timestamp.date() + == (timezone.now() - timezone.timedelta(days=1)).date() + ): return counter timestamp = status.timestamp counter += 1 @@ -31,7 +37,8 @@ def current_streak(context): counter += 1 continue - return counter + return counter + @register.simple_tag(takes_context=True) def closest_mood(context, value): @@ -48,9 +55,13 @@ def closest_mood(context, value): return found + @register.simple_tag(takes_context=True) def average_mood(context, start, end=None, daily_averages=True): - status_list = context["user"].status_set.filter(timestamp__gte=start.date(), timestamp__lte=(end.date() if end else start.date())) + status_list = context["user"].status_set.filter( + timestamp__gte=start.date(), + timestamp__lte=(end.date() if end else start.date()), + ) moods = list() if not daily_averages else dict() for status in status_list: @@ -73,16 +84,26 @@ def average_mood(context, start, end=None, daily_averages=True): return average + @register.simple_tag(takes_context=True) def average_mood_weekly(context, daily_averages=True): now = timezone.now() start = now - timezone.timedelta(days=7) + end = now.replace(hour=23, minute=59, second=59, microsecond=999999) + + return average_mood(context, start, end, daily_averages) - return average_mood(context, start, now, daily_averages) @register.simple_tag(takes_context=True) def most_common_activity(context, start, end=None): - status_list = context["user"].status_set.filter(timestamp__gte=start.date(), timestamp__lte=(end.date() if end else start.date())) + status_list = context["user"].status_set.filter( + timestamp__gte=start, + timestamp__lte=( + end + if end + else (start.replace(hour=23, minute=59, second=59, microsecond=999999)) + ), + ) activities = list() for status in status_list: @@ -96,9 +117,11 @@ def most_common_activity(context, start, end=None): return most_common[0], most_common[1] + @register.simple_tag(takes_context=True) def most_common_activity_weekly(context): now = timezone.now() start = now - timezone.timedelta(days=7) + end = now.replace(hour=23, minute=59, second=59, microsecond=999999) - return most_common_activity(context, start, now) \ No newline at end of file + return most_common_activity(context, start, end)