Include removed templatetag, add new one (cache buster) + tests

pull/154/head
Mauro Santos 2022-02-22 14:32:48 +01:00
parent 9f608a741a
commit b4ef20f5f5
6 changed files with 55 additions and 12 deletions

View File

@ -8,6 +8,8 @@
{% block extrastyle %} {% block extrastyle %}
{% get_admin_interface_theme as theme %} {% get_admin_interface_theme as theme %}
{% get_admin_interface_version as version %}
{% get_admin_interface_nocache as version_md5_cache %}
{% get_current_language as current_lang %} {% get_current_language as current_lang %}
<style type="text/css"> <style type="text/css">
{% include "admin_interface/css/admin-interface.css" %} {% include "admin_interface/css/admin-interface.css" %}
@ -35,10 +37,12 @@
{% block blockbots %} {% block blockbots %}
{{ block.super }} {{ block.super }}
{% get_admin_interface_theme as theme %} {% get_admin_interface_theme as theme %}
{% get_admin_interface_version as version %}
{% get_admin_interface_nocache as version_md5_cache %}
{# https://github.com/elky/django-flat-responsive#important-note #} {# https://github.com/elky/django-flat-responsive#important-note #}
<meta name="viewport" content="user-scalable=no, width=device-width, initial-scale=1.0, maximum-scale=1.0"> <meta name="viewport" content="user-scalable=no, width=device-width, initial-scale=1.0, maximum-scale=1.0">
<link rel="stylesheet" type="text/css" href="{% static 'admin/css/responsive.css' %}"> <link rel="stylesheet" type="text/css" href="{% static 'admin/css/responsive.css' %}?nocache={{ version_md5_cache }}">
<link rel="stylesheet" type="text/css" href="{% static 'admin/css/responsive_rtl.css' %}"> <link rel="stylesheet" type="text/css" href="{% static 'admin/css/responsive_rtl.css' %}?nocache={{ version_md5_cache }}">
{% include "admin_interface/favicon.html" %} {% include "admin_interface/favicon.html" %}
{% include "admin_interface/foldable-apps.html" %} {% include "admin_interface/foldable-apps.html" %}
{% include "admin_interface/related-modal.html" %} {% include "admin_interface/related-modal.html" %}

View File

@ -3,7 +3,7 @@
{% if theme.favicon %} {% if theme.favicon %}
<link rel="icon" href="{{ theme.favicon.url }}"> <link rel="icon" href="{{ theme.favicon.url }}">
{% if theme.env_visible_in_favicon %} {% if theme.env_visible_in_favicon %}
<script type="text/javascript" src="{% static 'admin_interface/favico/favico-0.3.10-patched.min.js' %}"></script> <script type="text/javascript" src="{% static 'admin_interface/favico/favico-0.3.10-patched.min.js' %}?nocache={{ version_md5_cache }}"></script>
<script type="text/javascript"> <script type="text/javascript">
var favicon = new Favico({ var favicon = new Favico({
type: 'circle', type: 'circle',

View File

@ -1,6 +1,6 @@
{% load static %} {% load static %}
{% if theme.foldable_apps %} {% if theme.foldable_apps %}
<link rel="stylesheet" type="text/css" href="{% static 'admin_interface/foldable-apps/foldable-apps.css' %}"> <link rel="stylesheet" type="text/css" href="{% static 'admin_interface/foldable-apps/foldable-apps.css' %}?nocache={{ version_md5_cache }}">
<script type="text/javascript" src="{% static 'admin_interface/foldable-apps/foldable-apps.js' %}"></script> <script type="text/javascript" src="{% static 'admin_interface/foldable-apps/foldable-apps.js' %}?nocache={{ version_md5_cache }}"></script>
{% endif %} {% endif %}

View File

@ -1,7 +1,7 @@
{% load static %} {% load static %}
{% if theme.related_modal_active %} {% if theme.related_modal_active %}
<link rel="stylesheet" type="text/css" href="{% static 'admin_interface/magnific-popup/magnific-popup.css' %}"> <link rel="stylesheet" type="text/css" href="{% static 'admin_interface/magnific-popup/magnific-popup.css' %}?nocache={{ version_md5_cache }}">
<script type="text/javascript" src="{% static 'admin_interface/magnific-popup/jquery.magnific-popup.js' %}"></script> <script type="text/javascript" src="{% static 'admin_interface/magnific-popup/jquery.magnific-popup.js' %}?nocache={{ version_md5_cache }}"></script>
<script type="text/javascript" src="{% static 'admin_interface/related-modal/related-modal.js' %}"></script> <script type="text/javascript" src="{% static 'admin_interface/related-modal/related-modal.js' %}?nocache={{ version_md5_cache }}"></script>
{% endif %} {% endif %}

View File

@ -1,5 +1,6 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import re
import hashlib
import django import django
from django import template from django import template
@ -9,8 +10,8 @@ from django.utils import translation
from admin_interface.cache import get_cached_active_theme, set_cached_active_theme from admin_interface.cache import get_cached_active_theme, set_cached_active_theme
from admin_interface.compat import NoReverseMatch, reverse from admin_interface.compat import NoReverseMatch, reverse
from admin_interface.models import Theme from admin_interface.models import Theme
from admin_interface.version import __version__
import re
register = template.Library() register = template.Library()
@ -73,3 +74,17 @@ def get_admin_interface_theme(context):
return theme return theme
@simple_tag(takes_context=False)
def get_admin_interface_version():
return __version__
def hash_string(text):
hash_object = hashlib.md5(text.encode())
md5_hash = hash_object.hexdigest()
return md5_hash
@simple_tag(takes_context=False)
def get_admin_interface_nocache():
return hash_string(__version__)

View File

@ -8,6 +8,8 @@ from django.template import Context, Template
from admin_interface.models import Theme from admin_interface.models import Theme
from admin_interface.templatetags import admin_interface_tags as templatetags from admin_interface.templatetags import admin_interface_tags as templatetags
from admin_interface.templatetags.admin_interface_tags import hash_string
from admin_interface.version import __version__
class AdminInterfaceTemplateTagsTestCase(TestCase): class AdminInterfaceTemplateTagsTestCase(TestCase):
@ -122,3 +124,25 @@ class AdminInterfaceTemplateTagsTestCase(TestCase):
context, context,
) )
self.assertEqual(rendered, "Django") self.assertEqual(rendered, "Django")
def test_get_version(self):
version = templatetags.get_admin_interface_version()
self.assertEqual(version, __version__)
rendered = self.__render_template(
"{% load admin_interface_tags %}"
"{% get_admin_interface_version as version %}"
"{{ version }}"
)
self.assertEqual(rendered, __version__)
def test_get_version_nocache(self):
hash_from_tag = templatetags.get_admin_interface_nocache()
hash_manual = hash_string(__version__)
self.assertEqual(hash_from_tag, hash_manual)
rendered = self.__render_template(
"{% load admin_interface_tags %}"
"{% get_admin_interface_nocache as version_md5_hash %}"
"{{ version_md5_hash }}"
)
self.assertEqual(rendered, hash_manual)