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