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.
This commit is contained in:
Kumi 2024-11-20 07:41:50 +01:00
parent bc8da6b1cc
commit 725f566d97
Signed by: kumi
GPG key ID: ECBCC9082395383F
10 changed files with 56 additions and 9 deletions

View file

@ -98,6 +98,14 @@ class DashboardSection:
self.name = name self.name = name
self.template = template self.template = template
self.context = context or {} self.context = context or {}
self.styles = []
self.scripts = []
def get_html(self, request): def get_html(self, request):
return render_to_string(self.template, self.context, 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)

View file

@ -19,7 +19,7 @@
<!-- Page level custom scripts --> <!-- Page level custom scripts -->
{% for script in scripts %} {% for script in scripts %}
<script src="{% static script %}"></script> <script src="{{ script }}"></script>
{% endfor %} {% endfor %}
</body> </body>

View file

@ -15,7 +15,7 @@
<!-- Custom fonts for this template --> <!-- Custom fonts for this template -->
<link href="https://fa.kumi.systems/css/all.min.css" rel="stylesheet" type="text/css"> <link href="https://fa.kumi.systems/css/all.min.css" rel="stylesheet" type="text/css">
<link <link
href="https://fontproxy.kumi.systems/css?family=Nunito:200,200i,300,300i,400,400i,600,600i,700,700i,800,800i,900,900i" href="https://googledonts.private.coffee/css?family=Nunito:200,200i,300,300i,400,400i,600,600i,700,700i,800,800i,900,900i"
rel="stylesheet"> rel="stylesheet">
<!-- Custom styles for this template --> <!-- Custom styles for this template -->
@ -24,7 +24,7 @@
<!-- Custom styles for this page --> <!-- Custom styles for this page -->
{% for style in styles %} {% for style in styles %}
<link href="{% static style %}" rel="stylesheet"> <link href="{{ style }}" rel="stylesheet">
{% endfor %} {% endfor %}
</head> </head>

View file

@ -31,3 +31,35 @@ def dashboard(context):
dashboard_html += '<hr class="dashboard-divider">' dashboard_html += '<hr class="dashboard-divider">'
return 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

View file

@ -4,6 +4,8 @@ from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth import get_user_model, login from django.contrib.auth import get_user_model, login
from django.urls import reverse_lazy from django.urls import reverse_lazy
from .templatetags.dashboard import dashboard_styles, dashboard_scripts
class DashboardView(LoginRequiredMixin, TemplateView): class DashboardView(LoginRequiredMixin, TemplateView):
template_name = "frontend/dashboard.html" template_name = "frontend/dashboard.html"
@ -14,12 +16,8 @@ class DashboardView(LoginRequiredMixin, TemplateView):
context["subtitle"] = ( context["subtitle"] = (
"An overview of everything going on in your Kumify account." "An overview of everything going on in your Kumify account."
) )
context["scripts"] = [ context["scripts"] = dashboard_scripts()
"frontend/dist/js/d3.v7.min.js", context["styles"] = dashboard_styles()
"frontend/dist/js/cal-heatmap.min.js",
"frontend/js/dashboard.js",
]
context["styles"] = ["frontend/dist/css/cal-heatmap.css"]
return context return context

View file

@ -1,6 +1,7 @@
from frontend.classes import NavSection, NavItem, NavCollapse, DashboardSection from frontend.classes import NavSection, NavItem, NavCollapse, DashboardSection
from django.urls import reverse_lazy from django.urls import reverse_lazy
from django.templatetags.static import static
# Sidebar navigation items # Sidebar navigation items
@ -31,4 +32,12 @@ NAV_SECTIONS = [mood_section]
mood_section = DashboardSection("Moods", "mood/dashboard_section.html") 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] DASHBOARD_SECTIONS = [mood_section]