Projet

Général

Profil

0001-custom-template-and-static-usage-when-uploaded-via-p.patch

Serghei Mihai, 18 novembre 2014 12:23

Télécharger (4,62 ko)

Voir les différences:

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
usr/local/univnautes/sp/sp/loaders.py
1
from django.conf import settings
2
from django.template.loaders.filesystem import Loader as BaseLoader
3

  
4
template_format = settings.UNIVNAUTES_TEMPLATE_FORMAT
5

  
6
class Loader(BaseLoader):
7

  
8
    def get_template_sources(self, template_name, template_dirs=None):
9
        # if template is in a folder, search for "folder-template"
10
        if '/' in template_name:
11
            template_name = template_name.replace('/', '-')
12
        return super(Loader, self).get_template_sources(template_format.format(path=template_name),
13
                                                        template_dirs)
usr/local/univnautes/sp/sp/settings.py
95 95

  
96 96
# List of callables to import templates from various sources.
97 97
TEMPLATE_LOADERS = (
98
    'sp.loaders.Loader',
98 99
    'django.template.loaders.filesystem.Loader',
99 100
    'django.template.loaders.app_directories.Loader',
100 101
)
......
113 114
# Python dotted path to the WSGI application used by Django's runserver.
114 115
WSGI_APPLICATION = 'sp.wsgi.application'
115 116

  
117
UNIVNAUTES_FILES_UPLOAD_DIR = '/var/db/cpelements'
118
UNIVNAUTES_UPLOADED_FILES_PREFIX = 'captiveportal'
119
UNIVNAUTES_STATIC_FORMAT = '%s-static-{path}' % UNIVNAUTES_UPLOADED_FILES_PREFIX
120
UNIVNAUTES_TEMPLATE_FORMAT = '%s-template-{path}' % UNIVNAUTES_UPLOADED_FILES_PREFIX
121

  
116 122
TEMPLATE_DIRS = (
123
    UNIVNAUTES_FILES_UPLOAD_DIR,
117 124
    os.path.join(PROJECT_PATH, 'sp', 'templates'),
118 125
)
119 126

  
usr/local/univnautes/sp/sp/urls.py
27 27
    url(r'^accounts/login/', 'sp.views.login', name='login'),
28 28
    url(r'^accounts/', include('django.contrib.auth.urls')),
29 29
    url(r'^authsaml2/', include('authentic2.authsaml2.urls')),
30
    url(r'^static/(?P<path>.*)$', 'sp.views.serve'),
30 31
)
31 32

  
32 33
if 'django.contrib.admin' in settings.INSTALLED_APPS:
usr/local/univnautes/sp/sp/views.py
16 16
# You should have received a copy of the GNU Affero General Public License
17 17
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 18

  
19
import urllib2
20
import subprocess
21
import os
22

  
19 23
from django.conf import settings
20 24
from django.views.generic.base import TemplateView
21 25
from django.contrib.auth.decorators import login_required
22 26
from django.contrib.auth.views import login as django_login
23 27
from django.template import Template, Context
24
import urllib2
25 28
from django.http import HttpResponse
26 29
from django.shortcuts import redirect
27
import subprocess
30
from django.views.static import serve
28 31

  
29 32
import pfconfigxml
30 33

  
34
static_format = settings.UNIVNAUTES_STATIC_FORMAT
35

  
31 36
class Homepage(TemplateView):
32 37
    '''Homepage View, displays a welcome message'''
33 38
    template_name = 'homepage.html'
......
75 80
    # 2. disconnect from django
76 81
    return redirect('django_logout_then_login')
77 82

  
83

  
84
def serve(request, path, document_root=None):
85
    custom_static_name = static_format.format(path=path)
86
    # if static in a folder, search for "folder-static"
87
    if '/' in custom_static_name:
88
        custome_static_name = custome_static_name.replace('/', '-')
89
    custom_static_path = os.path.join(settings.UNIVNAUTES_FILES_UPLOAD_DIR,
90
                                      custome_static_name)
91
    if os.path.exists(custom_static_path):
92
        serve(request, custome_static_name,
93
              document_root=settings.UNIVNAUTES_FILES_UPLOAD_DIR)
94
    return serve(request, path, document_root)
78
-