Statistics
| Branch: | Tag: | Revision:

root / snf-webproject / synnefo / webproject / urls.py @ 2e45abfd

History | View | Annotate | Download (4.6 kB)

1
# Copyright 2011 GRNET S.A. All rights reserved.
2
#
3
# Redistribution and use in source and binary forms, with or
4
# without modification, are permitted provided that the following
5
# conditions are met:
6
#
7
#   1. Redistributions of source code must retain the above
8
#      copyright notice, this list of conditions and the following
9
#      disclaimer.
10
#
11
#   2. Redistributions in binary form must reproduce the above
12
#      copyright notice, this list of conditions and the following
13
#      disclaimer in the documentation and/or other materials
14
#      provided with the distribution.
15
#
16
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27
# POSSIBILITY OF SUCH DAMAGE.
28
#
29
# The views and conclusions contained in the software and
30
# documentation are those of the authors and should not be
31
# interpreted as representing official policies, either expressed
32
# or implied, of GRNET S.A.
33

    
34
import os
35

    
36
from django.conf.urls.defaults import *
37
from synnefo.util.entry_points import extend_urls
38
from django.utils.importlib import import_module
39
from django.views.decorators.csrf import requires_csrf_token
40
from django.template import Context, loader, RequestContext
41
from django import http
42
from django.conf import settings
43

    
44
urlpatterns = patterns('')
45

    
46
ROOT_REDIRECT = getattr(settings, 'WEBPROJECT_ROOT_REDIRECT', None)
47
if ROOT_REDIRECT:
48
    urlpatterns += patterns('django.views.generic.simple',
49
                            url(r'^$', 'redirect_to', {'url': ROOT_REDIRECT}))
50

    
51
urlpatterns += patterns('',
52
    (r'^lang/$', 'synnefo.webproject.i18n.set_language')
53
)
54

    
55
if getattr(settings, 'WEBPROJECT_SERVE_STATIC', settings.DEBUG):
56

    
57
    for module_name, ns in settings.STATIC_FILES.iteritems():
58
        module = import_module(module_name)
59
        app_dir = 'static'
60

    
61
        # hook defined that application contains media files in other than
62
        # ``static`` directory
63
        # (e.g. django.contrib.admin which contains media files in media dir)
64
        if type(ns) == tuple:
65
            app_dir = ns[0]
66
            ns = ns[1]
67

    
68
        static_root = os.path.join(os.path.dirname(module.__file__), app_dir)
69
        if ns:
70
            # app contains static files in <appname>/static/
71
            urlns = ns
72
            url_r = r'^%s%s/(?P<path>.*)$' % (settings.MEDIA_URL.lstrip("/"),
73
                                              urlns)
74
            urlpatterns += patterns('', url(url_r,
75
                 'django.views.static.serve',
76
                 {'document_root': static_root,
77
                  'show_indexes': getattr(settings,
78
                      'WEBPROJECT_STATIC_SHOW_INDEXES', True)}))
79

    
80
        else:
81
            # app contains static files in <appname>/static/<appname>
82
            for fpath in os.listdir(static_root):
83
                urlns = ns + fpath
84
                url_r = r'^%s%s/(?P<path>.*)$' % (settings.MEDIA_URL.lstrip("/"), urlns)
85
                static_root = os.path.join(static_root, urlns)
86
                urlpatterns += patterns('',  url(url_r,
87
                     'django.views.static.serve',
88
                     {'document_root': static_root,
89
                      'show_indexes': getattr(settings,
90
                          'WEBPROJECT_STATIC_SHOW_INDEXES', True)}))
91

    
92
    # also serve the media root after all explicitly defined paths
93
    # to be able to serve uploaded files
94
    urlpatterns += patterns('', url(r'^%s(?P<path>.*)$' % \
95
         settings.MEDIA_URL.lstrip("/"),
96
         'django.views.static.serve',
97
         {'document_root': settings.MEDIA_ROOT,
98
          'show_indexes': getattr(settings,
99
              'WEBPROJECT_STATIC_SHOW_INDEXES', True)}))
100

    
101
urlpatterns = extend_urls(urlpatterns, 'synnefo')
102

    
103

    
104
# This can be called when CsrfViewMiddleware.process_view has not run,
105
# therefore need @requires_csrf_token in case the template needs
106
# {% csrf_token %}.
107
@requires_csrf_token
108
def handle500(request, template_name="500.html"):
109
    t = loader.get_template(template_name)
110
    context = Context({})
111
    try:
112
        context = RequestContext(request)
113
    except:
114
        pass
115
    return http.HttpResponseServerError(t.render(context))
116

    
117
handler500 = handle500