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.
This commit is contained in:
Kumi 2024-11-18 18:47:27 +01:00
parent 5c7586bb9a
commit ca2f3eda3a
Signed by: kumi
GPG key ID: ECBCC9082395383F

View file

@ -5,10 +5,12 @@ from collections import Counter
register = template.Library() register = template.Library()
@register.simple_tag(takes_context=True) @register.simple_tag(takes_context=True)
def total_moods(context): def total_moods(context):
return len(context["user"].status_set.all()) return len(context["user"].status_set.all())
@register.simple_tag(takes_context=True) @register.simple_tag(takes_context=True)
def current_streak(context): def current_streak(context):
status_list = context["user"].status_set.all().order_by("-timestamp") status_list = context["user"].status_set.all().order_by("-timestamp")
@ -17,7 +19,11 @@ def current_streak(context):
for status in status_list: for status in status_list:
if not timestamp: 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 return counter
timestamp = status.timestamp timestamp = status.timestamp
counter += 1 counter += 1
@ -31,7 +37,8 @@ def current_streak(context):
counter += 1 counter += 1
continue continue
return counter return counter
@register.simple_tag(takes_context=True) @register.simple_tag(takes_context=True)
def closest_mood(context, value): def closest_mood(context, value):
@ -48,9 +55,13 @@ def closest_mood(context, value):
return found return found
@register.simple_tag(takes_context=True) @register.simple_tag(takes_context=True)
def average_mood(context, start, end=None, daily_averages=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() moods = list() if not daily_averages else dict()
for status in status_list: for status in status_list:
@ -73,16 +84,26 @@ def average_mood(context, start, end=None, daily_averages=True):
return average return average
@register.simple_tag(takes_context=True) @register.simple_tag(takes_context=True)
def average_mood_weekly(context, daily_averages=True): def average_mood_weekly(context, daily_averages=True):
now = timezone.now() now = timezone.now()
start = now - timezone.timedelta(days=7) 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) @register.simple_tag(takes_context=True)
def most_common_activity(context, start, end=None): 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() activities = list()
for status in status_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] return most_common[0], most_common[1]
@register.simple_tag(takes_context=True) @register.simple_tag(takes_context=True)
def most_common_activity_weekly(context): def most_common_activity_weekly(context):
now = timezone.now() now = timezone.now()
start = now - timezone.timedelta(days=7) start = now - timezone.timedelta(days=7)
end = now.replace(hour=23, minute=59, second=59, microsecond=999999)
return most_common_activity(context, start, now) return most_common_activity(context, start, end)