[ADD] Custom CSS to base file

Check if the CSS file exists and add it to the HTML output
This commit is contained in:
Peter Pfeufer 2024-08-14 12:44:23 +02:00
parent 79a1fa3d7c
commit 0fe2855faa
No known key found for this signature in database
GPG Key ID: 6051D2C6AD4EBC27
5 changed files with 70 additions and 0 deletions

View File

@ -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),
),
]

View File

@ -0,0 +1,3 @@
{% load custom_css %}
{% custom_css_static 'allianceauth/custom-styles.css' %}

View File

@ -0,0 +1,3 @@
"""
Init file for custom_css templatetags
"""

View File

@ -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'<link rel="stylesheet" href="{versioned_url}">')

View File

@ -35,6 +35,8 @@
</style> </style>
{% block extra_css %}{% endblock extra_css %} {% block extra_css %}{% endblock extra_css %}
{% include 'custom_css/bundles/custom-css.html' %}
</head> </head>
<body> <body>