Calculate average moods from average daily moods

This commit is contained in:
Kumi 2021-01-17 16:49:22 +01:00
parent faa5a76d2d
commit 1d3251d886

View file

@ -46,14 +46,23 @@ 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): 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() moods = list() if not daily_averages else dict()
for status in status_list: for status in status_list:
if status.mood: if status.mood:
if daily_averages:
if not status.timestamp.date() in moods.keys():
moods[status.timestamp.date()] = [status.mood.value]
else:
moods[status.timestamp.date()].append(status.mood.value)
else:
moods.append(status.mood.value) moods.append(status.mood.value)
if daily_averages:
moods = [sum(entries) / len(entries) for date, entries in moods.items()]
try: try:
average = sum(moods) / len(moods) average = sum(moods) / len(moods)
except ZeroDivisionError: except ZeroDivisionError:
@ -62,11 +71,11 @@ def average_mood(context, start, end=None):
return average return average
@register.simple_tag(takes_context=True) @register.simple_tag(takes_context=True)
def average_mood_weekly(context): 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)
return average_mood(context, start, now) 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):