Commit ff25747d authored by Bryan  BRANCOTTE's avatar Bryan BRANCOTTE
Browse files

keep in cache the url computed for sstatic

parent 30bc2dc6
Pipeline #14211 passed with stage
in 29 seconds
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
{% load static %}
{% load sstatic %} {% load sstatic %}
{% load basetheme_bootstrap %} {% load basetheme_bootstrap %}
{% load i18n %} {% load i18n %}
...@@ -19,9 +18,9 @@ ...@@ -19,9 +18,9 @@
<link href="https://cdn.datatables.net/1.10.16/css/jquery.dataTables.min.css" rel="stylesheet"> <link href="https://cdn.datatables.net/1.10.16/css/jquery.dataTables.min.css" rel="stylesheet">
<link rel="stylesheet" <link rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-slider/10.0.0/css/bootstrap-slider.min.css"/> href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-slider/10.0.0/css/bootstrap-slider.min.css"/>
<link rel="stylesheet" href="{% static '/css/bootstrap-multiselect.css' %}" type="text/css"/> <link rel="stylesheet" href="{% sstatic '/css/bootstrap-multiselect.css' %}" type="text/css"/>
<link rel="stylesheet" href="{% static '/css/basetheme_bootstrap4_pasteur.css' %}"/> <link rel="stylesheet" href="{% sstatic '/css/basetheme_bootstrap4_pasteur.css' %}"/>
<link rel="stylesheet" href="{% static '/css/fonts.css' %}"/> <link rel="stylesheet" href="{% sstatic '/css/fonts.css' %}"/>
{% block extra_css %}{% endblock %} {% block extra_css %}{% endblock %}
</head> </head>
<body class="d-flex flex-column h-100"> <body class="d-flex flex-column h-100">
...@@ -187,12 +186,12 @@ ...@@ -187,12 +186,12 @@
<script src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js"></script> <script src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables.net/buttons/1.5.2/js/dataTables.buttons.min.js"></script> <script src="https://cdn.datatables.net/buttons/1.5.2/js/dataTables.buttons.min.js"></script>
<script src="https://cdn.datatables.net/buttons/1.5.2/js/buttons.flash.min.js"></script> <script src="https://cdn.datatables.net/buttons/1.5.2/js/buttons.flash.min.js"></script>
<script type="text/javascript" src="{% static '/js/bootstrap-multiselect.js' %}"></script> <script type="text/javascript" src="{% sstatic '/js/bootstrap-multiselect.js' %}"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jszip/3.1.3/jszip.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/jszip/3.1.3/jszip.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.36/pdfmake.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.36/pdfmake.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.36/vfs_fonts.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.36/vfs_fonts.js"></script>
<script src="https://cdn.datatables.net/buttons/1.5.2/js/buttons.html5.min.js"></script> <script src="https://cdn.datatables.net/buttons/1.5.2/js/buttons.html5.min.js"></script>
<script src="https://cdn.datatables.net/buttons/1.5.2/js/buttons.print.min.js"></script> <script src="https://cdn.datatables.net/buttons/1.5.2/js/buttons.print.min.js"></script>
<script src="{% static '/js/basetheme_bootstrap.js' %}"></script> <script src="{% sstatic '/js/basetheme_bootstrap.js' %}"></script>
{% block extra_js %}{% endblock %} {% block extra_js %}{% endblock %}
</html> </html>
...@@ -4,6 +4,7 @@ import string ...@@ -4,6 +4,7 @@ import string
from django import template from django import template
from django.conf import settings from django.conf import settings
from django.core.cache import cache
# credits : https://bitbucket.org/ad3w/django-sstatic # credits : https://bitbucket.org/ad3w/django-sstatic
...@@ -12,15 +13,27 @@ register = template.Library() ...@@ -12,15 +13,27 @@ register = template.Library()
@register.simple_tag @register.simple_tag
def sstatic(path): def sstatic(path):
url = cache.get(path)
if url:
return url
url = compute_url_for_path(path)
cache.set(path, url, None)
return url
def compute_url_for_path(path):
''' '''
Returns absolute URL to static file with versioning. Returns absolute URL to static file with versioning.
''' '''
full_path = os.path.join(settings.STATIC_ROOT, path[1:] if path[0] == '/' else path)
try: try:
full_path = os.path.join(settings.STATIC_ROOT, path[1:] if path[0] == '/' else path)
# Get file modification time. # Get file modification time.
mtime = os.path.getmtime(full_path) mtime = os.path.getmtime(full_path)
return '%s%s?%s' % (settings.STATIC_URL[:-1], path, mtime) return '%s%s?%s' % (settings.STATIC_URL[:-1], path, mtime)
except OSError: except OSError:
# Returns normal url if this file was not found in filesystem. pass
return '%s%s?%s' % (settings.STATIC_URL[:-1], path, ''.join( except TypeError:
random.choice(''.join((string.ascii_letters, string.digits))) for _ in range(32))) pass
# Returns normal url if this file was not found in filesystem.
return '%s%s?%s' % (settings.STATIC_URL[:-1], path, ''.join(
random.choice(''.join((string.ascii_letters, string.digits))) for _ in range(4)))
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment