Skip to content
Snippets Groups Projects
Verified Commit 4d1a68bb authored by Jonathan Weth's avatar Jonathan Weth :keyboard:
Browse files

Add way for dashboard widgets to add custom CSS/JS files which will be added in the html head

parent cc459120
No related branches found
No related tags found
1 merge request!155Add way for dashboard widgets to add custom CSS/JS files which will be added in the html head
Pipeline #869 failed
from datetime import date
from typing import Optional
from typing import Optional, Iterable, Union
from django.contrib.auth import get_user_model
from django.contrib.auth.models import User
from django.db import models
from django.db.models import QuerySet
from django.forms.widgets import Media
from django.utils.translation import ugettext_lazy as _
from image_cropping import ImageCropField, ImageRatioField
from phonenumber_field.modelfields import PhoneNumberField
......@@ -280,8 +282,13 @@ class DashboardWidget(PolymorphicModel):
If your widget does not add any database fields, you should mark it as a proxy model.
You can provide a Media meta class with custom JS and CSS files which will be added to html head.
For further information on media definition see https://docs.djangoproject.com/en/3.0/topics/forms/media/
Example::
from django.forms.widgets import Media
from aleksis.core.models import DashboardWidget
class MyWidget(DhasboardWIdget):
......@@ -293,9 +300,25 @@ class DashboardWidget(PolymorphicModel):
class Meta:
proxy = True
media = Media(css={
'all': ('pretty.css',)
},
js=('animations.js', 'actions.js')
)
"""
@staticmethod
def get_media(widgets: Union[QuerySet, Iterable]):
""" Return all media required to render the selected widgets. """
media = Media()
for widget in widgets:
media = media + widget.media
return media
template = None
media = Media()
title = models.CharField(max_length=150, verbose_name=_("Widget Title"))
active = models.BooleanField(blank=True, verbose_name=_("Activate Widget"))
......
......@@ -3,6 +3,10 @@
{% block browser_title %}{% blocktrans %}Home{% endblocktrans %}{% endblock %}
{% block extra_head %}
{{ media }}
{% endblock %}
{% block content %}
<p class="flow-text">{% blocktrans %}AlekSIS (School Information System){% endblocktrans %}</p>
......
......@@ -33,7 +33,11 @@ def index(request: HttpRequest) -> HttpResponse:
context["notifications"] = notifications
context["unread_notifications"] = unread_notifications
context["widgets"] = DashboardWidget.objects.filter(active=True)
widgets = DashboardWidget.objects.filter(active=True)
media = DashboardWidget.get_media(widgets)
context["widgets"] = widgets
context["media"] = media
return render(request, "core/index.html", context)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment