RoughpageFallbackMiddleware   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 15
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 15
rs 10
wmc 5

1 Method

Rating   Name   Duplication   Size   Complexity  
B process_response() 0 14 5
1
# coding=utf-8
2
"""
3
Roughpage Middleware
4
5
Ref: https://github.com/django/django/blob/master/
6
     django/contrib/flatpages/middleware.py
7
"""
8
__author__ = 'Alisue <[email protected]>'
9
from django.http import Http404
10
from roughpages.conf import settings
11
from roughpages.views import roughpage
12
13
try:
14
    # Support a new-middleware/old-middleware
15
    # Ref: https://docs.djangoproject.com/ja/1.10/topics/http/middleware/#upgrading-middleware
16
    from django.utils.deprecation import MiddlewareMixin
17
except ImportError:
18
    MiddlewareMixin = object
19
20
21
class RoughpageFallbackMiddleware(MiddlewareMixin):
22
    def process_response(self, request, response):
23
        if response.status_code != 404:
24
            # Non 404 response should not be treated with this middleware
25
            return response
26
        try:
27
            return roughpage(request, request.path_info)
28
        # Return the original response if any errors happened. Because this
29
        # is a middleware, we can't assume the errors will be caught elsewhere.
30
        except Http404:
31
            return response
32
        except Exception:
33
            if settings.DEBUG:
34
                raise
35
            return response
36