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)