import json from django.utils import timezone as tz from django.utils.translation import ugettext_lazy as _ from django.core.urlresolvers import reverse from django.contrib.staticfiles.templatetags.staticfiles import static from django.utils.html import format_html from django.utils.safestring import mark_safe from wagtail.wagtailcore import hooks from wagtail.wagtailadmin.menu import MenuItem, Menu, SubmenuMenuItem from wagtail.wagtailcore.models import PageRevision from wagtail.contrib.modeladmin.options import \ ModelAdmin, ModelAdminGroup, modeladmin_register import aircox.models import aircox_cms.models as models # # ModelAdmin items # class ProgramAdmin(ModelAdmin): model = aircox.models.Program menu_label = _('Programs') menu_icon = 'pick' menu_order = 200 list_display = ('name', 'active') search_fields = ('name',) class DiffusionAdmin(ModelAdmin): model = aircox.models.Diffusion menu_label = _('Diffusions') menu_icon = 'date' menu_order = 200 list_display = ('program', 'start', 'end', 'type', 'initial') list_filter = ('program', 'start', 'type') readonly_fields = ('conflicts',) search_fields = ('program__name', 'start') class ScheduleAdmin(ModelAdmin): model = aircox.models.Schedule menu_label = _('Schedules') menu_icon = 'time' menu_order = 200 list_display = ('program', 'frequency', 'duration', 'initial') list_filter = ('frequency', 'date', 'duration', 'program') class StreamAdmin(ModelAdmin): model = aircox.models.Stream menu_label = _('Streams') menu_icon = 'time' menu_order = 200 list_display = ('program', 'delay', 'begin', 'end') list_filter = ('program', 'delay', 'begin', 'end') class AdvancedAdminGroup(ModelAdminGroup): menu_label = _("Advanced") menu_icon = 'plus-inverse' items = (ProgramAdmin, DiffusionAdmin, ScheduleAdmin, StreamAdmin) modeladmin_register(AdvancedAdminGroup) class CommentAdmin(ModelAdmin): model = models.Comment menu_label = _('Comments') menu_icon = 'pick' menu_order = 300 list_display = ('published', 'publication', 'author', 'date', 'content') list_filter = ('date', 'published') search_fields = ('author', 'content', 'publication__title') modeladmin_register(CommentAdmin) class SoundAdmin(ModelAdmin): model = aircox.models.Sound menu_label = _('Sounds') menu_icon = 'media' menu_order = 350 list_display = ('name', 'program', 'type', 'duration', 'path', 'good_quality', 'public') list_filter = ('program', 'type', 'good_quality', 'public') search_fields = ('name', 'path') modeladmin_register(SoundAdmin) # # Menus with sub-menus # 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__('') def get_queryset(self): """ Return a queryset of items used to display menu """ pass def make_item(self, item): """ Return the instance of MenuItem for the given item in the queryset """ pass def get_parent(self, item): """ Return id of the parent page for the given item of the queryset """ pass def page_url(self, item): if item.page.count(): name = 'wagtailadmin_explore' \ if self.explore else 'wagtailadmin_pages:edit' return reverse(name, args=[item.page.first().id]) parent_page = self.get_parent(item) if not parent_page: return '' return reverse( 'wagtailadmin_pages:add', args= [ self.page_model._meta.app_label, self.page_model._meta.model_name, parent_page.id ] ) @property def registered_menu_items(self): now = tz.now() last_max = now - tz.timedelta(minutes = 10) qs = self.get_queryset() return [ 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) 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 = '