mirror of
https://gitlab.com/allianceauth/allianceauth.git
synced 2025-07-09 12:30:15 +02:00
[ADD] Custom CSS to base file
Check if the CSS file exists and add it to the HTML output
This commit is contained in:
parent
79a1fa3d7c
commit
0fe2855faa
@ -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),
|
||||
),
|
||||
]
|
@ -0,0 +1,3 @@
|
||||
{% load custom_css %}
|
||||
|
||||
{% custom_css_static 'allianceauth/custom-styles.css' %}
|
3
allianceauth/custom_css/templatetags/__init__.py
Normal file
3
allianceauth/custom_css/templatetags/__init__.py
Normal file
@ -0,0 +1,3 @@
|
||||
"""
|
||||
Init file for custom_css templatetags
|
||||
"""
|
44
allianceauth/custom_css/templatetags/custom_css.py
Normal file
44
allianceauth/custom_css/templatetags/custom_css.py
Normal 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}">')
|
@ -35,6 +35,8 @@
|
||||
</style>
|
||||
|
||||
{% block extra_css %}{% endblock extra_css %}
|
||||
|
||||
{% include 'custom_css/bundles/custom-css.html' %}
|
||||
</head>
|
||||
|
||||
<body>
|
||||
|
Loading…
x
Reference in New Issue
Block a user