forked from rc/aircox
add middleware for aircox used for timezone and current station selection in admin; add page summary for today's diffusions menu in link title
This commit is contained in:
80
aircox/middleware.py
Normal file
80
aircox/middleware.py
Normal file
@ -0,0 +1,80 @@
|
||||
import pytz
|
||||
import django.utils.timezone as tz
|
||||
|
||||
import aircox.models as models
|
||||
|
||||
|
||||
class AircoxInfo:
|
||||
"""
|
||||
Used to store informations about Aircox on a request. Some of theses
|
||||
information are None when user is anonymous.
|
||||
"""
|
||||
station = None
|
||||
"""
|
||||
Current station
|
||||
"""
|
||||
default_station = False
|
||||
"""
|
||||
Default station is used as the current station
|
||||
"""
|
||||
|
||||
def __init__(self, **kwargs):
|
||||
self.__dict__.update(kwargs)
|
||||
|
||||
|
||||
class AircoxMiddleware(object):
|
||||
"""
|
||||
Middleware used to get default info for the given website. Theses
|
||||
This middleware must be set after the middleware
|
||||
'django.contrib.auth.middleware.AuthenticationMiddleware',
|
||||
"""
|
||||
default_qs = models.Station.objects.filter(default = True)
|
||||
|
||||
def __init__(self, get_response):
|
||||
self.get_response = get_response
|
||||
|
||||
def init_station(self, aircox):
|
||||
station = None
|
||||
try:
|
||||
pk = request.session.get('aircox.station')
|
||||
if pk:
|
||||
station = int(pk)
|
||||
station = models.Station.objects.filter(pk = station).first()
|
||||
|
||||
if not station:
|
||||
pk = None
|
||||
station = self.default_qs.first() or \
|
||||
models.Station.objects.first()
|
||||
|
||||
aircox.station = station
|
||||
aircox.default_station = (pk is None)
|
||||
except:
|
||||
pass
|
||||
|
||||
def init_timezone(self, aircox):
|
||||
# note: later we can use http://freegeoip.net/ on user side if
|
||||
# required
|
||||
timezone = None
|
||||
try:
|
||||
timezone = request.session.get('aircox.timezone')
|
||||
if timezone:
|
||||
timezone = pytz.timezone(timezone)
|
||||
except:
|
||||
pass
|
||||
|
||||
if not timezone:
|
||||
timezone = tz.get_current_timezone()
|
||||
tz.activate(timezone)
|
||||
|
||||
def __call__(self, request):
|
||||
tz.activate(pytz.timezone('Europe/Brussels'))
|
||||
aircox = AircoxInfo()
|
||||
|
||||
if request.user.is_authenticated:
|
||||
self.init_station(aircox)
|
||||
self.init_timezone(aircox)
|
||||
|
||||
request.aircox = aircox
|
||||
return self.get_response(request)
|
||||
|
||||
|
Reference in New Issue
Block a user