Show More
@@ -1,54 +1,57 b'' | |||
|
1 | 1 | import pytz |
|
2 | 2 | |
|
3 | 3 | from django.shortcuts import redirect |
|
4 | 4 | from django.utils import timezone |
|
5 | 5 | |
|
6 | 6 | from boards import utils |
|
7 | 7 | from boards.models import Ban |
|
8 | 8 | |
|
9 | PATH_BANNED = '/banned/' | |
|
10 | ||
|
9 | 11 | SESSION_TIMEZONE = 'django_timezone' |
|
10 | 12 | |
|
11 | 13 | RESPONSE_CONTENT_TYPE = 'Content-Type' |
|
12 | 14 | |
|
13 | 15 | TYPE_HTML = 'text/html' |
|
14 | 16 | |
|
15 | 17 | |
|
16 | 18 | class BanMiddleware: |
|
17 | 19 | """ |
|
18 | 20 | This is run before showing the thread. Banned users don't need to see |
|
19 | 21 | anything |
|
20 | 22 | """ |
|
21 | 23 | |
|
22 | 24 | def __init__(self, get_response): |
|
23 | 25 | self.get_response = get_response |
|
24 | 26 | |
|
25 | 27 | def __call__(self, request): |
|
26 | 28 | response = self.get_response(request) |
|
27 | 29 | |
|
28 |
if request.path != |
|
|
30 | if request.path != PATH_BANNED: | |
|
29 | 31 | ip = utils.get_client_ip(request) |
|
30 | bans = Ban.objects.filter(ip=ip) | |
|
32 | try: | |
|
33 | ban = Ban.objects.get(ip=ip) | |
|
31 | 34 | |
|
32 | if bans.exists(): | |
|
33 | ban = bans[0] | |
|
34 | 35 | if not ban.can_read: |
|
35 | 36 | return redirect('banned') |
|
37 | except Ban.DoesNotExist: | |
|
38 | pass | |
|
36 | 39 | |
|
37 | 40 | return response |
|
38 | 41 | |
|
39 | 42 | |
|
40 | 43 | class TimezoneMiddleware(object): |
|
41 | 44 | def __init__(self, get_response): |
|
42 | 45 | self.get_response = get_response |
|
43 | 46 | |
|
44 | 47 | def __call__(self, request): |
|
45 | 48 | response = self.get_response(request) |
|
46 | 49 | |
|
47 | 50 | tzname = request.session.get(SESSION_TIMEZONE) |
|
48 | 51 | if tzname: |
|
49 | 52 | timezone.activate(pytz.timezone(tzname)) |
|
50 | 53 | else: |
|
51 | 54 | timezone.deactivate() |
|
52 | 55 | |
|
53 | 56 | return response |
|
54 | 57 |
General Comments 0
You need to be logged in to leave comments.
Login now