Statistics
| Branch: | Tag: | Revision:

root / snf-webproject / synnefo / webproject / urls.py @ 01d00723

History | View | Annotate | Download (4.4 kB)

1 adee02b8 Giorgos Verigakis
# Copyright 2011 GRNET S.A. All rights reserved.
2 2ce0636e Kostas Papadimitriou
#
3 adee02b8 Giorgos Verigakis
# Redistribution and use in source and binary forms, with or
4 adee02b8 Giorgos Verigakis
# without modification, are permitted provided that the following
5 adee02b8 Giorgos Verigakis
# conditions are met:
6 2ce0636e Kostas Papadimitriou
#
7 adee02b8 Giorgos Verigakis
#   1. Redistributions of source code must retain the above
8 adee02b8 Giorgos Verigakis
#      copyright notice, this list of conditions and the following
9 adee02b8 Giorgos Verigakis
#      disclaimer.
10 2ce0636e Kostas Papadimitriou
#
11 adee02b8 Giorgos Verigakis
#   2. Redistributions in binary form must reproduce the above
12 adee02b8 Giorgos Verigakis
#      copyright notice, this list of conditions and the following
13 adee02b8 Giorgos Verigakis
#      disclaimer in the documentation and/or other materials
14 adee02b8 Giorgos Verigakis
#      provided with the distribution.
15 2ce0636e Kostas Papadimitriou
#
16 adee02b8 Giorgos Verigakis
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 adee02b8 Giorgos Verigakis
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 adee02b8 Giorgos Verigakis
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 adee02b8 Giorgos Verigakis
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20 adee02b8 Giorgos Verigakis
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 adee02b8 Giorgos Verigakis
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 adee02b8 Giorgos Verigakis
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 adee02b8 Giorgos Verigakis
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 adee02b8 Giorgos Verigakis
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 adee02b8 Giorgos Verigakis
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 adee02b8 Giorgos Verigakis
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 adee02b8 Giorgos Verigakis
# POSSIBILITY OF SUCH DAMAGE.
28 2ce0636e Kostas Papadimitriou
#
29 adee02b8 Giorgos Verigakis
# The views and conclusions contained in the software and
30 adee02b8 Giorgos Verigakis
# documentation are those of the authors and should not be
31 adee02b8 Giorgos Verigakis
# interpreted as representing official policies, either expressed
32 adee02b8 Giorgos Verigakis
# or implied, of GRNET S.A.
33 00b4f1be Faidon Liambotis
34 b3ae4dd1 Kostas Papadimitriou
import os
35 b3ae4dd1 Kostas Papadimitriou
36 fcf4155e Giorgos Verigakis
from django.conf.urls.defaults import *
37 2ce0636e Kostas Papadimitriou
from synnefo.util.entry_points import extend_urls
38 b3ae4dd1 Kostas Papadimitriou
from django.utils.importlib import import_module
39 2e45abfd Kostas Papadimitriou
from django.template import Context, loader, RequestContext
40 2e45abfd Kostas Papadimitriou
from django import http
41 b3ae4dd1 Kostas Papadimitriou
from django.conf import settings
42 00b4f1be Faidon Liambotis
43 8b812637 Kostas Papadimitriou
urlpatterns = patterns('')
44 8b812637 Kostas Papadimitriou
45 8b812637 Kostas Papadimitriou
ROOT_REDIRECT = getattr(settings, 'WEBPROJECT_ROOT_REDIRECT', None)
46 8b812637 Kostas Papadimitriou
if ROOT_REDIRECT:
47 8b812637 Kostas Papadimitriou
    urlpatterns += patterns('django.views.generic.simple',
48 8b812637 Kostas Papadimitriou
                            url(r'^$', 'redirect_to', {'url': ROOT_REDIRECT}))
49 8b812637 Kostas Papadimitriou
50 8b812637 Kostas Papadimitriou
urlpatterns += patterns('',
51 0aecdbd8 Kostas Papadimitriou
    (r'^lang/$', 'synnefo.webproject.i18n.set_language')
52 2cc9d3a5 Markos Gogoulos
)
53 2ce0636e Kostas Papadimitriou
54 b3ae4dd1 Kostas Papadimitriou
if getattr(settings, 'WEBPROJECT_SERVE_STATIC', settings.DEBUG):
55 bfbdb021 Kostas Papadimitriou
56 bfbdb021 Kostas Papadimitriou
    for module_name, ns in settings.STATIC_FILES.iteritems():
57 bfbdb021 Kostas Papadimitriou
        module = import_module(module_name)
58 bfbdb021 Kostas Papadimitriou
        app_dir = 'static'
59 bfbdb021 Kostas Papadimitriou
60 bfbdb021 Kostas Papadimitriou
        # hook defined that application contains media files in other than
61 bfbdb021 Kostas Papadimitriou
        # ``static`` directory
62 bfbdb021 Kostas Papadimitriou
        # (e.g. django.contrib.admin which contains media files in media dir)
63 bfbdb021 Kostas Papadimitriou
        if type(ns) == tuple:
64 bfbdb021 Kostas Papadimitriou
            app_dir = ns[0]
65 bfbdb021 Kostas Papadimitriou
            ns = ns[1]
66 bfbdb021 Kostas Papadimitriou
67 bfbdb021 Kostas Papadimitriou
        static_root = os.path.join(os.path.dirname(module.__file__), app_dir)
68 b3ae4dd1 Kostas Papadimitriou
        if ns:
69 b3ae4dd1 Kostas Papadimitriou
            # app contains static files in <appname>/static/
70 b3ae4dd1 Kostas Papadimitriou
            urlns = ns
71 bfbdb021 Kostas Papadimitriou
            url_r = r'^%s%s/(?P<path>.*)$' % (settings.MEDIA_URL.lstrip("/"),
72 bfbdb021 Kostas Papadimitriou
                                              urlns)
73 bfbdb021 Kostas Papadimitriou
            urlpatterns += patterns('', url(url_r,
74 b3ae4dd1 Kostas Papadimitriou
                 'django.views.static.serve',
75 b3ae4dd1 Kostas Papadimitriou
                 {'document_root': static_root,
76 b3ae4dd1 Kostas Papadimitriou
                  'show_indexes': getattr(settings,
77 b3ae4dd1 Kostas Papadimitriou
                      'WEBPROJECT_STATIC_SHOW_INDEXES', True)}))
78 6f284708 Kostas Papadimitriou
79 b3ae4dd1 Kostas Papadimitriou
        else:
80 b3ae4dd1 Kostas Papadimitriou
            # app contains static files in <appname>/static/<appname>
81 b3ae4dd1 Kostas Papadimitriou
            for fpath in os.listdir(static_root):
82 b3ae4dd1 Kostas Papadimitriou
                urlns = ns + fpath
83 6f284708 Kostas Papadimitriou
                url_r = r'^%s%s/(?P<path>.*)$' % (settings.MEDIA_URL.lstrip("/"), urlns)
84 6f284708 Kostas Papadimitriou
                static_root = os.path.join(static_root, urlns)
85 6f284708 Kostas Papadimitriou
                urlpatterns += patterns('',  url(url_r,
86 b3ae4dd1 Kostas Papadimitriou
                     'django.views.static.serve',
87 6f284708 Kostas Papadimitriou
                     {'document_root': static_root,
88 b3ae4dd1 Kostas Papadimitriou
                      'show_indexes': getattr(settings,
89 b3ae4dd1 Kostas Papadimitriou
                          'WEBPROJECT_STATIC_SHOW_INDEXES', True)}))
90 b3ae4dd1 Kostas Papadimitriou
91 62a0fc36 Kostas Papadimitriou
    # also serve the media root after all explicitly defined paths
92 62a0fc36 Kostas Papadimitriou
    # to be able to serve uploaded files
93 62a0fc36 Kostas Papadimitriou
    urlpatterns += patterns('', url(r'^%s(?P<path>.*)$' % \
94 62a0fc36 Kostas Papadimitriou
         settings.MEDIA_URL.lstrip("/"),
95 62a0fc36 Kostas Papadimitriou
         'django.views.static.serve',
96 62a0fc36 Kostas Papadimitriou
         {'document_root': settings.MEDIA_ROOT,
97 62a0fc36 Kostas Papadimitriou
          'show_indexes': getattr(settings,
98 62a0fc36 Kostas Papadimitriou
              'WEBPROJECT_STATIC_SHOW_INDEXES', True)}))
99 62a0fc36 Kostas Papadimitriou
100 2e45abfd Kostas Papadimitriou
urlpatterns = extend_urls(urlpatterns, 'synnefo')
101 62a0fc36 Kostas Papadimitriou
102 62a0fc36 Kostas Papadimitriou
103 2e45abfd Kostas Papadimitriou
def handle500(request, template_name="500.html"):
104 2e45abfd Kostas Papadimitriou
    t = loader.get_template(template_name)
105 2e45abfd Kostas Papadimitriou
    context = Context({})
106 2e45abfd Kostas Papadimitriou
    try:
107 2e45abfd Kostas Papadimitriou
        context = RequestContext(request)
108 2e45abfd Kostas Papadimitriou
    except:
109 2e45abfd Kostas Papadimitriou
        pass
110 2e45abfd Kostas Papadimitriou
    return http.HttpResponseServerError(t.render(context))
111 b3ae4dd1 Kostas Papadimitriou
112 2e45abfd Kostas Papadimitriou
handler500 = handle500