From 6e5aca044e6262791b52ddd964b6cebd2f249933 Mon Sep 17 00:00:00 2001 From: Serghei MIHAI Date: Tue, 18 Nov 2014 12:20:36 +0100 Subject: [PATCH] custom template and static usage when uploaded via portal's file manager Closes #5570 --- usr/local/univnautes/sp/sp/loaders.py | 13 +++++++++++++ usr/local/univnautes/sp/sp/settings.py | 7 +++++++ usr/local/univnautes/sp/sp/urls.py | 1 + usr/local/univnautes/sp/sp/views.py | 21 +++++++++++++++++++-- 4 files changed, 40 insertions(+), 2 deletions(-) create mode 100644 usr/local/univnautes/sp/sp/loaders.py diff --git a/usr/local/univnautes/sp/sp/loaders.py b/usr/local/univnautes/sp/sp/loaders.py new file mode 100644 index 0000000..ba70a80 --- /dev/null +++ b/usr/local/univnautes/sp/sp/loaders.py @@ -0,0 +1,13 @@ +from django.conf import settings +from django.template.loaders.filesystem import Loader as BaseLoader + +template_format = settings.UNIVNAUTES_TEMPLATE_FORMAT + +class Loader(BaseLoader): + + def get_template_sources(self, template_name, template_dirs=None): + # if template is in a folder, search for "folder-template" + if '/' in template_name: + template_name = template_name.replace('/', '-') + return super(Loader, self).get_template_sources(template_format.format(path=template_name), + template_dirs) diff --git a/usr/local/univnautes/sp/sp/settings.py b/usr/local/univnautes/sp/sp/settings.py index 185c964..4fbd9cf 100644 --- a/usr/local/univnautes/sp/sp/settings.py +++ b/usr/local/univnautes/sp/sp/settings.py @@ -95,6 +95,7 @@ except IOError: # List of callables to import templates from various sources. TEMPLATE_LOADERS = ( + 'sp.loaders.Loader', 'django.template.loaders.filesystem.Loader', 'django.template.loaders.app_directories.Loader', ) @@ -113,7 +114,13 @@ ROOT_URLCONF = 'sp.urls' # Python dotted path to the WSGI application used by Django's runserver. WSGI_APPLICATION = 'sp.wsgi.application' +UNIVNAUTES_FILES_UPLOAD_DIR = '/var/db/cpelements' +UNIVNAUTES_UPLOADED_FILES_PREFIX = 'captiveportal' +UNIVNAUTES_STATIC_FORMAT = '%s-static-{path}' % UNIVNAUTES_UPLOADED_FILES_PREFIX +UNIVNAUTES_TEMPLATE_FORMAT = '%s-template-{path}' % UNIVNAUTES_UPLOADED_FILES_PREFIX + TEMPLATE_DIRS = ( + UNIVNAUTES_FILES_UPLOAD_DIR, os.path.join(PROJECT_PATH, 'sp', 'templates'), ) diff --git a/usr/local/univnautes/sp/sp/urls.py b/usr/local/univnautes/sp/sp/urls.py index 24249a2..b77d4bf 100644 --- a/usr/local/univnautes/sp/sp/urls.py +++ b/usr/local/univnautes/sp/sp/urls.py @@ -27,6 +27,7 @@ urlpatterns = patterns('', url(r'^accounts/login/', 'sp.views.login', name='login'), url(r'^accounts/', include('django.contrib.auth.urls')), url(r'^authsaml2/', include('authentic2.authsaml2.urls')), + url(r'^static/(?P.*)$', 'sp.views.serve'), ) if 'django.contrib.admin' in settings.INSTALLED_APPS: diff --git a/usr/local/univnautes/sp/sp/views.py b/usr/local/univnautes/sp/sp/views.py index 1da7594..5e6fc62 100644 --- a/usr/local/univnautes/sp/sp/views.py +++ b/usr/local/univnautes/sp/sp/views.py @@ -16,18 +16,23 @@ # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . +import urllib2 +import subprocess +import os + from django.conf import settings from django.views.generic.base import TemplateView from django.contrib.auth.decorators import login_required from django.contrib.auth.views import login as django_login from django.template import Template, Context -import urllib2 from django.http import HttpResponse from django.shortcuts import redirect -import subprocess +from django.views.static import serve import pfconfigxml +static_format = settings.UNIVNAUTES_STATIC_FORMAT + class Homepage(TemplateView): '''Homepage View, displays a welcome message''' template_name = 'homepage.html' @@ -75,3 +80,15 @@ def logout(request): # 2. disconnect from django return redirect('django_logout_then_login') + +def serve(request, path, document_root=None): + custom_static_name = static_format.format(path=path) + # if static in a folder, search for "folder-static" + if '/' in custom_static_name: + custome_static_name = custome_static_name.replace('/', '-') + custom_static_path = os.path.join(settings.UNIVNAUTES_FILES_UPLOAD_DIR, + custome_static_name) + if os.path.exists(custom_static_path): + serve(request, custome_static_name, + document_root=settings.UNIVNAUTES_FILES_UPLOAD_DIR) + return serve(request, path, document_root) -- 2.1.3