middlewares.py
54 lines
| 1.2 KiB
| text/x-python
|
PythonLexer
/ boards / middlewares.py
neko259
|
r1065 | import pytz | ||
neko259
|
r210 | from django.shortcuts import redirect | ||
neko259
|
r1065 | from django.utils import timezone | ||
neko259
|
r690 | from boards import utils | ||
neko259
|
r210 | from boards.models import Ban | ||
neko259
|
r444 | |||
neko259
|
r1090 | SESSION_TIMEZONE = 'django_timezone' | ||
neko259
|
r444 | RESPONSE_CONTENT_TYPE = 'Content-Type' | ||
TYPE_HTML = 'text/html' | ||||
neko259
|
r210 | |||
class BanMiddleware: | ||||
neko259
|
r444 | """ | ||
This is run before showing the thread. Banned users don't need to see | ||||
anything | ||||
""" | ||||
neko259
|
r210 | |||
neko259
|
r1986 | def __init__(self, get_response): | ||
self.get_response = get_response | ||||
neko259
|
r721 | |||
neko259
|
r1986 | def __call__(self, request): | ||
response = self.get_response(request) | ||||
neko259
|
r210 | |||
neko259
|
r853 | if request.path != '/banned/': | ||
neko259
|
r210 | ip = utils.get_client_ip(request) | ||
neko259
|
r340 | bans = Ban.objects.filter(ip=ip) | ||
neko259
|
r210 | |||
neko259
|
r340 | if bans.exists(): | ||
ban = bans[0] | ||||
if not ban.can_read: | ||||
neko259
|
r542 | return redirect('banned') | ||
neko259
|
r1065 | |||
neko259
|
r1986 | return response | ||
neko259
|
r1065 | |||
class TimezoneMiddleware(object): | ||||
neko259
|
r1986 | def __init__(self, get_response): | ||
self.get_response = get_response | ||||
def __call__(self, request): | ||||
response = self.get_response(request) | ||||
neko259
|
r1090 | tzname = request.session.get(SESSION_TIMEZONE) | ||
neko259
|
r1065 | if tzname: | ||
timezone.activate(pytz.timezone(tzname)) | ||||
else: | ||||
timezone.deactivate() | ||||
neko259
|
r1986 | return response | ||