5
5
the locale codes.
6
6
"""
7
7
import base64
8
- import urllib
8
+ import urllib .parse
9
+ from urllib .parse import unquote
9
10
from warnings import warn
10
11
12
+ from commonware .middleware import FrameOptionsHeader as OldFrameOptionsHeader
13
+ from commonware .middleware import RobotsTagHeader as OldRobotsTagHeader
11
14
from django .conf import settings
12
15
from django .core .exceptions import MiddlewareNotUsed
13
- from django .http import HttpResponsePermanentRedirect , HttpResponse
14
- from django .utils .encoding import force_text
16
+ from django .http import HttpResponse , HttpResponsePermanentRedirect
17
+ from django .utils .deprecation import MiddlewareMixin
15
18
16
- from . import urlresolvers
17
19
from lib .l10n_utils import translation
18
20
21
+ from . import urlresolvers
22
+
19
23
20
- class LocaleURLMiddleware ( object ) :
24
+ class LocaleURLMiddleware :
21
25
"""
22
26
1. Search for the locale.
23
27
2. Save it in the request.
24
28
3. Strip them from the URL.
25
29
"""
26
30
27
- def __init__ (self ):
31
+ def __init__ (self , get_response = None ):
28
32
if not settings .USE_I18N or not settings .USE_L10N :
29
33
warn ("USE_I18N or USE_L10N is False but LocaleURLMiddleware is "
30
34
"loaded. Consider removing bedrock.base.middleware."
31
- "LocaleURLMiddleware from your MIDDLEWARE_CLASSES setting." )
35
+ "LocaleURLMiddleware from your MIDDLEWARE setting." )
36
+ self .get_response = get_response
37
+
38
+ def __call__ (self , request ):
39
+ response = self .process_request (request )
40
+ if response :
41
+ return response
42
+ return self .get_response (request )
32
43
33
44
def process_request (self , request ):
34
45
prefixer = urlresolvers .Prefixer (request )
@@ -37,11 +48,11 @@ def process_request(self, request):
37
48
38
49
if full_path != request .path :
39
50
query_string = request .META .get ('QUERY_STRING' , '' )
40
- full_path = urllib .quote (full_path .encode ('utf-8' ))
51
+ full_path = urllib .parse . quote (full_path .encode ('utf-8' ))
41
52
42
53
if query_string :
43
54
full_path = '?' .join (
44
- [full_path , force_text (query_string , errors = 'ignore' )])
55
+ [full_path , unquote (query_string , errors = 'ignore' )])
45
56
46
57
response = HttpResponsePermanentRedirect (full_path )
47
58
@@ -58,14 +69,21 @@ def process_request(self, request):
58
69
translation .activate (prefixer .locale or settings .LANGUAGE_CODE )
59
70
60
71
61
- class BasicAuthMiddleware ( object ) :
72
+ class BasicAuthMiddleware :
62
73
"""
63
74
Middleware to protect the entire site with a single basic-auth username and password.
64
75
Set the BASIC_AUTH_CREDS environment variable to enable.
65
76
"""
66
- def __init__ (self ):
77
+ def __init__ (self , get_response = None ):
67
78
if not settings .BASIC_AUTH_CREDS :
68
79
raise MiddlewareNotUsed
80
+ self .get_response = None
81
+
82
+ def __call__ (self , request ):
83
+ response = self .process_request (request )
84
+ if response :
85
+ return response
86
+ return self .get_response (request )
69
87
70
88
def process_request (self , request ):
71
89
required_auth = settings .BASIC_AUTH_CREDS
@@ -85,3 +103,11 @@ def process_request(self, request):
85
103
realm = settings .APP_NAME or 'bedrock-demo'
86
104
response ['WWW-Authenticate' ] = 'Basic realm="{}"' .format (realm )
87
105
return response
106
+
107
+
108
+ class RobotsTagHeader (OldRobotsTagHeader , MiddlewareMixin ):
109
+ pass
110
+
111
+
112
+ class FrameOptionsHeader (OldFrameOptionsHeader , MiddlewareMixin ):
113
+ pass
0 commit comments