-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathurls.py
More file actions
executable file
·134 lines (117 loc) · 4.16 KB
/
urls.py
File metadata and controls
executable file
·134 lines (117 loc) · 4.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
#
#
# Copyright (c) 2008-2014 University of Dundee.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# Author: Aleksandra Tarkowska <A(dot)Tarkowska(at)dundee(dot)ac(dot)uk>, 2008.
#
# Version: 1.0
#
import logging
import importlib.util
from django.conf import settings
from django.apps import AppConfig
from django.conf.urls import include
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from django.shortcuts import redirect
from django.urls import re_path, reverse
from django.utils.functional import lazy
from django.views.generic import RedirectView
from django.views.decorators.cache import never_cache
from omeroweb.webclient import views as webclient_views
logger = logging.getLogger(__name__)
# error handler
handler404 = "omeroweb.feedback.views.handler404"
handler500 = "omeroweb.feedback.views.handler500"
reverse_lazy = lazy(reverse, str)
def redirect_urlpatterns():
"""
Helper function to return a URL pattern for index page http://host/.
"""
if settings.INDEX_TEMPLATE is None:
return [
re_path(
r"^$",
never_cache(
RedirectView.as_view(url=reverse_lazy("webindex"), permanent=True)
),
name="index",
)
]
else:
return [
re_path(
r"^$",
never_cache(
RedirectView.as_view(
url=reverse_lazy("webindex_custom"), permanent=True
)
),
name="index",
),
]
# url patterns
urlpatterns = []
for app in settings.ADDITIONAL_APPS:
if isinstance(app, AppConfig):
app_config = app
else:
app_config = AppConfig.create(app)
label = app_config.label
# Depending on how we added the app to INSTALLED_APPS in settings.py,
# include the urls the same way
if "omeroweb.%s" % app in settings.INSTALLED_APPS:
urlmodule = "omeroweb.%s.urls" % app
else:
urlmodule = "%s.urls" % app
# Try to import module.urls.py if it exists (not for corsheaders etc)
urls_found = importlib.util.find_spec(urlmodule)
if urls_found is not None:
try:
__import__(urlmodule)
# https://stackoverflow.com/questions/7580220/django-urls-how-to-map-root-to-app
if label == settings.OMEROWEB_ROOT_APPLICATION:
regex = r"^"
else:
regex = "^%s/" % label
urlpatterns.append(re_path(regex, include(urlmodule)))
except ImportError:
print("""Failed to import %s
Please check if the app is installed and the versions of the app and
OMERO.web are compatible
""" % urlmodule)
raise
else:
logger.debug("Module not found: %s" % urlmodule)
urlpatterns += [
re_path(
r"^favicon\.ico$",
lambda request: redirect("%s%s" % (settings.STATIC_URL, settings.FAVICON_URL)),
name="favicon",
),
re_path(r"^webgateway/", include("omeroweb.webgateway.urls")),
re_path(r"^webadmin/", include("omeroweb.webadmin.urls")),
re_path(r"^webclient/", include("omeroweb.webclient.urls")),
re_path(r"^url/", include("omeroweb.webredirect.urls")),
re_path(r"^feedback/", include("omeroweb.feedback.urls")),
re_path(r"^api/", include("omeroweb.api.urls")),
re_path(r"^index/$", webclient_views.custom_index, name="webindex_custom"),
]
urlpatterns += redirect_urlpatterns()
if settings.DEBUG:
urlpatterns += staticfiles_urlpatterns()