From 5c7586bb9ab9441639b635053afe9d408b0078d8 Mon Sep 17 00:00:00 2001 From: Kumi Date: Mon, 18 Nov 2024 10:49:04 +0100 Subject: [PATCH] feat: Modularizes dashboard structure Introduces DashboardSection class to encapsulate dashboard logic, enabling easier extension and customization. Adds separate mood and dreams dashboard templates to streamline content rendering. Refactors main dashboard template to dynamically render content from enabled modules using a new template tag system, improving flexibility and maintainability. --- dreams/features.py | 12 +- .../templates/dreams/dashboard_section.html | 78 ++++++++ frontend/classes.py | 24 ++- frontend/templates/frontend/dashboard.html | 180 +----------------- frontend/templatetags/dashboard.py | 33 ++++ mood/features.py | 18 +- mood/templates/mood/dashboard_section.html | 94 +++++++++ 7 files changed, 254 insertions(+), 185 deletions(-) create mode 100644 dreams/templates/dreams/dashboard_section.html create mode 100644 frontend/templatetags/dashboard.py create mode 100644 mood/templates/mood/dashboard_section.html diff --git a/dreams/features.py b/dreams/features.py index 0cd85a0..e3145f5 100644 --- a/dreams/features.py +++ b/dreams/features.py @@ -1,7 +1,9 @@ -from frontend.classes import NavSection, NavItem +from frontend.classes import NavSection, NavItem, DashboardSection from django.urls import reverse_lazy +# Sidebar navigation items + dreams_section = NavSection("Dreams") dreams_items = { @@ -13,4 +15,10 @@ dreams_items = { for _, item in dreams_items.items(): dreams_section.add_item(item) -NAV_SECTIONS = [dreams_section] \ No newline at end of file +NAV_SECTIONS = [dreams_section] + +# Dashboard sections + +dreams_section = DashboardSection("Dreams", "dreams/dashboard_section.html") + +DASHBOARD_SECTIONS = [dreams_section] \ No newline at end of file diff --git a/dreams/templates/dreams/dashboard_section.html b/dreams/templates/dreams/dashboard_section.html new file mode 100644 index 0000000..c46eca7 --- /dev/null +++ b/dreams/templates/dreams/dashboard_section.html @@ -0,0 +1,78 @@ +{% load dream_stats %} + +
+ +
+
+
+
+
+
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 }}) +
+
+
+ +
+
+
+
+
+
diff --git a/frontend/classes.py b/frontend/classes.py index 5e45322..f5148ad 100644 --- a/frontend/classes.py +++ b/frontend/classes.py @@ -1,3 +1,6 @@ +from django.template.loader import render_to_string + + class NavSection: def __init__(self, name, order=100): self.name = name @@ -16,22 +19,37 @@ class NavSection: self.items.sort(key=lambda x: x.order) for item in self.items: - html += """ + html += ( + """ - """ + ) return html + class NavItem: def __init__(self, name, url, icon="fas fa-fw fa-smile", title=None, order=100): self.name = name self.url = url self.icon = icon self.title = title or name - self.order = order \ No newline at end of file + self.order = order + + +class DashboardSection: + def __init__(self, name, template, context=None): + self.name = name + self.template = template + self.context = context or {} + + def get_html(self, request): + return render_to_string(self.template, self.context, request) diff --git a/frontend/templates/frontend/dashboard.html b/frontend/templates/frontend/dashboard.html index 7bd1bcc..759ebca 100644 --- a/frontend/templates/frontend/dashboard.html +++ b/frontend/templates/frontend/dashboard.html @@ -1,178 +1,6 @@ {% extends "frontend/base.html" %} -{% load mood_stats %} -{% load dream_stats %} +{% load dashboard %} {% block "content" %} -

Moods

- -
-
-
-
-
Mood Calendar
-
-
-
-
-
-
-
- -
- -
-
-
-
-
-
Status count (total)
-
{% total_moods %}
-
-
- -
-
-
-
-
- -
-
-
-
-
-
Current Streak Length
-
- {% current_streak %} days -
-
-
- -
-
-
-
-
- -
-
-
-
-
-
Average mood (weekly)
-
-
- {% average_mood_weekly as mood %} {% closest_mood mood as moodobj %} -
- {{ moodobj }} ({{ mood|floatformat:2 }}) -
-
-
-
-
- -
-
-
-
-
- -
-
-
-
-
-
Most Common Activity (weekly)
- {% most_common_activity_weekly as activity %} -
- {{ activity.0 }}({{ activity.1 }}) -
-
-
- -
-
-
-
-
-
- -
-

Dreams

- -
- -
-
-
-
-
-
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 %} + {% dashboard as dashboard %} + {{ dashboard | safe }} +{% endblock "content" %} diff --git a/frontend/templatetags/dashboard.py b/frontend/templatetags/dashboard.py new file mode 100644 index 0000000..390c13e --- /dev/null +++ b/frontend/templatetags/dashboard.py @@ -0,0 +1,33 @@ +from django import template +from django.conf import settings + +from importlib import import_module + +register = template.Library() + + +@register.simple_tag(takes_context=True) +def dashboard(context): + sections = [] + + for module in settings.CORE_MODULES + settings.ENABLED_MODULES: + try: + features = import_module(f"{module}.features") + try: + sections += features.DASHBOARD_SECTIONS + except Exception: + pass + except Exception: + pass + + dashboard_html = "" + + for section in sections: + dashboard_html += f"

{section.name}

" + + dashboard_html += section.get_html(context["request"]) + + if section != sections[-1]: + dashboard_html += '
' + + return dashboard_html diff --git a/mood/features.py b/mood/features.py index 5261b76..71f193b 100644 --- a/mood/features.py +++ b/mood/features.py @@ -1,18 +1,28 @@ -from frontend.classes import NavSection, NavItem +from frontend.classes import NavSection, NavItem, DashboardSection from django.urls import reverse_lazy +# Sidebar navigation items + mood_section = NavSection("Mood") mood_items = { "mood_status_list": NavItem("Status List", reverse_lazy("mood:status_list")), "mood_activity_list": NavItem("Activities", reverse_lazy("mood:activity_list")), "mood_mood_list": NavItem("Moods", reverse_lazy("mood:mood_list")), - "mood_notification_list": NavItem("Notifications", reverse_lazy("mood:notification_list")), - "mood_statistics": NavItem("Statistics", reverse_lazy("mood:statistics")) + "mood_notification_list": NavItem( + "Notifications", reverse_lazy("mood:notification_list") + ), + "mood_statistics": NavItem("Statistics", reverse_lazy("mood:statistics")), } for _, item in mood_items.items(): mood_section.add_item(item) -NAV_SECTIONS = [mood_section] \ No newline at end of file +NAV_SECTIONS = [mood_section] + +# Dashboard sections + +mood_section = DashboardSection("Moods", "mood/dashboard_section.html") + +DASHBOARD_SECTIONS = [mood_section] diff --git a/mood/templates/mood/dashboard_section.html b/mood/templates/mood/dashboard_section.html new file mode 100644 index 0000000..d559f9a --- /dev/null +++ b/mood/templates/mood/dashboard_section.html @@ -0,0 +1,94 @@ +{% load mood_stats %} + + +
+
+
+
+
Mood Calendar
+
+
+
+
+
+
+
+ +
+ +
+
+
+
+
+
Status count (total)
+
{% total_moods %}
+
+
+ +
+
+
+
+
+ +
+
+
+
+
+
Current Streak Length
+
+ {% current_streak %} days +
+
+
+ +
+
+
+
+
+ +
+
+
+
+
+
Average mood (weekly)
+
+
+ {% average_mood_weekly as mood %} {% closest_mood mood as moodobj %} +
+ {{ moodobj }} ({{ mood|floatformat:2 }}) +
+
+
+
+
+ +
+
+
+
+
+ +
+
+
+
+
+
Most Common Activity (weekly)
+ {% most_common_activity_weekly as activity %} +
+ {{ activity.0 }}({{ activity.1 }}) +
+
+
+ +
+
+
+
+
+