select current station in cms' admin
This commit is contained in:
		@ -33,25 +33,40 @@ class AircoxMiddleware(object):
 | 
			
		||||
    def __init__(self, get_response):
 | 
			
		||||
        self.get_response = get_response
 | 
			
		||||
 | 
			
		||||
    def init_station(self, aircox):
 | 
			
		||||
        station = None
 | 
			
		||||
    def init_station(self, request, aircox):
 | 
			
		||||
        # update current station
 | 
			
		||||
        station = request.GET.get('aircox.station')
 | 
			
		||||
        pk = 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)
 | 
			
		||||
            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
 | 
			
		||||
 | 
			
		||||
    def init_timezone(self, aircox):
 | 
			
		||||
        # select current station
 | 
			
		||||
        station = None
 | 
			
		||||
        pk = None
 | 
			
		||||
        try:
 | 
			
		||||
            pk = request.session.get('aircox.station')
 | 
			
		||||
            if pk:
 | 
			
		||||
                pk = int(pk)
 | 
			
		||||
                station = models.Station.objects.filter(pk = pk).first()
 | 
			
		||||
        except:
 | 
			
		||||
            pass
 | 
			
		||||
 | 
			
		||||
        if not station:
 | 
			
		||||
            pk = None
 | 
			
		||||
            station = self.default_qs.first() or \
 | 
			
		||||
                        models.Station.objects.first()
 | 
			
		||||
 | 
			
		||||
        aircox.station = station
 | 
			
		||||
        aircox.default_station = (pk is None)
 | 
			
		||||
 | 
			
		||||
    def init_timezone(self, request, aircox):
 | 
			
		||||
        # note: later we can use http://freegeoip.net/ on user side if
 | 
			
		||||
        # required
 | 
			
		||||
        timezone = None
 | 
			
		||||
@ -70,9 +85,9 @@ class AircoxMiddleware(object):
 | 
			
		||||
        tz.activate(pytz.timezone('Europe/Brussels'))
 | 
			
		||||
        aircox = AircoxInfo()
 | 
			
		||||
 | 
			
		||||
        if request.user.is_authenticated:
 | 
			
		||||
            self.init_station(aircox)
 | 
			
		||||
        self.init_timezone(aircox)
 | 
			
		||||
        if request.user.is_authenticated():
 | 
			
		||||
            self.init_station(request, aircox)
 | 
			
		||||
        self.init_timezone(request, aircox)
 | 
			
		||||
 | 
			
		||||
        request.aircox = aircox
 | 
			
		||||
        return self.get_response(request)
 | 
			
		||||
 | 
			
		||||
@ -278,7 +278,7 @@ class Publication(Page):
 | 
			
		||||
    headline = models.TextField(
 | 
			
		||||
        _('headline'),
 | 
			
		||||
        blank = True, null = True,
 | 
			
		||||
        help_text = _('headline of the publication'),
 | 
			
		||||
        help_text = _('headline of the publication, use it as an introduction'),
 | 
			
		||||
    )
 | 
			
		||||
    tags = ClusterTaggableManager(
 | 
			
		||||
        verbose_name = _('tags'),
 | 
			
		||||
 | 
			
		||||
@ -92,10 +92,21 @@ modeladmin_register(SoundAdmin)
 | 
			
		||||
#
 | 
			
		||||
class GenericMenu(Menu):
 | 
			
		||||
    page_model = models.Publication
 | 
			
		||||
    """
 | 
			
		||||
    Model of the page for the items
 | 
			
		||||
    """
 | 
			
		||||
    explore = False
 | 
			
		||||
    """
 | 
			
		||||
    If True, show page explorer instead of page editor.
 | 
			
		||||
    """
 | 
			
		||||
    request = None
 | 
			
		||||
    """
 | 
			
		||||
    Current request
 | 
			
		||||
    """
 | 
			
		||||
    station = None
 | 
			
		||||
    """
 | 
			
		||||
    Current station
 | 
			
		||||
    """
 | 
			
		||||
 | 
			
		||||
    def __init__(self):
 | 
			
		||||
        super().__init__('')
 | 
			
		||||
@ -146,6 +157,12 @@ class GenericMenu(Menu):
 | 
			
		||||
            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
 | 
			
		||||
@ -157,7 +174,11 @@ class TodayMenu(GenericMenu):
 | 
			
		||||
    page_model = models.DiffusionPage
 | 
			
		||||
 | 
			
		||||
    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,
 | 
			
		||||
            start__contains = tz.now().date(),
 | 
			
		||||
            initial__isnull = True,
 | 
			
		||||
@ -206,10 +227,13 @@ class ProgramsMenu(GenericMenu):
 | 
			
		||||
    explore = True
 | 
			
		||||
 | 
			
		||||
    def get_queryset(self):
 | 
			
		||||
        return aircox.models.Program.objects \
 | 
			
		||||
                    .filter(active = True, page__isnull = False) \
 | 
			
		||||
                    .filter(stream__isnull = True) \
 | 
			
		||||
                    .order_by('name')
 | 
			
		||||
        qs = aircox.models.Program.objects
 | 
			
		||||
        if self.station:
 | 
			
		||||
            qs = qs.filter(station = self.station)
 | 
			
		||||
 | 
			
		||||
        return qs.filter(active = True, page__isnull = False) \
 | 
			
		||||
                 .filter(stream__isnull = True) \
 | 
			
		||||
                 .order_by('name')
 | 
			
		||||
 | 
			
		||||
    def make_item(self, item):
 | 
			
		||||
        return MenuItem(item.name, self.page_url(item))
 | 
			
		||||
@ -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
 | 
			
		||||
    )
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user