Exemple #1
0
    def render(self, context):
        args = [arg.resolve(context) for arg in self.args]
        kwargs = dict([(smart_str(k, 'ascii'), v.resolve(context))
                       for k, v in self.kwargs.items()])

        view_name = self.view_name
        if not self.legacy_view_name:
            view_name = view_name.resolve(context)

        # ensure request object is available
        request = context.get('request', None)
        if request is None:
            from django.core.exceptions import ImproperlyConfigured
            raise ImproperlyConfigured(
                "Request object not available in context. Make sure 'django.core.context_processors.request' is added to TEMPLATE_CONTEXT_PROCESSORS."
            )

        # Try to look up the URL twice: once given the view name, and again
        # relative to what we guess is the "main" app. If they both fail,
        # re-raise the NoReverseMatch unless we're using the
        # {% url ... as var %} construct in which cause return nothing.
        url = ''
        try:
            url = reverse(view_name,
                          request=request,
                          args=args,
                          kwargs=kwargs,
                          current_app=context.current_app)
        except NoReverseMatch, e:
            if settings.SETTINGS_MODULE:
                project_name = settings.SETTINGS_MODULE.split('.')[0]
                try:
                    url = reverse(project_name + '.' + view_name,
                                  args=args,
                                  kwargs=kwargs,
                                  current_app=context.current_app)
                except NoReverseMatch:
                    if self.asvar is None:
                        # Re-raise the original exception, not the one with
                        # the path relative to the project. This makes a
                        # better error message.
                        raise e
            else:
                if self.asvar is None:
                    raise e
 def test_reverse_without_mapping(self):
     settings = {
         'ROOT_URLCONF': 'iframed.tests',
         'IFRAMED_MAPPINGS': [
             ('/appname/viewname', '/refererpath'),
         ]
     }
     expected_result = '/appname/viewname/param'
     with patch_settings(**settings):
         reload(urlresolvers)
         result = urlresolvers.reverse('test',  args=['param'])
         self.assertEqual(result, expected_result)
 def test_reverse_without_mapping_using_default_base(self):
     settings = {
         'ROOT_URLCONF': 'iframed.tests',
         'IFRAMED_DEFAULT_BASE': 'http://parentdomain.com',
         'IFRAMED_MAPPINGS': [
             ('/appname/viewname', '/refererpath'),
         ]
     }
     expected_result = 'http://parentdomain.com/refererpath?id=/param'
     with patch_settings(**settings):
         reload(urlresolvers)
         result = urlresolvers.reverse('test', use_default_base=True,  args=['param'])
         self.assertEqual(result, expected_result)
Exemple #4
0
    def render(self, context):
        args = [arg.resolve(context) for arg in self.args]
        kwargs = dict([(smart_str(k, 'ascii'), v.resolve(context))
                       for k, v in self.kwargs.items()])

        view_name = self.view_name
        if not self.legacy_view_name:
            view_name = view_name.resolve(context)
        
        # ensure request object is available
        request = context.get('request', None)
        if request is None:
            from django.core.exceptions import ImproperlyConfigured
            raise ImproperlyConfigured("Request object not available in context. Make sure 'django.core.context_processors.request' is added to TEMPLATE_CONTEXT_PROCESSORS.")
        
        # Try to look up the URL twice: once given the view name, and again
        # relative to what we guess is the "main" app. If they both fail,
        # re-raise the NoReverseMatch unless we're using the
        # {% url ... as var %} construct in which cause return nothing.
        url = ''
        try:
            url = reverse(view_name, request=request, args=args, kwargs=kwargs, current_app=context.current_app)
        except NoReverseMatch, e:
            if settings.SETTINGS_MODULE:
                project_name = settings.SETTINGS_MODULE.split('.')[0]
                try:
                    url = reverse(project_name + '.' + view_name,
                              args=args, kwargs=kwargs,
                              current_app=context.current_app)
                except NoReverseMatch:
                    if self.asvar is None:
                        # Re-raise the original exception, not the one with
                        # the path relative to the project. This makes a
                        # better error message.
                        raise e
            else:
                if self.asvar is None:
                    raise e
 def test_reverse_with_rewrite(self):
     settings = {
         'ROOT_URLCONF': 'iframed.tests',
         'IFRAMED_REWRITES': {
             '/appname/viewname/': 'http://www.otherdomain/arbitrarypath/',
         },
         'IFRAMED_MAPPINGS': [
             ('/appname/viewname', '/refererpath'),
         ]
     }
     
     expected_result = 'http://www.otherdomain/arbitrarypath/'
     
     with patch_settings(**settings):
         reload(urlresolvers)
         result = urlresolvers.reverse('test', args=[''])
         self.assertEqual(result, expected_result)
 def test_reverse_with_referer(self):
     settings = {
         'ROOT_URLCONF': 'iframed.tests',
         'IFRAMED_MAPPINGS': [
             ('/appname/viewname', '/refererpath'),
         ]
     }
     
     request = self.factory.get('/appnameignored/viewnameignored', **{
         'HTTP_REFERER': 'http://www.referer.com/refererpath/?id=/viewparams/moreparams'
     })
     expected_result = 'http://www.referer.com/refererpath?id=/param'
     
     with patch_settings(**settings):
         reload(urlresolvers)
         middleware = IframedMiddleware()
         middleware.process_request(request)
         result = urlresolvers.reverse('test', request=request,  args=['param'])
         self.assertEqual(result, expected_result)