From 2a45e733039d90e404b0d7b8345f1ca654d1c9ec Mon Sep 17 00:00:00 2001 From: Klaus-Uwe Mitterer Date: Fri, 1 Jan 2021 18:17:10 +0100 Subject: [PATCH] Preliminary dream stat template tags --- TODO.md | 26 ++++--- dreams/templatetags/__init__.py | 0 dreams/templatetags/dream_stats.py | 60 +++++++++++++++ frontend/templates/frontend/dashboard.html | 86 ++++++++++++++++++++++ 4 files changed, 162 insertions(+), 10 deletions(-) create mode 100644 dreams/templatetags/__init__.py create mode 100644 dreams/templatetags/dream_stats.py diff --git a/TODO.md b/TODO.md index 577c397..fda0e21 100644 --- a/TODO.md +++ b/TODO.md @@ -1,30 +1,36 @@ # To-Do List +## General + +[_] Make sure users can't access other users' data +[_] Allow configuration as single user instance + ## mood module [_] Missing mood views -[ ] Statistics / graphs +[_] Statistics / graphs ## cbt module -[ ] Complete thought record creation -[ ] Thought record editing (?) -[ ] Thought record deletion +[_] Complete thought record creation +[_] Thought record editing (?) +[_] Thought record deletion ## msgio module -[ ] Implement Telegram webhooks +[_] Implement Telegram webhooks ## dreams module [x] New dream page [x] Edit dream page [x] Delete dream page -[ ] Statistics / template tags -[ ] Check for copy-paste errors in pages +[x] Statistics / template tags +[_] Check for copy-paste errors in pages ## Frontend -[ ] Better icons for sidebar items -[ ] Sidebar auto-generation (?) -[ ] Fix sidebar highlighting for pages with same name \ No newline at end of file +[_] Better icons for sidebar items +[_] Sidebar auto-generation (?) +[_] Fix sidebar highlighting for pages with same name +[_] Move dashboard components to module templates \ No newline at end of file diff --git a/dreams/templatetags/__init__.py b/dreams/templatetags/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/dreams/templatetags/dream_stats.py b/dreams/templatetags/dream_stats.py new file mode 100644 index 0000000..2719aee --- /dev/null +++ b/dreams/templatetags/dream_stats.py @@ -0,0 +1,60 @@ +from django import template +from django.utils import timezone + +from collections import Counter + +register = template.Library() + +@register.simple_tag(takes_context=True) +def total_dreams(context): + return len(context["user"].dream_set.all()) + +@register.simple_tag(takes_context=True) +def weekly_dreams(context): + now = timezone.now() + start = now - timezone.timedelta(days=7) + + return len(context["user"].dream_set.filter(timestamp__gte=start, timestamp__lte=now)) + +@register.simple_tag(takes_context=True) +def most_common_theme(context, start, end=None): + dream_list = context["user"].dream_set.filter(timestamp__gte=start.date(), timestamp__lte=(end.date() if end else start.date())) + themes = list() + + for dream in dream_list: + for theme in dream.dreamtheme_set.all(): + themes.append(theme.theme) + + try: + most_common = Counter(themes).most_common(1)[0] + return most_common[0], most_common[1] + except: + return None, None + +@register.simple_tag(takes_context=True) +def most_common_theme_weekly(context): + now = timezone.now() + start = now - timezone.timedelta(days=7) + + return most_common_theme(context, start, now) + +@register.simple_tag(takes_context=True) +def special_dreams(context, start, end=None): + dream_list = context["user"].dream_set.filter(timestamp__gte=start.date(), timestamp__lte=(end.date() if end else start.date())) + wet = 0 + lucid = 0 + + for dream in dream_list: + if dream.lucid: + lucid += 1 + if dream.wet: + wet += 1 + + return lucid, wet + +@register.simple_tag(takes_context=True) +def special_dreams_weekly(context): + now = timezone.now() + start = now - timezone.timedelta(days=7) + + return special_dreams(context, start, now) \ No newline at end of file diff --git a/frontend/templates/frontend/dashboard.html b/frontend/templates/frontend/dashboard.html index 9487cbc..e807c8b 100644 --- a/frontend/templates/frontend/dashboard.html +++ b/frontend/templates/frontend/dashboard.html @@ -1,7 +1,9 @@ {% extends "frontend/base.html" %} {% load mood_stats %} +{% load dream_stats %} {% block "content" %} +
@@ -85,4 +87,88 @@
+ +
+ + +
+
+
+
+
+
+ Dream count (total)
+
{% total_dreams %}
+
+
+ +
+
+
+
+
+ + +
+
+
+
+
+
+ Dreams (weekly)
+
{% weekly_dreams %}
+
+
+ +
+
+
+
+
+ + +
+
+
+
+
+
Special Dreams (lucid / wet) +
+
+
+ {% special_dreams_weekly as weekly %} +
{{ weekly.0 }} / {{ weekly.1 }} (weekly)
+
+
+
+
+ + +
+
+
+
+
+ + +
+
+
+
+
+
+ Most Common Theme (weekly)
+ {% most_common_theme_weekly as theme %} +
{{ theme.0 }} ({{ theme.1 }})
+
+
+ +
+
+
+
+
+
+ + {% endblock %} \ No newline at end of file