import pytz from django.shortcuts import redirect from django.utils import timezone from django.utils.deprecation import MiddlewareMixin from boards import utils from boards.models import Ban PATH_BANNED = '/banned/' SESSION_TIMEZONE = 'django_timezone' RESPONSE_CONTENT_TYPE = 'Content-Type' TYPE_HTML = 'text/html' class BanMiddleware(MiddlewareMixin): """ This is run before showing the thread. Banned users don't need to see anything """ def process_request(self, request): if request.path != PATH_BANNED: ip = utils.get_client_ip(request) try: ban = Ban.objects.get(ip=ip) if not ban.can_read: return redirect('banned') except Ban.DoesNotExist: pass class TimezoneMiddleware(MiddlewareMixin): def process_request(self, request): tzname = request.session.get(SESSION_TIMEZONE) if tzname: timezone.activate(pytz.timezone(tzname)) else: timezone.deactivate()