def test_urlpattern_reverse(self): for regex, expected, args, kwargs in test_data: try: got = reverse_helper(re.compile(regex), *args, **kwargs) except NoReverseMatch as e: self.assertEqual(expected, NoReverseMatch) else: self.assertEqual(got, expected)
def test_urlpattern_reverse(self): for regex, expected, args, kwargs in test_data: try: got = reverse_helper(re.compile(regex), *args, **kwargs) except NoReverseMatch, e: self.assertEqual(expected, NoReverseMatch) else: self.assertEquals(got, expected)
def get_url(cls, *args, **kargs): """Returns the url for the given handler. The default implementation uses the patterns passed to the active WSGIApplication and the django urlresolvers module to create a url. However, it is different from urlresolvers.reverse() in the following ways: - It does not try to resolve handlers via module loading - It does not support named arguments - It performs some post-prosessing on the url to remove some regex operators that urlresolvers.reverse_helper() seems to miss. - It will try to fill in the left-most missing arguments with the args used in the active request. Args: args: Parameters for the url pattern's groups. kwargs: Optionally contains 'implicit_args' that can either be a boolean or a tuple. When it is True, it will use the arguments to the active request as implicit arguments. When it is False (default), it will not use any implicit arguments. When it is a tuple, it will use the tuple as the implicit arguments. the left-most args if some are missing from args. Returns: The url for this handler/args combination. Raises: NoUrlFoundError: No url pattern for this handler has the same number of args that were passed in. """ app = WSGIApplication.active_instance pattern_map = app._pattern_map implicit_args = kargs.get('implicit_args', ()) if implicit_args == True: implicit_args = app.current_request_args min_params = len(args) urlresolvers = None for pattern_tuple in pattern_map.get(cls, ()): num_params_in_pattern = pattern_tuple[1] if num_params_in_pattern < min_params: continue if urlresolvers is None: from django.core import urlresolvers try: num_implicit_args = max(0, num_params_in_pattern - len(args)) merged_args = implicit_args[:num_implicit_args] + args url = urlresolvers.reverse_helper(pattern_tuple[0], *merged_args) url = url.replace('\\', '') url = url.replace('?', '') return url except urlresolvers.NoReverseMatch: continue logging.warning('get_url failed for Handler name: %r, Args: %r', cls.__name__, args) raise NoUrlFoundError