Spirit/spirit/middleware.py

104 lines
2.8 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
2014-11-03 14:26:53 +00:00
from __future__ import absolute_import
from __future__ import unicode_literals
2014-05-09 21:34:48 +00:00
import pytz
from django.utils import timezone
from django.contrib.auth import get_user_model
from django.conf import settings
from django.contrib.auth import logout
2014-05-20 22:08:55 +00:00
from django.core.urlresolvers import resolve
from django.contrib.auth.views import redirect_to_login
2014-05-09 21:34:48 +00:00
User = get_user_model()
class XForwardedForMiddleware(object):
def process_request(self, request):
if not settings.DEBUG:
request.META['REMOTE_ADDR'] = request.META['HTTP_X_FORWARDED_FOR'].split(",")[-1].strip()
class TimezoneMiddleware(object):
def process_request(self, request):
if request.user.is_authenticated():
timezone.activate(request.user.timezone)
else:
timezone.deactivate()
class LastIPMiddleware(object):
def process_request(self, request):
if not request.user.is_authenticated():
return
last_ip = request.META['REMOTE_ADDR'].strip()
if request.user.last_ip == last_ip:
return
User.objects.filter(pk=request.user.pk)\
.update(last_ip=last_ip)
class LastSeenMiddleware(object):
def process_request(self, request):
if not request.user.is_authenticated():
return
threshold = settings.ST_USER_LAST_SEEN_THRESHOLD_MINUTES * 60
delta = timezone.now() - request.user.last_seen
if delta.seconds < threshold:
return
User.objects.filter(pk=request.user.pk)\
.update(last_seen=timezone.now())
class ActiveUserMiddleware(object):
def process_request(self, request):
if not request.user.is_authenticated():
return
if not request.user.is_active:
2014-05-20 22:08:55 +00:00
logout(request)
class PrivateForumMiddleware(object):
def process_request(self, request):
if not settings.ST_PRIVATE_FORUM:
return
if request.user.is_authenticated():
return
resolver_match = resolve(request.path)
if resolver_match.app_name != 'spirit':
return
# Namespacing /user/ would be better but breaks current urls namespace.
2014-09-10 22:23:56 +00:00
url_whitelist = {'user-login',
2014-05-20 22:08:55 +00:00
'user-logout',
'user-register',
'resend-activation',
'registration-activation',
'password-reset',
'password-reset-done',
'password-reset-confirm',
2014-09-10 22:23:56 +00:00
'password-reset-complete'}
2014-05-20 22:08:55 +00:00
if resolver_match.url_name in url_whitelist:
return
return redirect_to_login(next=request.get_full_path(),
login_url=settings.LOGIN_URL)