From 725f566d97dcbe7395e642f391d78e76925a880d Mon Sep 17 00:00:00 2001 From: Kumi Date: Wed, 20 Nov 2024 07:41:50 +0100 Subject: [PATCH] feat: Enhances asset management for dashboard sections Introduces methods to manage styles and scripts in dashboard sections, promoting modularity and reusability. Updates templates to dynamically render styles and scripts directly from context, ensuring better integration with existing features system. Migrates related static assets to 'mood' section to streamline the module structure. These changes enable smoother customization and extendability of the dashboard's look and feel. Relates to improved frontend architecture. --- frontend/classes.py | 8 +++++ frontend/templates/frontend/footer.html | 2 +- frontend/templates/frontend/header.html | 4 +-- frontend/templatetags/dashboard.py | 32 +++++++++++++++++++ frontend/views.py | 10 +++--- mood/features.py | 9 ++++++ .../js => mood/static/mood}/dashboard.js | 0 .../static/mood}/dist/css/cal-heatmap.css | 0 .../static/mood}/dist/js/cal-heatmap.min.js | 0 .../static/mood}/dist/js/d3.v7.min.js | 0 10 files changed, 56 insertions(+), 9 deletions(-) rename {frontend/static/frontend/js => mood/static/mood}/dashboard.js (100%) rename {frontend/static/frontend => mood/static/mood}/dist/css/cal-heatmap.css (100%) rename {frontend/static/frontend => mood/static/mood}/dist/js/cal-heatmap.min.js (100%) rename {frontend/static/frontend => mood/static/mood}/dist/js/d3.v7.min.js (100%) diff --git a/frontend/classes.py b/frontend/classes.py index 015f897..35a05cf 100644 --- a/frontend/classes.py +++ b/frontend/classes.py @@ -98,6 +98,14 @@ class DashboardSection: self.name = name self.template = template self.context = context or {} + self.styles = [] + self.scripts = [] def get_html(self, request): return render_to_string(self.template, self.context, request) + + def add_style(self, style): + self.styles.append(style) + + def add_script(self, script): + self.scripts.append(script) diff --git a/frontend/templates/frontend/footer.html b/frontend/templates/frontend/footer.html index 81162d6..e86cbcb 100644 --- a/frontend/templates/frontend/footer.html +++ b/frontend/templates/frontend/footer.html @@ -19,7 +19,7 @@ {% for script in scripts %} - + {% endfor %} diff --git a/frontend/templates/frontend/header.html b/frontend/templates/frontend/header.html index 700cc2f..19d5d78 100644 --- a/frontend/templates/frontend/header.html +++ b/frontend/templates/frontend/header.html @@ -15,7 +15,7 @@ @@ -24,7 +24,7 @@ {% for style in styles %} - + {% endfor %} diff --git a/frontend/templatetags/dashboard.py b/frontend/templatetags/dashboard.py index 390c13e..409d882 100644 --- a/frontend/templatetags/dashboard.py +++ b/frontend/templatetags/dashboard.py @@ -31,3 +31,35 @@ def dashboard(context): dashboard_html += '
' return dashboard_html + + +@register.simple_tag +def dashboard_styles(): + styles = [] + + for module in settings.CORE_MODULES + settings.ENABLED_MODULES: + try: + features = import_module(f"{module}.features") + for section in features.DASHBOARD_SECTIONS: + for style in section.styles: + styles.append(style) + except Exception: + pass + + return styles + + +@register.simple_tag +def dashboard_scripts(): + scripts = [] + + for module in settings.CORE_MODULES + settings.ENABLED_MODULES: + try: + features = import_module(f"{module}.features") + for section in features.DASHBOARD_SECTIONS: + for script in section.scripts: + scripts.append(script) + except Exception: + pass + + return scripts diff --git a/frontend/views.py b/frontend/views.py index 7b26b08..bd82152 100644 --- a/frontend/views.py +++ b/frontend/views.py @@ -4,6 +4,8 @@ from django.contrib.auth.forms import UserCreationForm from django.contrib.auth import get_user_model, login from django.urls import reverse_lazy +from .templatetags.dashboard import dashboard_styles, dashboard_scripts + class DashboardView(LoginRequiredMixin, TemplateView): template_name = "frontend/dashboard.html" @@ -14,12 +16,8 @@ class DashboardView(LoginRequiredMixin, TemplateView): context["subtitle"] = ( "An overview of everything going on in your Kumify account." ) - context["scripts"] = [ - "frontend/dist/js/d3.v7.min.js", - "frontend/dist/js/cal-heatmap.min.js", - "frontend/js/dashboard.js", - ] - context["styles"] = ["frontend/dist/css/cal-heatmap.css"] + context["scripts"] = dashboard_scripts() + context["styles"] = dashboard_styles() return context diff --git a/mood/features.py b/mood/features.py index 024c014..58909eb 100644 --- a/mood/features.py +++ b/mood/features.py @@ -1,6 +1,7 @@ from frontend.classes import NavSection, NavItem, NavCollapse, DashboardSection from django.urls import reverse_lazy +from django.templatetags.static import static # Sidebar navigation items @@ -31,4 +32,12 @@ NAV_SECTIONS = [mood_section] mood_section = DashboardSection("Moods", "mood/dashboard_section.html") +mood_section.add_script(static("mood/dist/js/d3.v7.min.js")) +mood_section.add_script(static("mood/dist/js/cal-heatmap.min.js")) + +mood_section.add_script(static("mood/dashboard.js")) + +mood_section.add_style(static("mood/dist/css/cal-heatmap.css")) + + DASHBOARD_SECTIONS = [mood_section] diff --git a/frontend/static/frontend/js/dashboard.js b/mood/static/mood/dashboard.js similarity index 100% rename from frontend/static/frontend/js/dashboard.js rename to mood/static/mood/dashboard.js diff --git a/frontend/static/frontend/dist/css/cal-heatmap.css b/mood/static/mood/dist/css/cal-heatmap.css similarity index 100% rename from frontend/static/frontend/dist/css/cal-heatmap.css rename to mood/static/mood/dist/css/cal-heatmap.css diff --git a/frontend/static/frontend/dist/js/cal-heatmap.min.js b/mood/static/mood/dist/js/cal-heatmap.min.js similarity index 100% rename from frontend/static/frontend/dist/js/cal-heatmap.min.js rename to mood/static/mood/dist/js/cal-heatmap.min.js diff --git a/frontend/static/frontend/dist/js/d3.v7.min.js b/mood/static/mood/dist/js/d3.v7.min.js similarity index 100% rename from frontend/static/frontend/dist/js/d3.v7.min.js rename to mood/static/mood/dist/js/d3.v7.min.js