def register_views(self, app, ar): umap = defaultdict(list) # url -> [(URL, view)] for view in app.get_views(): if hasattr(view, "url"): for u in self.view_urls(view): umap[u.url] += [(u, view)] for url in umap: mm = {} names = set() for u, v in umap[url]: for m in u.method: mm[m] = v if getattr(v, "menu", None): self.register_app_menu(app, v) if u.name: names.add(u.name) sv = self.site_view(app, mm) # Django 1.4 workaround u_name = u.name if u_name and u_name.startswith("admin:"): if "%" in u_name: u_name = u_name % (app.module, app.app) url = "^../%s/%s/%s" % (app.module, app.app, u.url[1:]) self.admin_patterns += [ RegexURLPattern(url, sv, name=u_name.split(":", 1)[1]) ] u_name = u_name.rsplit("_", 1)[1] ar += [RegexURLPattern(u.url, sv, name=u_name)] for n in names: self.register_named_view(app.module, app.app, n, sv)
def _assert_dispatchable_url(self, name, user): backend = Backend() section = backend.get_section_by_class(MediaBackendSection) subsection = section.get_section_by_class(ImageBackendSection) def callback(self): return HttpResponse('Foo') regexUrl = RegexURLPattern(r'/', callback, name=name) backend.dispatchable_url(regexUrl, section, subsection) factory = RequestFactory() request = factory.get('/') request.user = user return regexUrl.callback(request)
def recurse_patterns(path, pattern_list, page_id): """ Recurse over a list of to-be-hooked patterns for a given path prefix """ newpatterns = [] for pattern in pattern_list: app_pat = pattern.regex.pattern if app_pat.startswith('^'): app_pat = app_pat[1:] regex = r'^%s%s' % (path, app_pat) if isinstance(pattern, RegexURLResolver): # this is an 'include', recurse! resolver = RegexURLResolver(regex, 'cms_appresolver', pattern.default_kwargs, pattern.app_name, pattern.namespace) resolver.page_id = page_id # see lines 243 and 236 of urlresolvers.py to understand the next line resolver._urlconf_module = recurse_patterns( regex, pattern.url_patterns, page_id) else: # Re-do the RegexURLPattern with the new regular expression resolver = RegexURLPattern(regex, pattern.callback, pattern.default_args, pattern.name) resolver.page_id = page_id newpatterns.append(resolver) return newpatterns
def decorate_pattern(urlpattern): callback = urlpattern.callback for decorator in reversed(decorators): callback = decorator(callback) return RegexURLPattern(urlpattern.regex.pattern, callback, urlpattern.default_args, urlpattern.name)
def resolve(self, *args, **kwargs): result = RegexURLPattern.resolve(self, *args, **kwargs) if result: # result = list(result) result.func = self._decorate_with(result.func) # result[0] = self._decorate_with(result[0]) return result
def test_populate_urlconf_no_ssl_urls(self): class URLConf: urlpatterns = [] sub_urlconf = URLConf() sub_urlconf.urlpatterns = [ RegexURLPattern('', logout_view), ] root_urlconf = URLConf() root_urlconf.urlpatterns = [ RegexURLPattern('', login_view), RegexURLResolver('', sub_urlconf), ] self.mw._protected_urls = [] self.mw._walk_module(root_urlconf) self.assertIn(login_view, self.mw._protected_urls) self.assertIn(logout_view, self.mw._protected_urls)
def get_proxy_resolver(urlconf, default_view): from django.conf import settings set_urlconf(urlconf) resolver = get_resolver(urlconf) callback = import_string(default_view) if callback not in resolver.reverse_dict: pattern = RegexURLPattern(r'', callback) resolver.url_patterns.append(pattern) return resolver
def ajax_url(regex, view, ajax_name, default=None, group=None, kwargs=None): if isinstance(view, (list, tuple)): urlconf_module, app_name, namespace = view AjaxTracker.Instance().add_ajax_url(regex, ajax_name, default, group) return RegexURLResolver(regex, urlconf_module, kwargs, app_name=app_name, namespace=namespace) if isinstance(view, six.string_types): raise ImproperlyConfigured('Invalid view specified (%s). Are you passing the callable?' % view) AjaxTracker.Instance().add_ajax_url(regex, ajax_name, default, group) return RegexURLPattern(regex, view, kwargs, ajax_name)
def url(regex, view, kwargs=None, name=None, prefix=''): if isinstance(view, (list, tuple)): # For include(...) processing. urlconf_module, app_name, namespace = view return RegexURLResolver(regex, urlconf_module, kwargs, app_name=app_name, namespace=namespace) else: if isinstance(view, six.string_types): if not view: raise ImproperlyConfigured('Empty URL pattern view name not permitted (for pattern %r)' % regex) if prefix: view = prefix + '.' + view return RegexURLPattern(regex, view, kwargs, name)
def patterns(prefix, *tuples): pattern_list = [] for t in tuples: regex, view_or_include = t[:2] default_kwargs = t[2:] if type(view_or_include) == list: pattern_list.append( RegexURLResolver(regex, view_or_include[0], *default_kwargs)) else: pattern_list.append( RegexURLPattern( regex, prefix and (prefix + '.' + view_or_include) or view_or_include, *default_kwargs)) return pattern_list
def patterns_insert(prefix, pattern_list, index, tuples): from django.core.urlresolvers import RegexURLPattern, RegexURLResolver pattern_list = pattern_list if ( misc.isList(pattern_list)) else [pattern_list] for t in tuples: regex, view_or_include = t[:2] default_kwargs = t[2:] if type(view_or_include) == list: pattern_list.append( RegexURLResolver(regex, view_or_include[0], *default_kwargs)) else: pattern_list.append( RegexURLPattern( regex, prefix and (prefix + '.' + view_or_include) or view_or_include, *default_kwargs)) return pattern_list
def urlpatterns(self, prefix, pattern_dict): patterns = self.patterns(prefix, pattern_dict) urlpatterns = [] for p in patterns: urlpatterns.append(RegexURLPattern(*p)) return urlpatterns
def resolve(self, *args, **kwargs): result = RegexURLPattern.resolve(self, *args, **kwargs) if result: for func in self._decorate_with: result.func = func(result.func) return result
def url_annotator(view): view.pattern = RegexURLPattern(pattern, view, extra, name) return view
def get_urls(self, *args, **kwds): urls = super(TrackerAdmin, self).get_urls(*args, **kwds) urls.insert(0, RegexURLPattern(r'sync_all$', sync_all)) return urls
from django.core.urlresolvers import RegexURLPattern from rest_framework.routers import DefaultRouter from archiveit.wasapi.views import WebdataQueryViewSet, JobsViewSet, update_result_file_state, JobResultViewSet router = DefaultRouter(trailing_slash=False) router.register(r'webdata', WebdataQueryViewSet) router.register(r'jobs/(?P<jobid>\d+)/result', JobResultViewSet) router.register(r'jobs', JobsViewSet) router.urls.append( RegexURLPattern(r'^update_result_file_state/(?P<filename>.*)', update_result_file_state)) urlpatterns = router.urls
def resolve(self, path): if get_metro()['multiple_cities']: return None return RegexURLPattern.resolve(self, path)