예제 #1
0
    def _get_response(self, request):
        response = None

        if hasattr(request, 'urlconf'):
            urlconf = request.urlconf
            set_urlconf(urlconf)
            resolver = get_resolver(urlconf)
        else:
            resolver = get_resolver()

        resolver_match = resolver.resolve(request.path_info)
        callback, callback_args, callback_kwargs = resolver_match
        request.resolver_match = resolver_match

        # Apply view middleware
        for middleware_method in self._view_middleware:
            response = middleware_method(request, callback, callback_args, callback_kwargs)
            if response:
                return response

        wrapped_callback = self.make_view_atomic(callback)
        try:
            response = wrapped_callback(request, *callback_args, **callback_kwargs)
        except Exception as e:
            response = self.process_exception_by_middleware(e, request)

        # Complain if the view returned None (a common error).
        if response is None:
            if isinstance(callback, types.FunctionType):    # FBV
                view_name = callback.__name__
            else:                                           # CBV
                view_name = callback.__class__.__name__ + '.__call__'

            raise ValueError(
                "The view %s.%s didn't return an HttpResponse object. It "
                "returned None instead." % (callback.__module__, view_name)
            )

        # If the response supports deferred rendering, apply template
        # response middleware and then render the response
        elif hasattr(response, 'render') and callable(response.render):
            for middleware_method in self._template_response_middleware:
                response = middleware_method(request, response)
                # Complain if the template response middleware returned None (a common error).
                if response is None:
                    raise ValueError(
                        "%s.process_template_response didn't return an "
                        "HttpResponse object. It returned None instead."
                        % (middleware_method.__self__.__class__.__name__)
                    )

            try:
                response = response.render()
            except Exception as e:
                response = self.process_exception_by_middleware(e, request)

        return response
예제 #2
0
 def run(self, django_main_thread):
     logger.debug('Waiting for apps ready_event.')
     self.wait_for_apps_ready(apps, django_main_thread)
     from django.urls import get_resolver
     # Prevent a race condition where URL modules aren't loaded when the
     # reloader starts by accessing the urlconf_module property.
     get_resolver().urlconf_module
     logger.debug('Apps ready_event triggered. Sending autoreload_started signal.')
     autoreload_started.send(sender=self)
     self.run_loop()
예제 #3
0
def response_for_exception(request, exc):
    if isinstance(exc, Http404):
        if settings.DEBUG:
            response = debug.technical_404_response(request, exc)
        else:
            response = get_exception_response(request, get_resolver(get_urlconf()), 404, exc)

    elif isinstance(exc, PermissionDenied):
        logger.warning(
            'Forbidden (Permission denied): %s', request.path,
            extra={'status_code': 403, 'request': request},
        )
        response = get_exception_response(request, get_resolver(get_urlconf()), 403, exc)

    elif isinstance(exc, MultiPartParserError):
        logger.warning(
            'Bad request (Unable to parse request body): %s', request.path,
            extra={'status_code': 400, 'request': request},
        )
        response = get_exception_response(request, get_resolver(get_urlconf()), 400, exc)

    elif isinstance(exc, SuspiciousOperation):
        if isinstance(exc, (RequestDataTooBig, TooManyFieldsSent)):
            # POST data can't be accessed again, otherwise the original
            # exception would be raised.
            request._mark_post_parse_error()

        # The request logger receives events for any problematic request
        # The security logger receives events for all SuspiciousOperations
        security_logger = logging.getLogger('django.security.%s' % exc.__class__.__name__)
        security_logger.error(
            force_text(exc),
            extra={'status_code': 400, 'request': request},
        )
        if settings.DEBUG:
            response = debug.technical_500_response(request, *sys.exc_info(), status_code=400)
        else:
            response = get_exception_response(request, get_resolver(get_urlconf()), 400, exc)

    elif isinstance(exc, SystemExit):
        # Allow sys.exit() to actually exit. See tickets #1023 and #4701
        raise

    else:
        signals.got_request_exception.send(sender=None, request=request)
        response = handle_uncaught_exception(request, get_resolver(get_urlconf()), sys.exc_info())

    # Force a TemplateResponse to be rendered.
    if not getattr(response, 'is_rendered', True) and callable(getattr(response, 'render', None)):
        response = response.render()

    return response
예제 #4
0
 def run(self, django_main_thread):
     logger.debug('Waiting for apps ready_event.')
     self.wait_for_apps_ready(apps, django_main_thread)
     from django.urls import get_resolver
     # Prevent a race condition where URL modules aren't loaded when the
     # reloader starts by accessing the urlconf_module property.
     try:
         get_resolver().urlconf_module
     except Exception:
         # Loading the urlconf can result in errors during development.
         # If this occurs then swallow the error and continue.
         pass
     logger.debug('Apps ready_event triggered. Sending autoreload_started signal.')
     autoreload_started.send(sender=self)
     self.run_loop()
예제 #5
0
    def get_form(self, request, obj=None, **kwargs):
        """Returns modified form for TreeItem model.
        'Parent' field choices are built by sitetree itself.

        """
        if obj is not None and obj.parent is not None:
            self.previous_parent = obj.parent
            previous_parent_id = self.previous_parent.id
        else:
            previous_parent_id = None

        my_choice_field = TreeItemChoiceField(self.tree, initial=previous_parent_id)
        form = super(TreeItemAdmin, self).get_form(request, obj, **kwargs)
        my_choice_field.label = form.base_fields['parent'].label
        my_choice_field.help_text = form.base_fields['parent'].help_text
        # Replace 'parent' TreeItem field with new appropriate one
        form.base_fields['parent'] = my_choice_field

        # Try to resolve all currently registered url names including those in namespaces.
        if not getattr(self, 'known_url_names', False):
            self.known_url_names = []
            self.known_url_rules = []
            resolver = get_resolver(get_urlconf())
            for ns, (url_prefix, ns_resolver) in resolver.namespace_dict.items():
                if ns != 'admin':
                    self._stack_known_urls(ns_resolver.reverse_dict, ns)
            self._stack_known_urls(resolver.reverse_dict)
            self.known_url_rules = sorted(self.known_url_rules)

        form.known_url_names_hint = _(
            'You are seeing this warning because "URL as Pattern" option is active and pattern entered above '
            'seems to be invalid. Currently registered URL pattern names and parameters: ')
        form.known_url_names = self.known_url_names
        form.known_url_rules = self.known_url_rules
        return form
예제 #6
0
    def get_response(self, request):
        """Return an HttpResponse object for the given HttpRequest."""
        # Setup default url resolver for this thread
        set_urlconf(settings.ROOT_URLCONF)

        response = self._middleware_chain(request)

        try:
            # Apply response middleware, regardless of the response
            for middleware_method in self._response_middleware:
                response = middleware_method(request, response)
                # Complain if the response middleware returned None (a common error).
                if response is None:
                    raise ValueError(
                        "%s.process_response didn't return an "
                        "HttpResponse object. It returned None instead."
                        % (middleware_method.__self__.__class__.__name__))
        except Exception:  # Any exception should be gathered and handled
            signals.got_request_exception.send(sender=self.__class__, request=request)
            response = self.handle_uncaught_exception(request, get_resolver(get_urlconf()), sys.exc_info())

        response._closable_objects.append(request)

        # If the exception handler returns a TemplateResponse that has not
        # been rendered, force it to be rendered.
        if not getattr(response, 'is_rendered', True) and callable(getattr(response, 'render', None)):
            response = response.render()

        if response.status_code == 404:
            logger.warning(
                'Not Found: %s', request.path,
                extra={'status_code': 404, 'request': request},
            )

        return response
예제 #7
0
    def __call__(self, request):
        try:
            response = self.get_response(request)
        except Http404 as exc:
            if settings.DEBUG:
                response = debug.technical_404_response(request, exc)
            else:
                response = self.handler.get_exception_response(request, get_resolver(get_urlconf()), 404, exc)

        except PermissionDenied as exc:
            logger.warning(
                'Forbidden (Permission denied): %s', request.path,
                extra={'status_code': 403, 'request': request},
            )
            response = self.handler.get_exception_response(request, get_resolver(get_urlconf()), 403, exc)

        except MultiPartParserError as exc:
            logger.warning(
                'Bad request (Unable to parse request body): %s', request.path,
                extra={'status_code': 400, 'request': request},
            )
            response = self.handler.get_exception_response(request, get_resolver(get_urlconf()), 400, exc)

        except SuspiciousOperation as exc:
            # The request logger receives events for any problematic request
            # The security logger receives events for all SuspiciousOperations
            security_logger = logging.getLogger('django.security.%s' % exc.__class__.__name__)
            security_logger.error(
                force_text(exc),
                extra={'status_code': 400, 'request': request},
            )
            if settings.DEBUG:
                return debug.technical_500_response(request, *sys.exc_info(), status_code=400)

            response = self.handler.get_exception_response(request, get_resolver(get_urlconf()), 400, exc)

        except SystemExit:
            # Allow sys.exit() to actually exit. See tickets #1023 and #4701
            raise

        except Exception:  # Handle everything else.
            # Get the exception info now, in case another exception is thrown later.
            signals.got_request_exception.send(sender=self.handler.__class__, request=request)
            response = self.handler.handle_uncaught_exception(request, get_resolver(get_urlconf()), sys.exc_info())

        return response
예제 #8
0
def language_prefix_patterns_used():
    '''Returns `True` if the `LocalePrefixPattern` is used
       at root level of the urlpatterns, else it returns `False`. '''

    for url_pattern in get_resolver(None).url_patterns:
        if isinstance(url_pattern, LocalePrefixPattern):
            return True
    return False
예제 #9
0
파일: tests.py 프로젝트: Ricard4869/django
 def test_reverse_lazy_object_coercion_by_resolve(self):
     """
     Verifies lazy object returned by reverse_lazy is coerced to
     text by resolve(). Previous to #21043, this would raise a TypeError.
     """
     urls = 'urlpatterns_reverse.named_urls'
     proxy_url = reverse_lazy('named-url1', urlconf=urls)
     resolver = get_resolver(urls)
     resolver.resolve(proxy_url)
예제 #10
0
파일: locale.py 프로젝트: nbsky/django
 def is_language_prefix_patterns_used(self):
     """
     Returns `True` if the `LocaleRegexURLResolver` is used
     at root level of the urlpatterns, else it returns `False`.
     """
     for url_pattern in get_resolver(None).url_patterns:
         if isinstance(url_pattern, LocaleRegexURLResolver):
             return True
     return False
예제 #11
0
파일: tests.py 프로젝트: Ricard4869/django
 def test_resolver_repr(self):
     """
     Test repr of RegexURLResolver, especially when urlconf_name is a list
     (#17892).
     """
     # Pick a resolver from a namespaced URLconf
     resolver = get_resolver('urlpatterns_reverse.namespace_urls')
     sub_resolver = resolver.namespace_dict['test-ns1'][1]
     self.assertIn('<RegexURLPattern list>', repr(sub_resolver))
예제 #12
0
def response_for_exception(request, exc):
    if isinstance(exc, Http404):
        if settings.DEBUG:
            response = debug.technical_404_response(request, exc)
        else:
            response = get_exception_response(request, get_resolver(get_urlconf()), 404, exc)

    elif isinstance(exc, PermissionDenied):
        logger.warning(
            'Forbidden (Permission denied): %s', request.path,
            extra={'status_code': 403, 'request': request},
        )
        response = get_exception_response(request, get_resolver(get_urlconf()), 403, exc)

    elif isinstance(exc, MultiPartParserError):
        logger.warning(
            'Bad request (Unable to parse request body): %s', request.path,
            extra={'status_code': 400, 'request': request},
        )
        response = get_exception_response(request, get_resolver(get_urlconf()), 400, exc)

    elif isinstance(exc, SuspiciousOperation):
        # The request logger receives events for any problematic request
        # The security logger receives events for all SuspiciousOperations
        security_logger = logging.getLogger('django.security.%s' % exc.__class__.__name__)
        security_logger.error(
            force_text(exc),
            extra={'status_code': 400, 'request': request},
        )
        if settings.DEBUG:
            response = debug.technical_500_response(request, *sys.exc_info(), status_code=400)
        else:
            response = get_exception_response(request, get_resolver(get_urlconf()), 400, exc)

    elif isinstance(exc, SystemExit):
        # Allow sys.exit() to actually exit. See tickets #1023 and #4701
        raise

    else:
        signals.got_request_exception.send(sender=None, request=request)
        response = handle_uncaught_exception(request, get_resolver(get_urlconf()), sys.exc_info())

    return response
예제 #13
0
def is_language_prefix_patterns_used(urlconf):
    """
    Return a tuple of two booleans: (
        `True` if i18n_patterns() (LocalePrefixPattern) is used in the URLconf,
        `True` if the default language should be prefixed
    )
    """
    for url_pattern in get_resolver(urlconf).url_patterns:
        if isinstance(url_pattern.pattern, LocalePrefixPattern):
            return True, url_pattern.pattern.prefix_default_language
    return False, False
예제 #14
0
def get_site_dict(app_name='filebrowser'):
    """
    Return a dict with all *deployed* FileBrowser sites that have
    a given app_name.
    """
    if app_name not in _sites_cache:
        return {}
    # Get names of all deployed filebrowser sites with a give app_name
    deployed = get_resolver(get_urlconf()).app_dict[app_name]
    # Get the deployed subset from the cache
    return dict((k, v) for k, v in _sites_cache[app_name].items() if k in deployed)
예제 #15
0
파일: i18n.py 프로젝트: 9nix00/django
def is_language_prefix_patterns_used(urlconf):
    """
    Return a tuple of two booleans: (
        `True` if LocaleRegexURLResolver` is used in the `urlconf`,
        `True` if the default language should be prefixed
    )
    """
    for url_pattern in get_resolver(urlconf).url_patterns:
        if isinstance(url_pattern, LocaleRegexURLResolver):
            return True, url_pattern.prefix_default_language
    return False, False
    def handle(self, *args, **options):
        location = self.get_location()
        file = 'reverse.js'
        fs = FileSystemStorage(location=location)
        if fs.exists(file):
            fs.delete(file)

        urlconf = getattr(settings, 'ROOT_URLCONF', None)
        default_urlresolver = get_resolver(urlconf)
        content = generate_js(default_urlresolver)
        fs.save(file, ContentFile(content))
        if len(sys.argv) > 1 and sys.argv[1] in ['collectstatic_js_reverse']:
            self.stdout.write('js-reverse file written to %s' % (location))  # pragma: no cover
예제 #17
0
def get_default_site(app_name='filebrowser'):
    """
    Returns the default site. This function uses Django's url resolution method to
    obtain the name of the default site.
    """
    # Get the name of the default site:
    resolver = get_resolver(get_urlconf())
    name = 'filebrowser'

    # Django's default name resolution method (see django.core.urlresolvers.reverse())
    app_list = resolver.app_dict[app_name]
    if name not in app_list:
        name = app_list[0]

    return get_site_dict()[name]
예제 #18
0
 def _get_view_func(view):
     urlconf = get_urlconf()
     if get_resolver(urlconf)._is_callback(view):
         mod, func = get_mod_func(view)
         try:
             # Separate the module and function, e.g.
             # 'mymodule.views.myview' -> 'mymodule.views', 'myview').
             return getattr(import_module(mod), func)
         except ImportError:
             # Import may fail because view contains a class name, e.g.
             # 'mymodule.views.ViewContainer.my_view', so mod takes the form
             # 'mymodule.views.ViewContainer'. Parse it again to separate
             # the module and class.
             mod, klass = get_mod_func(mod)
             return getattr(getattr(import_module(mod), klass), func)
예제 #19
0
파일: urls.py 프로젝트: camilonova/django
def check_url_namespaces_unique(app_configs, **kwargs):
    """
    Warn if URL namespaces used in applications aren't unique.
    """
    if not getattr(settings, 'ROOT_URLCONF', None):
        return []

    from django.urls import get_resolver
    resolver = get_resolver()
    all_namespaces = _load_all_namespaces(resolver)
    counter = Counter(all_namespaces)
    non_unique_namespaces = [n for n, count in counter.items() if count > 1]
    errors = []
    for namespace in non_unique_namespaces:
        errors.append(Warning(
            "URL namespace '{}' isn't unique. You may not be able to reverse "
            "all URLs in this namespace".format(namespace),
            id="urls.W005",
        ))
    return errors
예제 #20
0
파일: realms.py 프로젝트: idlesign/pythonz
    def update_syndication_urls(cls, **kwargs):
        """Обновляет url-шаблоны синдикации, заменяя старые новыми."""
        target_namespace = cls.SYNDICATION_NAMESPACE
        linked_category_id_str = 'category_%s' % kwargs['instance'].category_id
        pattern_idx = -1

        resolver = get_resolver(None)
        urlpatterns = getattr(resolver.urlconf_module, 'urlpatterns', resolver.urlconf_module)

        for idx, pattern in enumerate(urlpatterns):
            if getattr(pattern, 'namespace', '') == target_namespace:
                pattern_idx = idx
                if linked_category_id_str in pattern.reverse_dict.keys():
                    # Категория была известна и ранее, перепривязка URL не требуется.
                    return
                break

        if pattern_idx > -1:
            del urlpatterns[pattern_idx]
            urlpatterns += cls.get_syndication_urls()
예제 #21
0
    def process_request(self, request):
        # Allow access if middleware is not activated
        allow_staff = getattr(settings, 'MAINTENANCE_ALLOW_STAFF', True)
        allow_superuser = getattr(settings, 'MAINTENANCE_ALLOW_SUPERUSER', True)

        if not (settings.MAINTENANCE_MODE or maintenance.status()):
            return None

        INTERNAL_IPS = maintenance.IPList(settings.INTERNAL_IPS)

        # Preferentially check HTTP_X_FORWARDED_FOR b/c a proxy
        # server might have obscured REMOTE_ADDR
        for ip in request.META.get('HTTP_X_FORWARDED_FOR', '').split(','):
            if ip.strip() in INTERNAL_IPS:
                return None

        # Allow access if remote ip is in INTERNAL_IPS
        if request.META.get('REMOTE_ADDR') in INTERNAL_IPS:
            return None

        # Allow access if the user doing the request is logged in and a
        # staff member.
        if hasattr(request, 'user'):
            if request.user.is_staff and allow_staff:
                return None

            if request.user.is_superuser and allow_superuser:
                return None

        # Check if a path is explicitly excluded from maintenance mode
        for url in IGNORE_URLS:
            if url.match(request.path_info):
                return None

        # Otherwise show the user the 503 page
        resolver = get_resolver()

        callback, param_dict = resolver.resolve_error_handler('503')

        return callback(request, **param_dict)
예제 #22
0
 def _calculate_user_urlconf(self, request_path, url_conf):
     """
     :param request_path:
     :param url_conf:
     :return:
     """
     if isinstance(url_conf, six.string_types):
         return url_conf
     elif isinstance(url_conf, (list, tuple, set)):
         _temp = None
         for uc in url_conf:
             try:
                 resolver = get_resolver(uc)
                 resolver.resolve(request_path)
                 _temp = uc
                 break
             except Resolver404:
                 continue
         # @TODO: need some decision what should do if result is None
         return _temp
     else:
         return None
예제 #23
0
파일: views.py 프로젝트: 01-/django
 def get_context_data(self, **kwargs):
     view = self.kwargs['view']
     urlconf = get_urlconf()
     if get_resolver(urlconf)._is_callback(view):
         mod, func = get_mod_func(view)
         view_func = getattr(import_module(mod), func)
     else:
         raise Http404
     title, body, metadata = utils.parse_docstring(view_func.__doc__)
     if title:
         title = utils.parse_rst(title, 'view', _('view:') + view)
     if body:
         body = utils.parse_rst(body, 'view', _('view:') + view)
     for key in metadata:
         metadata[key] = utils.parse_rst(metadata[key], 'model', _('view:') + view)
     kwargs.update({
         'name': view,
         'summary': title,
         'body': body,
         'meta': metadata,
     })
     return super(ViewDetailView, self).get_context_data(**kwargs)
예제 #24
0
파일: views.py 프로젝트: mnach/django
 def _get_view_func(view):
     urlconf = get_urlconf()
     if get_resolver(urlconf)._is_callback(view):
         mod, func = get_mod_func(view)
         try:
             # Separate the module and function, e.g.
             # 'mymodule.views.myview' -> 'mymodule.views', 'myview').
             return getattr(import_module(mod), func)
         except ImportError:
             # Import may fail because view contains a class name, e.g.
             # 'mymodule.views.ViewContainer.my_view', so mod takes the form
             # 'mymodule.views.ViewContainer'. Parse it again to separate
             # the module and class.
             mod, klass = get_mod_func(mod)
             return getattr(getattr(import_module(mod), klass), func)
         except AttributeError:
             # PY2 generates incorrect paths for views that are methods,
             # e.g. 'mymodule.views.ViewContainer.my_view' will be
             # listed as 'mymodule.views.my_view' because the class name
             # can't be detected. This causes an AttributeError when
             # trying to resolve the view.
             return None
예제 #25
0
def get_all_remote_methods(resolver=None, ns_prefix=''):
    """
    Returns a dictionary to be used for calling ``djangoCall.configure()``, which itself extends the
    Angular API to the client, offering him to call remote methods.
    """
    if not resolver:
        resolver = get_resolver(get_urlconf())
    result = {}
    for name in resolver.reverse_dict.keys():
        if not isinstance(name, six.string_types):
            continue
        try:
            url = reverse(ns_prefix + name)
            resmgr = resolve(url)
            ViewClass = import_string('{0}.{1}'.format(resmgr.func.__module__, resmgr.func.__name__))
            if isclass(ViewClass) and issubclass(ViewClass, JSONResponseMixin):
                result[name] = _get_remote_methods_for(ViewClass, url)
        except (NoReverseMatch, ImproperlyConfigured):
            pass
    for namespace, ns_pattern in resolver.namespace_dict.items():
        sub_res = get_all_remote_methods(ns_pattern[1], ns_prefix + namespace + ':')
        if sub_res:
            result[namespace] = sub_res
    return result
예제 #26
0
    def get_resource_uri_template(self):
        """
        URI template processor.
        
        See http://bitworking.org/projects/URI-Templates/
        """
        def _convert(template, params=[]):
            """URI template converter"""
            paths = template % dict([p, "{%s}" % p] for p in params)
            return '%s%s' % (get_script_prefix(), paths)
        
        try:
            resource_uri = self.handler.resource_uri()
            
            components = [None, [], {}]

            for i, value in enumerate(resource_uri):
                components[i] = value
        
            lookup_view, args, kwargs = components
            lookup_view = get_callable(lookup_view, True)

            possibilities = get_resolver(None).reverse_dict.getlist(lookup_view)
            
            for possibility, pattern in possibilities:
                for result, params in possibility:
                    if args:
                        if len(args) != len(params):
                            continue
                        return _convert(result, params)
                    else:
                        if set(kwargs.keys()) != set(params):
                            continue
                        return _convert(result, params)
        except:
            return None
예제 #27
0
파일: views.py 프로젝트: matason/django
def raises404(request):
    resolver = get_resolver(None)
    resolver.resolve('/not-in-urls')
예제 #28
0
 def setUp(self):
     self.resolver = get_resolver(None)
예제 #29
0
파일: tests.py 프로젝트: Ricard4869/django
 def test_resolver_reverse(self):
     resolver = get_resolver('urlpatterns_reverse.named_urls')
     self.assertEqual(resolver.reverse('named-url1'), '')
     self.assertEqual(resolver.reverse('named-url2', 'arg'), 'extra/arg/')
     self.assertEqual(resolver.reverse('named-url2', extra='arg'), 'extra/arg/')
예제 #30
0
def resolve_to_name(path, urlconf=None):
    r = get_resolver(urlconf)
    if isinstance(r, RegexURLPattern):
        return _pattern_resolve_to_name(r, path)
    else:
        return _resolver_resolve_to_name(r, path)
예제 #31
0
def get_kwargs_for_view(view_name):
    resolver = get_resolver()
    patterns = resolver.reverse_dict.getlist(view_name)
    return list(set(flatten([p[0][0][1] for p in patterns])))
def response_for_exception(request, exc):
    if isinstance(exc, Http404):
        if settings.DEBUG:
            response = debug.technical_404_response(request, exc)
        else:
            response = get_exception_response(request,
                                              get_resolver(get_urlconf()), 404,
                                              exc)

    elif isinstance(exc, PermissionDenied):
        logger.warning(
            'Forbidden (Permission denied): %s',
            request.path,
            extra={
                'status_code': 403,
                'request': request
            },
        )
        response = get_exception_response(request, get_resolver(get_urlconf()),
                                          403, exc)

    elif isinstance(exc, MultiPartParserError):
        logger.warning(
            'Bad request (Unable to parse request body): %s',
            request.path,
            extra={
                'status_code': 400,
                'request': request
            },
        )
        response = get_exception_response(request, get_resolver(get_urlconf()),
                                          400, exc)

    elif isinstance(exc, SuspiciousOperation):
        if isinstance(exc, (RequestDataTooBig, TooManyFieldsSent)):
            # POST data can't be accessed again, otherwise the original
            # exception would be raised.
            request._mark_post_parse_error()

        # The request logger receives events for any problematic request
        # The security logger receives events for all SuspiciousOperations
        security_logger = logging.getLogger('django.security.%s' %
                                            exc.__class__.__name__)
        security_logger.error(
            str(exc),
            extra={
                'status_code': 400,
                'request': request
            },
        )
        if settings.DEBUG:
            response = debug.technical_500_response(request,
                                                    *sys.exc_info(),
                                                    status_code=400)
        else:
            response = get_exception_response(request,
                                              get_resolver(get_urlconf()), 400,
                                              exc)

    elif isinstance(exc, SystemExit):
        # Allow sys.exit() to actually exit. See tickets #1023 and #4701
        raise Exception

    else:
        signals.got_request_exception.send(sender=None, request=request)
        response = handle_uncaught_exception(request,
                                             get_resolver(get_urlconf()),
                                             sys.exc_info())

    # Force a TemplateResponse to be rendered.
    if not getattr(response, 'is_rendered', True) and callable(
            getattr(response, 'render', None)):
        response = response.render()

    return response
예제 #33
0
파일: tests.py 프로젝트: Ricard4869/django
 def test_namespaced_view_detail(self):
     resolver = get_resolver('urlpatterns_reverse.nested_urls')
     self.assertTrue(resolver._is_callback('urlpatterns_reverse.nested_urls.view1'))
     self.assertTrue(resolver._is_callback('urlpatterns_reverse.nested_urls.view2'))
     self.assertTrue(resolver._is_callback('urlpatterns_reverse.nested_urls.View3'))
     self.assertFalse(resolver._is_callback('urlpatterns_reverse.nested_urls.blub'))
예제 #34
0
def get_named_patterns():
    """Returns a generator containing (pattern name, pattern) tuples."""
    return ((k, v[0][0][0])
            for k, v in get_resolver(None).reverse_dict.items()
            if isinstance(k, str))