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