diff --git a/allianceauth/custom_css/migrations/0005_customcss_timestamp.py b/allianceauth/custom_css/migrations/0005_customcss_timestamp.py new file mode 100644 index 00000000..8fb15877 --- /dev/null +++ b/allianceauth/custom_css/migrations/0005_customcss_timestamp.py @@ -0,0 +1,18 @@ +# Generated by Django 4.2.15 on 2024-08-14 09:35 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ("custom_css", "0004_alter_customcss_css"), + ] + + operations = [ + migrations.AddField( + model_name="customcss", + name="timestamp", + field=models.DateTimeField(auto_now=True), + ), + ] diff --git a/allianceauth/custom_css/templates/custom_css/bundles/custom-css.html b/allianceauth/custom_css/templates/custom_css/bundles/custom-css.html new file mode 100644 index 00000000..4dd634ed --- /dev/null +++ b/allianceauth/custom_css/templates/custom_css/bundles/custom-css.html @@ -0,0 +1,3 @@ +{% load custom_css %} + +{% custom_css_static 'allianceauth/custom-styles.css' %} diff --git a/allianceauth/custom_css/templatetags/__init__.py b/allianceauth/custom_css/templatetags/__init__.py new file mode 100644 index 00000000..e0365e18 --- /dev/null +++ b/allianceauth/custom_css/templatetags/__init__.py @@ -0,0 +1,3 @@ +""" +Init file for custom_css templatetags +""" diff --git a/allianceauth/custom_css/templatetags/custom_css.py b/allianceauth/custom_css/templatetags/custom_css.py new file mode 100644 index 00000000..2f355d45 --- /dev/null +++ b/allianceauth/custom_css/templatetags/custom_css.py @@ -0,0 +1,44 @@ +""" +Custom template tags for custom_css app +""" + +# Alliance Auth Custom CSS +from allianceauth.custom_css.models import CustomCSS + +# Django +from django.conf import settings +from django.template.defaulttags import register +from django.templatetags.static import static +from django.utils.safestring import mark_safe + +from pathlib import Path + + +@register.simple_tag +def custom_css_static(path: str) -> str: + """ + Versioned static URL + This is to make sure to break the browser cache on CSS updates. + + Example: /static/allianceauth/custom-styles.css?v=1234567890 + + :param path: + :type path: + :return: + :rtype: + """ + + custom_css_changed = CustomCSS.objects.first().timestamp.timestamp() + custom_css_version = ( + str(custom_css_changed).replace(" ", "").replace(":", "").replace("-", "") + ) # remove spaces, colons, and dashes + static_url = static(path) + + try: + Path(f"{settings.STATIC_ROOT}allianceauth/custom-styles.css").resolve(strict=True) + except FileNotFoundError: + return "" + else: + versioned_url = static_url + "?v=" + custom_css_version + + return mark_safe(f'') diff --git a/allianceauth/templates/allianceauth/base-bs5.html b/allianceauth/templates/allianceauth/base-bs5.html index 58235b9e..4e9a31eb 100644 --- a/allianceauth/templates/allianceauth/base-bs5.html +++ b/allianceauth/templates/allianceauth/base-bs5.html @@ -35,6 +35,8 @@ {% block extra_css %}{% endblock extra_css %} + + {% include 'custom_css/bundles/custom-css.html' %}