From 3b6af9d01a6868c0b02d58d3bf7bac496730f368 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yoann=20Pi=C3=A9tri?= Date: Tue, 16 Feb 2021 12:23:04 +0100 Subject: [PATCH] feat: :art: Allow the use of CDN and generalise the staticfiles. A CDN can be used. Other locations than the debian ones can be used for staticfiles. python3 manage.py runserver is now functionnal Configuration need to be changed to have re2o2.9 behavior --- re2o/settings.py | 28 +++++++++++++++++----------- re2o/settings_default.py | 6 ++++++ re2o/settings_local.example.py | 9 +++++++++ re2o/templatetags/fontawesome.py | 20 ++++++++++++++++++++ templates/base.html | 8 ++++++-- 5 files changed, 58 insertions(+), 13 deletions(-) create mode 100644 re2o/templatetags/fontawesome.py diff --git a/re2o/settings.py b/re2o/settings.py index 0c30814a..c298b9f8 100644 --- a/re2o/settings.py +++ b/re2o/settings.py @@ -173,22 +173,28 @@ if "LOCAL_ROUTERS" in globals(): DATABASE_ROUTERS += LOCAL_ROUTERS # django-bootstrap3 config -BOOTSTRAP3 = { - "css_url": "/javascript/bootstrap/css/bootstrap.min.css", - "javascript_url": "/javascript/bootstrap/js/bootstrap.min.js", - "jquery_url": "/javascript/jquery/jquery.min.js", - "base_url": "/javascript/bootstrap/", - "include_jquery": True, -} -BOOTSTRAP_BASE_URL = "/javascript/bootstrap/" +if USE_CDN: + BOOTSTRAP3 = { + "include_jquery": True, + } +else: + BOOTSTRAP3 = { + "css_url": "/javascript/bootstrap/css/bootstrap.min.css", + "javascript_url": "/javascript/bootstrap/js/bootstrap.min.js", + "jquery_url": "/javascript/jquery/jquery.min.js", + "base_url": "/javascript/bootstrap/", + "include_jquery": True, + } + BOOTSTRAP_BASE_URL = "/javascript/bootstrap/" # Directories where collectstatic should look for static files # Use only absolute paths with '/' delimiters even on Windows -STATICFILES_DIRS = ( +RE2O_STATICFILES_DIRS = ( os.path.join(BASE_DIR, "static").replace("\\", "/"), - "/usr/share/fonts-font-awesome/", - "/usr/share/javascript/", ) + +STATICFILES_DIRS = RE2O_STATICFILES_DIRS + SYSTEM_STATICFILES_DIRS + # Directory where the static files served by the server are stored STATIC_ROOT = os.path.join(BASE_DIR, "static_files") # The URL to access the static files diff --git a/re2o/settings_default.py b/re2o/settings_default.py index 8f552c2b..8b3cfa34 100644 --- a/re2o/settings_default.py +++ b/re2o/settings_default.py @@ -65,6 +65,12 @@ OPTIONNAL_APPS_RE2O = () # Some Django apps you want to add in you local project OPTIONNAL_APPS = OPTIONNAL_APPS_RE2O + () +# Add statiffiles dir that were installed using system packaging +SYSTEM_STATICFILES_DIRS = () + +# Wether to use CDN to retrieve bootstrap, font-aweseome and jquery files +USE_CDN = False + # Set auth password validator AUTH_PASSWORD_VALIDATORS = [ { diff --git a/re2o/settings_local.example.py b/re2o/settings_local.example.py index c1e315b8..2a199bbd 100644 --- a/re2o/settings_local.example.py +++ b/re2o/settings_local.example.py @@ -115,6 +115,15 @@ OPTIONNAL_APPS_RE2O = () # Some Django apps you want to add in you local project OPTIONNAL_APPS = OPTIONNAL_APPS_RE2O + () +# Add statiffiles dir that were installed using system packaging +# Example to reproduce re2o2.9 behavior +# SYSTEM_STATICFILES_DIRS = ("/usr/share/fonts-font-awesome/", "/usr/share/javascript/") +SYSTEM_STATICFILES_DIRS = () + +# Wether to use CDN to retrieve bootstrap, font-aweseome and jquery files +# Default to False +USE_CDN = False + # Set auth password validator AUTH_PASSWORD_VALIDATORS = [ { diff --git a/re2o/templatetags/fontawesome.py b/re2o/templatetags/fontawesome.py new file mode 100644 index 00000000..a68dd250 --- /dev/null +++ b/re2o/templatetags/fontawesome.py @@ -0,0 +1,20 @@ +""" +Templatetags for fontawesome +""" +from django import template +from django.conf import settings +from django.templatetags.static import static + +register = template.Library() + +@register.simple_tag +def font_awesome_url(): + """Return the font awesome url depending on the use of a CDN. + + Returns: + string: url of the font-awesome css file + """ + if settings.USE_CDN: + return "https://pro.fontawesome.com/releases/v5.10.0/css/all.css" + else: + return static("css/font-awesome.min.css") \ No newline at end of file diff --git a/templates/base.html b/templates/base.html index 324891d0..bf23e66c 100644 --- a/templates/base.html +++ b/templates/base.html @@ -28,6 +28,7 @@ with this program; if not, write to the Free Software Foundation, Inc., {% load acl %} {% load self_adhesion %} {% load i18n %} +{% load fontawesome %} {% self_adhesion as var_sa %} @@ -46,13 +47,16 @@ with this program; if not, write to the Free Software Foundation, Inc., {# Preload JavaScript #} {% bootstrap_javascript %} - + {% block custom_js %}{% endblock %} {# Load CSS #} {% bootstrap_css %} - + + {# Load font-awesome #} + + {# load theme #} {% if request.user.is_authenticated %}