X-Git-Url: https://git.0d.be/?p=django-panik-combo.git;a=blobdiff_plain;f=panikombo%2Fmodels.py;h=4f9baeb71bcfbe2ee6d46841cf3d42d7d752e8d9;hp=96021a066ffada1ae2fd23a690470fcf02fd5b6a;hb=efe26dec17e20d9e10317a12a6e8aaf2b7732345;hpb=168b4fc8814408ed95c02293d3dce5a76a789f1c diff --git a/panikombo/models.py b/panikombo/models.py index 96021a0..4f9baeb 100644 --- a/panikombo/models.py +++ b/panikombo/models.py @@ -1,10 +1,17 @@ +from datetime import date +import os + from django import template from django.db import models from django.utils.translation import ugettext_lazy as _ +from taggit.managers import TaggableManager + from combo.data.models import CellBase from combo.data.library import register_cell_class +from emissions.models import Episode, NewsItem + @register_cell_class class SoundCell(CellBase): soundfile = models.ForeignKey('emissions.SoundFile', null=True) @@ -20,3 +27,128 @@ class SoundCell(CellBase): def get_default_form_class(self): from .forms import SoundCellForm return SoundCellForm + + def get_additional_label(self): + if self.soundfile: + if self.soundfile.fragment: + return u'%s - %s - %s' % ( + self.soundfile.episode.emission.title, + self.soundfile.episode.title, + self.soundfile.title) + else: + return u'%s - %s' % ( + self.soundfile.episode.emission.title, + self.soundfile.episode.title) + + return '' + + +@register_cell_class +class EpisodeCell(CellBase): + episode = models.ForeignKey('emissions.Episode', null=True) + + class Meta: + verbose_name = _('Episode') + + def render(self, context): + tmpl = template.loader.get_template('panikombo/episode.html') + context['episode'] = self.episode + context['soundfile'] = self.episode.main_sound + return tmpl.render(context) + + def get_default_form_class(self): + from .forms import EpisodeCellForm + return EpisodeCellForm + + def get_additional_label(self): + if self.episode: + return u'%s - %s' % ( + self.episode.emission.title, + self.episode.title) + return '' + + +@register_cell_class +class EpisodeAutoSelectionCell(CellBase): + title = models.CharField(_('Title'), max_length=50, blank=True) + tags = TaggableManager(_('Tags'), blank=True) + category = models.ForeignKey('emissions.Category', null=True, blank=True) + + class Meta: + verbose_name = _('Automatic Episode Selection') + + def render(self, context): + tmpl = template.loader.get_template('panikombo/episode_auto_selection.html') + context['title'] = self.title + + episodes_queryset = Episode.objects.select_related() + if self.category: + episodes_queryset = episodes_queryset.filter(emission__categories__in=[self.category.id]) + if self.tags.count(): + episodes_queryset = episodes_queryset.filter(tags__in=self.tags.all()) + + episodes_queryset = episodes_queryset.extra(select={ + 'first_diffusion': 'emissions_diffusion.datetime', + }, + select_params=(False, True), + where=['''datetime = (SELECT MIN(datetime) + FROM emissions_diffusion + WHERE episode_id = emissions_episode.id)'''], + tables=['emissions_diffusion'], + ).order_by('-first_diffusion').distinct() + context['episodes'] = episodes_queryset + return tmpl.render(context) + + def get_default_form_class(self): + from .forms import EpisodeAutoSelectionCellForm + return EpisodeAutoSelectionCellForm + + def get_additional_label(self): + if self.title: + return self.title + return '' + +@register_cell_class +class NewsItemAutoSelectionCell(CellBase): + title = models.CharField(_('Title'), max_length=50, blank=True) + tags = TaggableManager(_('Tags'), blank=True) + future = models.BooleanField(_('Future Events Only'), default=True) + + class Meta: + verbose_name = _('Automatic Newsitem Selection') + + def render(self, context): + tmpl = template.loader.get_template('panikombo/newsitem_auto_selection.html') + context['title'] = self.title + + newsitems_queryset = NewsItem.objects.select_related() + if self.tags.count(): + newsitems_queryset = newsitems_queryset.filter(tags__in=self.tags.all()) + if self.future: + newsitems_queryset = newsitems_queryset.filter(event_date__gte=date.today()) + + context['newsitems'] = newsitems_queryset + return tmpl.render(context) + + def get_default_form_class(self): + from .forms import NewsItemAutoSelectionCellForm + return NewsItemAutoSelectionCellForm + + def get_additional_label(self): + if self.title: + return self.title + return '' + + +def get_topik_image_path(instance, filename): + return os.path.join('images', 'topik', instance.page.slug, + os.path.basename(filename)) + +class Topik(models.Model): + page = models.ForeignKey('data.Page') + image = models.ImageField(_('Image'), + upload_to=get_topik_image_path, max_length=250, null=True, blank=True) + + # denormalized from Focus + got_focus = models.DateTimeField(default=None, null=True, blank=True) + has_focus = models.BooleanField(default=False)