forked from rc/aircox
select current station in cms' admin
This commit is contained in:
parent
e7700a3896
commit
fe3f0f0713
|
@ -33,13 +33,30 @@ class AircoxMiddleware(object):
|
||||||
def __init__(self, get_response):
|
def __init__(self, get_response):
|
||||||
self.get_response = get_response
|
self.get_response = get_response
|
||||||
|
|
||||||
def init_station(self, aircox):
|
def init_station(self, request, aircox):
|
||||||
|
# update current station
|
||||||
|
station = request.GET.get('aircox.station')
|
||||||
|
pk = None
|
||||||
|
try:
|
||||||
|
if station:
|
||||||
|
pk = request.GET['aircox.station']
|
||||||
|
if station:
|
||||||
|
pk = int(pk)
|
||||||
|
if models.Station.objects.filter(pk = station).exists():
|
||||||
|
request.session['aircox.station'] = pk
|
||||||
|
except:
|
||||||
|
pass
|
||||||
|
|
||||||
|
# select current station
|
||||||
station = None
|
station = None
|
||||||
|
pk = None
|
||||||
try:
|
try:
|
||||||
pk = request.session.get('aircox.station')
|
pk = request.session.get('aircox.station')
|
||||||
if pk:
|
if pk:
|
||||||
station = int(pk)
|
pk = int(pk)
|
||||||
station = models.Station.objects.filter(pk = station).first()
|
station = models.Station.objects.filter(pk = pk).first()
|
||||||
|
except:
|
||||||
|
pass
|
||||||
|
|
||||||
if not station:
|
if not station:
|
||||||
pk = None
|
pk = None
|
||||||
|
@ -48,10 +65,8 @@ class AircoxMiddleware(object):
|
||||||
|
|
||||||
aircox.station = station
|
aircox.station = station
|
||||||
aircox.default_station = (pk is None)
|
aircox.default_station = (pk is None)
|
||||||
except:
|
|
||||||
pass
|
|
||||||
|
|
||||||
def init_timezone(self, aircox):
|
def init_timezone(self, request, aircox):
|
||||||
# note: later we can use http://freegeoip.net/ on user side if
|
# note: later we can use http://freegeoip.net/ on user side if
|
||||||
# required
|
# required
|
||||||
timezone = None
|
timezone = None
|
||||||
|
@ -70,9 +85,9 @@ class AircoxMiddleware(object):
|
||||||
tz.activate(pytz.timezone('Europe/Brussels'))
|
tz.activate(pytz.timezone('Europe/Brussels'))
|
||||||
aircox = AircoxInfo()
|
aircox = AircoxInfo()
|
||||||
|
|
||||||
if request.user.is_authenticated:
|
if request.user.is_authenticated():
|
||||||
self.init_station(aircox)
|
self.init_station(request, aircox)
|
||||||
self.init_timezone(aircox)
|
self.init_timezone(request, aircox)
|
||||||
|
|
||||||
request.aircox = aircox
|
request.aircox = aircox
|
||||||
return self.get_response(request)
|
return self.get_response(request)
|
||||||
|
|
|
@ -278,7 +278,7 @@ class Publication(Page):
|
||||||
headline = models.TextField(
|
headline = models.TextField(
|
||||||
_('headline'),
|
_('headline'),
|
||||||
blank = True, null = True,
|
blank = True, null = True,
|
||||||
help_text = _('headline of the publication'),
|
help_text = _('headline of the publication, use it as an introduction'),
|
||||||
)
|
)
|
||||||
tags = ClusterTaggableManager(
|
tags = ClusterTaggableManager(
|
||||||
verbose_name = _('tags'),
|
verbose_name = _('tags'),
|
||||||
|
|
|
@ -92,10 +92,21 @@ modeladmin_register(SoundAdmin)
|
||||||
#
|
#
|
||||||
class GenericMenu(Menu):
|
class GenericMenu(Menu):
|
||||||
page_model = models.Publication
|
page_model = models.Publication
|
||||||
|
"""
|
||||||
|
Model of the page for the items
|
||||||
|
"""
|
||||||
explore = False
|
explore = False
|
||||||
"""
|
"""
|
||||||
If True, show page explorer instead of page editor.
|
If True, show page explorer instead of page editor.
|
||||||
"""
|
"""
|
||||||
|
request = None
|
||||||
|
"""
|
||||||
|
Current request
|
||||||
|
"""
|
||||||
|
station = None
|
||||||
|
"""
|
||||||
|
Current station
|
||||||
|
"""
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
super().__init__('')
|
super().__init__('')
|
||||||
|
@ -146,6 +157,12 @@ class GenericMenu(Menu):
|
||||||
self.make_item(item) for item in qs
|
self.make_item(item) for item in qs
|
||||||
]
|
]
|
||||||
|
|
||||||
|
def render_html(self, request):
|
||||||
|
self.request = request
|
||||||
|
self.station = self.request and self.request.aircox.station
|
||||||
|
return super().render_html(request)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#
|
#
|
||||||
# Today's diffusions menu
|
# Today's diffusions menu
|
||||||
|
@ -157,7 +174,11 @@ class TodayMenu(GenericMenu):
|
||||||
page_model = models.DiffusionPage
|
page_model = models.DiffusionPage
|
||||||
|
|
||||||
def get_queryset(self):
|
def get_queryset(self):
|
||||||
return aircox.models.Diffusion.objects.filter(
|
qs = aircox.models.Diffusion.objects
|
||||||
|
if self.station:
|
||||||
|
qs = qs.filter(program__station = self.station)
|
||||||
|
|
||||||
|
return qs.filter(
|
||||||
type = aircox.models.Diffusion.Type.normal,
|
type = aircox.models.Diffusion.Type.normal,
|
||||||
start__contains = tz.now().date(),
|
start__contains = tz.now().date(),
|
||||||
initial__isnull = True,
|
initial__isnull = True,
|
||||||
|
@ -206,8 +227,11 @@ class ProgramsMenu(GenericMenu):
|
||||||
explore = True
|
explore = True
|
||||||
|
|
||||||
def get_queryset(self):
|
def get_queryset(self):
|
||||||
return aircox.models.Program.objects \
|
qs = aircox.models.Program.objects
|
||||||
.filter(active = True, page__isnull = False) \
|
if self.station:
|
||||||
|
qs = qs.filter(station = self.station)
|
||||||
|
|
||||||
|
return qs.filter(active = True, page__isnull = False) \
|
||||||
.filter(stream__isnull = True) \
|
.filter(stream__isnull = True) \
|
||||||
.order_by('name')
|
.order_by('name')
|
||||||
|
|
||||||
|
@ -231,3 +255,61 @@ def register_programs_menu_item():
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
#
|
||||||
|
# Select station
|
||||||
|
#
|
||||||
|
# Submenu hides themselves if there are no children
|
||||||
|
#
|
||||||
|
#
|
||||||
|
class GroupMenuItem(MenuItem):
|
||||||
|
"""
|
||||||
|
Display a list of items based on given list of items
|
||||||
|
"""
|
||||||
|
def __init__(self, label, *args, **kwargs):
|
||||||
|
super().__init__(label, None, *args, **kwargs)
|
||||||
|
|
||||||
|
def get_queryset(self):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def make_item(self, item):
|
||||||
|
pass
|
||||||
|
|
||||||
|
def render_html(self, request):
|
||||||
|
self.request = request
|
||||||
|
self.station = self.request and self.request.aircox.station
|
||||||
|
|
||||||
|
title = '<h1>{}</h1>'.format(self.label)
|
||||||
|
qs = [
|
||||||
|
self.make_item(item).render_html(request)
|
||||||
|
for item in self.get_queryset()
|
||||||
|
]
|
||||||
|
return title + '\n'.join(qs)
|
||||||
|
|
||||||
|
|
||||||
|
class SelectStationMenuItem(GroupMenuItem):
|
||||||
|
"""
|
||||||
|
Menu to display today's diffusions
|
||||||
|
"""
|
||||||
|
def get_queryset(self):
|
||||||
|
return aircox.models.Station.objects.all()
|
||||||
|
|
||||||
|
def make_item(self, station):
|
||||||
|
return MenuItem(
|
||||||
|
station.name,
|
||||||
|
reverse('wagtailadmin_home') + '?aircox.station=' + str(station.pk),
|
||||||
|
classnames = 'icon ' + ('icon-success'
|
||||||
|
if station == self.station else
|
||||||
|
'icon-cross'
|
||||||
|
if not station.active else
|
||||||
|
''
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
@hooks.register('register_settings_menu_item')
|
||||||
|
def register_select_station_menu_item():
|
||||||
|
return SelectStationMenuItem(
|
||||||
|
_('Current Station'), order=10000
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user