我们从Python开源项目中,提取了以下17个代码示例,用于说明如何使用django.core.urlresolvers.RegexURLPattern()。
def find_url_callbacks(urls_module): if isinstance(urls_module, list): urlpatterns = urls_module hid_list = [] else: urlpatterns = urls_module.urlpatterns hid_list = [urls_module.__name__] for pattern in urlpatterns: if isinstance(pattern, RegexURLPattern): hid_list.append(pattern.callback.__module__) elif isinstance(pattern, RegexURLResolver): hid_list += find_url_callbacks(pattern.urlconf_module) return hid_list # Add templatetags and context processors for each installed app.
def check_resolver(resolver): """ Recursively check the resolver. """ from django.core.urlresolvers import RegexURLPattern, RegexURLResolver warnings = [] for pattern in resolver.url_patterns: if isinstance(pattern, RegexURLResolver): warnings.extend(check_include_trailing_dollar(pattern)) # Check resolver recursively warnings.extend(check_resolver(pattern)) elif isinstance(pattern, RegexURLPattern): warnings.extend(check_pattern_name(pattern)) warnings.extend(check_pattern_startswith_slash(pattern)) return warnings
def decorate_url_pattern(self, pattern, *args, **kwargs): """Decorate a RegexURLPattern or RegexURLResolver. Args: resolve_fn: Either RegexURLPattern or RegexURLResolver, from django.core.urlresolvers This decorates the callback for a url after it gets resolved with self.decorate_method. """ resolve_fn = pattern.resolve @functools.wraps(resolve_fn) def patch_resolve(path): result = resolve_fn(path) if result: result.func = self.decorate_method( result.func, *args, **kwargs) return result pattern.resolve = patch_resolve return pattern
def __call__(self, fn, *args, **kwargs): """Delegate the decoration to the appropriate method.""" if isinstance(fn, functools.partial) and not hasattr(fn, '__module__'): raise ValueError( 'Cannot decorate a bare functools.partial view. ' 'You must invoke functools.update_wrapper(partial_view, ' 'full_view) first.') if not isinstance(fn, type) and isinstance(fn, collections.Callable): return self.decorate_method(fn, *args, **kwargs) elif isinstance(fn, tuple): # Must be an include('my_app.urls') we're decorating return self.decorate_include(fn, *args, **kwargs) elif isinstance(fn, (RegexURLPattern, RegexURLResolver)): return self.decorate_url_pattern(fn, *args, **kwargs) elif isinstance(fn, type) and issubclass(fn, BaseModelAdmin): if issubclass(fn, InlineModelAdmin): raise TypeError("Cannot decorate Inlines. See " "baya.admin.options.BayaInline instead.") return self.decorate_admin(fn, *args, **kwargs) elif isinstance(fn, six.string_types): raise TypeError("Cannot decorate string-path to view: %s." % fn) else: # You'll probably only get here if you're trying to decorate # a class-based view raise TypeError( "Invalid type for requires decorator (%s). " "You cannot decorate class-based views. Decorate " "the URL or the as_view method instead." % type(fn))
def test_returns_regex_url_pattern_if_passed_view_func(self): view = Mock() resolver = simple_url('/home-page/', view, sentinel.kwargs, sentinel.name) self.assertIsInstance(resolver, RegexURLPattern)
def get_views(urlpatterns, base=''): views = OrderedDict() for pattern in urlpatterns: if isinstance(pattern, RegexURLResolver): views.update(get_views( urlpatterns=pattern.url_patterns, base=base + pattern.regex.pattern, )) elif isinstance(pattern, RegexURLPattern): url = base + pattern.regex.pattern url = URL_PARAMETER_PATTERN.sub('{\g<param>}', simplify_regex(url)) views[url] = pattern.callback return views