Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

384: Make nocache optional #385

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ INSTALLED_APPS = (

X_FRAME_OPTIONS = "SAMEORIGIN"
SILENCED_SYSTEM_CHECKS = ["security.W019"]
ADMIN_TEMPLATE_USE_VERSION_NOCACHE = True

> [!WARNING]
> If you are using S3 with querysting signature set `ADMIN_TEMPLATE_USE_VERSION_NOCACHE = False`
```
- Run `python manage.py migrate`
- Run `python manage.py collectstatic --clear`
Expand Down
63 changes: 32 additions & 31 deletions admin_interface/templates/admin/base_site.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
{% block extrastyle %}
{% get_admin_interface_theme as theme %}
{% get_admin_interface_nocache as version_md5_cache %}
{% get_use_version_nocache as nocache %}
{% get_current_language as current_lang %}
<style>
:root .admin-interface {
Expand Down Expand Up @@ -79,39 +80,39 @@
{% endif %}
}
</style>
<link rel="stylesheet" href="{% static 'admin_interface/css/admin-interface.css' %}?v={{ version_md5_cache }}">
<link rel="stylesheet" href="{% static 'admin_interface/css/changelist.css' %}?v={{ version_md5_cache }}">
<link rel="stylesheet" href="{% static 'admin_interface/css/change-form.css' %}?v={{ version_md5_cache }}">
<link rel="stylesheet" href="{% static 'admin_interface/css/fieldsets.css' %}?v={{ version_md5_cache }}">
<link rel="stylesheet" href="{% static 'admin_interface/css/file-upload.css' %}?v={{ version_md5_cache }}">
<link rel="stylesheet" href="{% static 'admin_interface/css/header.css' %}?v={{ version_md5_cache }}">
<link rel="stylesheet" href="{% static 'admin_interface/css/inlines.css' %}?v={{ version_md5_cache }}">
<link rel="stylesheet" href="{% static 'admin_interface/css/jquery.ui.tabs.css' %}?v={{ version_md5_cache }}">
<link rel="stylesheet" href="{% static 'admin_interface/css/language-chooser.css' %}?v={{ version_md5_cache }}">
<link rel="stylesheet" href="{% static 'admin_interface/css/list-filter.css' %}?v={{ version_md5_cache }}">
<link rel="stylesheet" href="{% static 'admin_interface/css/list-filter-dropdown.css' %}?v={{ version_md5_cache }}">
<link rel="stylesheet" href="{% static 'admin_interface/css/login.css' %}?v={{ version_md5_cache }}">
<link rel="stylesheet" href="{% static 'admin_interface/css/modules.css' %}?v={{ version_md5_cache }}">
<link rel="stylesheet" href="{% static 'admin_interface/css/nav-sidebar.css' %}?v={{ version_md5_cache }}">
<link rel="stylesheet" href="{% static 'admin_interface/css/paginator.css' %}?v={{ version_md5_cache }}">
<link rel="stylesheet" href="{% static 'admin_interface/css/object-tools.css' %}?v={{ version_md5_cache }}">
<link rel="stylesheet" href="{% static 'admin_interface/css/admin-interface.css' %}{% if nocache %}?v={{ version_md5_cache }}{% endif %}">
<link rel="stylesheet" href="{% static 'admin_interface/css/changelist.css' %}{% if nocache %}?v={{ version_md5_cache }}{% endif %}">
<link rel="stylesheet" href="{% static 'admin_interface/css/change-form.css' %}{% if nocache %}?v={{ version_md5_cache }}{% endif %}">
<link rel="stylesheet" href="{% static 'admin_interface/css/fieldsets.css' %}{% if nocache %}?v={{ version_md5_cache }}{% endif %}">
<link rel="stylesheet" href="{% static 'admin_interface/css/file-upload.css' %}{% if nocache %}?v={{ version_md5_cache }}{% endif %}">
<link rel="stylesheet" href="{% static 'admin_interface/css/header.css' %}{% if nocache %}?v={{ version_md5_cache }}{% endif %}">
<link rel="stylesheet" href="{% static 'admin_interface/css/inlines.css' %}{% if nocache %}?v={{ version_md5_cache }}{% endif %}">
<link rel="stylesheet" href="{% static 'admin_interface/css/jquery.ui.tabs.css' %}{% if nocache %}?v={{ version_md5_cache }}{% endif %}">
<link rel="stylesheet" href="{% static 'admin_interface/css/language-chooser.css' %}{% if nocache %}?v={{ version_md5_cache }}{% endif %}">
<link rel="stylesheet" href="{% static 'admin_interface/css/list-filter.css' %}{% if nocache %}?v={{ version_md5_cache }}{% endif %}">
<link rel="stylesheet" href="{% static 'admin_interface/css/list-filter-dropdown.css' %}{% if nocache %}?v={{ version_md5_cache }}{% endif %}">
<link rel="stylesheet" href="{% static 'admin_interface/css/login.css' %}{% if nocache %}?v={{ version_md5_cache }}{% endif %}">
<link rel="stylesheet" href="{% static 'admin_interface/css/modules.css' %}{% if nocache %}?v={{ version_md5_cache }}{% endif %}">
<link rel="stylesheet" href="{% static 'admin_interface/css/nav-sidebar.css' %}{% if nocache %}?v={{ version_md5_cache }}{% endif %}">
<link rel="stylesheet" href="{% static 'admin_interface/css/paginator.css' %}{% if nocache %}?v={{ version_md5_cache }}{% endif %}">
<link rel="stylesheet" href="{% static 'admin_interface/css/object-tools.css' %}{% if nocache %}?v={{ version_md5_cache }}{% endif %}">
{% if not theme.recent_actions_visible %}
<link rel="stylesheet" href="{% static 'admin_interface/css/recent-actions.css' %}?v={{ version_md5_cache }}">
<link rel="stylesheet" href="{% static 'admin_interface/css/recent-actions.css' %}{% if nocache %}?v={{ version_md5_cache }}{% endif %}">
{% endif %}
<link rel="stylesheet" href="{% static 'admin_interface/css/rtl.css' %}?v={{ version_md5_cache }}">
<link rel="stylesheet" href="{% static 'admin_interface/css/sticky-form-controls.css' %}?v={{ version_md5_cache }}">
<link rel="stylesheet" href="{% static 'admin_interface/css/tabbed-changeform.css' %}?v={{ version_md5_cache }}">
<link rel="stylesheet" href="{% static 'admin_interface/css/widgets.css' %}?v={{ version_md5_cache }}">
<link rel="stylesheet" href="{% static 'admin_interface/css/rtl.css' %}{% if nocache %}?v={{ version_md5_cache }}{% endif %}">
<link rel="stylesheet" href="{% static 'admin_interface/css/sticky-form-controls.css' %}{% if nocache %}?v={{ version_md5_cache }}{% endif %}">
<link rel="stylesheet" href="{% static 'admin_interface/css/tabbed-changeform.css' %}{% if nocache %}?v={{ version_md5_cache }}{% endif %}">
<link rel="stylesheet" href="{% static 'admin_interface/css/widgets.css' %}{% if nocache %}?v={{ version_md5_cache }}{% endif %}">

<!-- third-party packages compatibility / style optimizations -->
<link rel="stylesheet" href="{% static 'admin_interface/css/third-party/ckeditor.css' %}?v={{ version_md5_cache }}">
<link rel="stylesheet" href="{% static 'admin_interface/css/third-party/import-export.css' %}?v={{ version_md5_cache }}">
<link rel="stylesheet" href="{% static 'admin_interface/css/third-party/json-widget.css' %}?v={{ version_md5_cache }}">
<link rel="stylesheet" href="{% static 'admin_interface/css/third-party/modeltranslation.css' %}?v={{ version_md5_cache }}">
<link rel="stylesheet" href="{% static 'admin_interface/css/third-party/rangefilter.css' %}?v={{ version_md5_cache }}">
<link rel="stylesheet" href="{% static 'admin_interface/css/third-party/sorl-thumbnail.css' %}?v={{ version_md5_cache }}">
<link rel="stylesheet" href="{% static 'admin_interface/css/third-party/streamfield.css' %}?v={{ version_md5_cache }}">
<link rel="stylesheet" href="{% static 'admin_interface/css/third-party/tinymce.css' %}?v={{ version_md5_cache }}">
<link rel="stylesheet" href="{% static 'admin_interface/css/third-party/ckeditor.css' %}{% if nocache %}?v={{ version_md5_cache }}{% endif %}">
<link rel="stylesheet" href="{% static 'admin_interface/css/third-party/import-export.css' %}{% if nocache %}?v={{ version_md5_cache }}{% endif %}">
<link rel="stylesheet" href="{% static 'admin_interface/css/third-party/json-widget.css' %}{% if nocache %}?v={{ version_md5_cache }}{% endif %}">
<link rel="stylesheet" href="{% static 'admin_interface/css/third-party/modeltranslation.css' %}{% if nocache %}?v={{ version_md5_cache }}{% endif %}">
<link rel="stylesheet" href="{% static 'admin_interface/css/third-party/rangefilter.css' %}{% if nocache %}?v={{ version_md5_cache }}{% endif %}">
<link rel="stylesheet" href="{% static 'admin_interface/css/third-party/sorl-thumbnail.css' %}{% if nocache %}?v={{ version_md5_cache }}{% endif %}">
<link rel="stylesheet" href="{% static 'admin_interface/css/third-party/streamfield.css' %}{% if nocache %}?v={{ version_md5_cache }}{% endif %}">
<link rel="stylesheet" href="{% static 'admin_interface/css/third-party/tinymce.css' %}{% if nocache %}?v={{ version_md5_cache }}{% endif %}">
<!-- end third-party packages compatibility / style optimizations -->

{% if current_lang == 'fa' %}
Expand All @@ -126,8 +127,8 @@
{% get_admin_interface_nocache as version_md5_cache %}
{# 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">
<link rel="stylesheet" href="{% static 'admin/css/responsive.css' %}?nocache={{ version_md5_cache }}">
<link rel="stylesheet" href="{% static 'admin/css/responsive_rtl.css' %}?nocache={{ version_md5_cache }}">
<link rel="stylesheet" href="{% static 'admin/css/responsive.css' %}{% if nocache %}?nocache={{ version_md5_cache }}{% endif %}">
<link rel="stylesheet" href="{% static 'admin/css/responsive_rtl.css' %}{% if nocache %}?nocache={{ version_md5_cache }}{% endif %}">
{% include "admin_interface/favicon.html" %}
{% include "admin_interface/foldable-apps.html" %}
{% include "admin_interface/related-modal.html" %}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{% load static %}
{% load static admin_interface_tags %}
{% get_use_version_nocache as nocache %}

{% if theme.collapsible_stacked_inlines or theme.collapsible_tabular_inlines %}
<script src="{% static 'admin_interface/collapsible-inlines/collapsible-inlines.js' %}?nocache={{ version_md5_cache }}"></script>
<script src="{% static 'admin_interface/collapsible-inlines/collapsible-inlines.js' %}{% if nocache %}?nocache={{ version_md5_cache }}{% endif %}"></script>
{% endif %}
5 changes: 3 additions & 2 deletions admin_interface/templates/admin_interface/favicon.html
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
{% load static %}
{% load static admin_interface_tags %}
{% get_use_version_nocache as nocache %}

{% if theme.favicon %}
<link rel="icon" href="{{ theme.favicon.url }}">
{% if theme.env_visible_in_favicon %}
<script src="{% static 'admin_interface/favico/favico-0.3.10-patched.min.js' %}?nocache={{ version_md5_cache }}"></script>
<script src="{% static 'admin_interface/favico/favico-0.3.10-patched.min.js' %}{% if nocache %}?nocache={{ version_md5_cache }}{% endif %}"></script>
<script>
var favicon = new Favico({
type: 'circle',
Expand Down
7 changes: 4 additions & 3 deletions admin_interface/templates/admin_interface/foldable-apps.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{% load static %}
{% load static admin_interface_tags %}
{% get_use_version_nocache as nocache %}

{% if theme.foldable_apps %}
<link rel="stylesheet" href="{% static 'admin_interface/foldable-apps/foldable-apps.css' %}?nocache={{ version_md5_cache }}">
<script src="{% static 'admin_interface/foldable-apps/foldable-apps.js' %}?nocache={{ version_md5_cache }}"></script>
<link rel="stylesheet" href="{% static 'admin_interface/foldable-apps/foldable-apps.css' %}{% if nocache %}?nocache={{ version_md5_cache }}{% endif %}">
<script src="{% static 'admin_interface/foldable-apps/foldable-apps.js' %}{% if nocache %}?nocache={{ version_md5_cache }}{% endif %}"></script>
{% endif %}
11 changes: 6 additions & 5 deletions admin_interface/templates/admin_interface/related-modal.html
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
{% load static %}
{% load static admin_interface_tags %}
{% get_use_version_nocache as nocache %}

{% if theme.related_modal_active %}
<link rel="stylesheet" href="{% static 'admin_interface/magnific-popup/magnific-popup.css' %}?nocache={{ version_md5_cache }}">
<link rel="stylesheet" href="{% static 'admin_interface/related-modal/related-modal.css' %}?v={{ version_md5_cache }}">
<script src="{% static 'admin_interface/magnific-popup/jquery.magnific-popup.js' %}?nocache={{ version_md5_cache }}"></script>
<script src="{% static 'admin_interface/related-modal/related-modal.js' %}?nocache={{ version_md5_cache }}"></script>
<link rel="stylesheet" href="{% static 'admin_interface/magnific-popup/magnific-popup.css' %}{% if nocache %}?nocache={{ version_md5_cache }}{% endif %}">
<link rel="stylesheet" href="{% static 'admin_interface/related-modal/related-modal.css' %}{% if nocache %}?v={{ version_md5_cache }}{% endif %}">
<script src="{% static 'admin_interface/magnific-popup/jquery.magnific-popup.js' %}{% if nocache %}?nocache={{ version_md5_cache }}{% endif %}"></script>
<script src="{% static 'admin_interface/related-modal/related-modal.js' %}{% if nocache %}?nocache={{ version_md5_cache }}{% endif %}"></script>
{% endif %}
5 changes: 5 additions & 0 deletions admin_interface/templatetags/admin_interface_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@ def get_admin_interface_nocache():
return hash_string(__version__)


@register.simple_tag()
def get_use_version_nocache():
return bool(getattr(settings, "ADMIN_TEMPLATE_USE_VERSION_NOCACHE", False))


@register.simple_tag(takes_context=False)
def get_admin_interface_active_date_hierarchy(changelist):
date_field_name = changelist.date_hierarchy
Expand Down
30 changes: 30 additions & 0 deletions tests/test_templatetags.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,36 @@ def test_get_version_nocache(self):
)
self.assertEqual(rendered, hash_manual)

@override_settings(
ADMIN_TEMPLATE_USE_VERSION_NOCACHE=True,
)
def test_use_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 %}"
"{% get_use_version_nocache as nocache %}"
"{% if nocache %}?v={{ version_md5_hash }}{% endif %}"
)
self.assertEqual(rendered, f"?v={hash_manual}")

@override_settings(
ADMIN_TEMPLATE_USE_VERSION_NOCACHE=False,
)
def test_not_use_version_nocache(self):
empty = ""

rendered = self.__render_template(
"{% load admin_interface_tags %}"
"{% get_admin_interface_nocache as version_md5_hash %}"
"{% get_use_version_nocache as nocache %}"
"{% if nocache %}?v={{ version_md5_hash }}{% endif %}"
)
self.assertEqual(rendered, empty)

def test_get_admin_interface_inline_template(self):
headless_template = templatetags.get_admin_interface_inline_template(
"admin/edit_inline/stacked.html"
Expand Down