X-Git-Url: https://git.0d.be/?p=django-panik-combo.git;a=blobdiff_plain;f=panikombo%2Fmodels.py;h=2907b86cb22fdf868a7efde55422654cbcd39e10;hp=102a5ed9799511f56916539e6996741b2a4ab21b;hb=5dda5b2c6ebe426361c3f8f4e92e2f070e5a6fe1;hpb=35e33c4a7f2a4ea5f47e8430323532308438bd5e diff --git a/panikombo/models.py b/panikombo/models.py index 102a5ed..2907b86 100644 --- a/panikombo/models.py +++ b/panikombo/models.py @@ -3,9 +3,14 @@ import os from django import template from django.db import models +from django.db.models.functions import Lower +from django.utils.encoding import force_text, python_2_unicode_compatible from django.utils.translation import ugettext_lazy as _ +from ckeditor.fields import RichTextField +from taggit.models import Tag from taggit.managers import TaggableManager +from taggit.utils import parse_tags from combo.data.models import CellBase from combo.data.library import register_cell_class @@ -58,7 +63,8 @@ class EpisodeCell(CellBase): def render(self, context): tmpl = template.loader.get_template('panikombo/episode.html') context['episode'] = self.episode - context['soundfile'] = self.episode.main_sound + if self.episode: + context['soundfile'] = self.episode.main_sound return tmpl.render(context) def get_included_items(self): @@ -78,11 +84,25 @@ class EpisodeCell(CellBase): return '' +def get_parsed_tags(tagstring): + tags = parse_tags(tagstring) + return [x for x in Tag.objects.filter(name__in=tags)] + + @register_cell_class class EpisodeAutoSelectionCell(CellBase): title = models.CharField(_('Title'), max_length=50, blank=True) tags = TaggableManager(_('Tags'), blank=True) + and_tags = models.CharField(_('And Tags'), max_length=100, blank=True) category = models.ForeignKey('emissions.Category', null=True, blank=True) + period = models.PositiveSmallIntegerField( + _('Period'), default=0, + choices=((0, _('All')), + (1, _('Future')), + (2, _('Past')))) + + template_name = 'panikombo/episode_auto_selection.html' + class Meta: verbose_name = _('Automatic Episode Selection') @@ -93,24 +113,49 @@ class EpisodeAutoSelectionCell(CellBase): 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()) + if self.and_tags: + and_tags = get_parsed_tags(self.and_tags) + episodes_queryset = episodes_queryset.filter(tags__in=and_tags) + + if self.period == 0: + 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']) + episodes_queryset = episodes_queryset.order_by('first_diffusion').distinct() + elif self.period == 1: + 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) AND + datetime >= CURRENT_TIMESTAMP'''], + tables=['emissions_diffusion']) + episodes_queryset = episodes_queryset.order_by('-first_diffusion').distinct() + elif self.period == 2: + 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) AND + datetime < CURRENT_TIMESTAMP'''], + tables=['emissions_diffusion']) + episodes_queryset = episodes_queryset.order_by('-first_diffusion').distinct() + return episodes_queryset - def render(self, context): - tmpl = template.loader.get_template('panikombo/episode_auto_selection.html') - context['title'] = self.title - - episodes_queryset = self.get_included_items() - 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_cell_extra_context(self, context): + ctx = super(EpisodeAutoSelectionCell, self).get_cell_extra_context(context) + ctx['title'] = self.title + + if (self.category or self.period or self.tags.count()): + ctx['episodes'] = self.get_included_items() + else: + ctx['episodes'] = [] + + return ctx def get_default_form_class(self): from .forms import EpisodeAutoSelectionCellForm @@ -125,7 +170,12 @@ class EpisodeAutoSelectionCell(CellBase): class NewsItemAutoSelectionCell(CellBase): title = models.CharField(_('Title'), max_length=50, blank=True) tags = TaggableManager(_('Tags'), blank=True) + and_tags = models.CharField(_('And Tags'), max_length=100, blank=True) future = models.BooleanField(_('Future Events Only'), default=True) + category = models.ForeignKey('emissions.NewsCategory', + verbose_name=_('Category'), null=True, blank=True) + + template_name = 'panikombo/newsitem_auto_selection.html' class Meta: verbose_name = _('Automatic Newsitem Selection') @@ -134,15 +184,26 @@ class NewsItemAutoSelectionCell(CellBase): newsitems_queryset = NewsItem.objects.select_related() if self.tags.count(): newsitems_queryset = newsitems_queryset.filter(tags__in=self.tags.all()) + if self.and_tags: + and_tags = get_parsed_tags(self.and_tags) + newsitems_queryset = newsitems_queryset.filter(tags__in=and_tags) if self.future: newsitems_queryset = newsitems_queryset.filter(event_date__gte=date.today()) + if self.category: + newsitems_queryset = newsitems_queryset.filter(category=self.category) + newsitems_queryset = newsitems_queryset.order_by('-event_date', '-creation_timestamp') return newsitems_queryset - def render(self, context): - tmpl = template.loader.get_template('panikombo/newsitem_auto_selection.html') - context['title'] = self.title - context['newsitems'] = self.get_included_items() - return tmpl.render(context) + def get_cell_extra_context(self, context): + ctx = super(NewsItemAutoSelectionCell, self).get_cell_extra_context(context) + ctx['title'] = self.title + + if self.tags.count() or self.future or self.category: + ctx['newsitems'] = self.get_included_items() + else: + ctx['newsitems'] = [] + + return ctx def get_default_form_class(self): from .forms import NewsItemAutoSelectionCellForm @@ -158,6 +219,7 @@ def get_topik_image_path(instance, filename): return os.path.join('images', 'topik', instance.page.slug, os.path.basename(filename)) +@python_2_unicode_compatible class Topik(models.Model): page = models.ForeignKey('data.Page') image = models.ImageField(_('Image'), @@ -167,11 +229,31 @@ class Topik(models.Model): got_focus = models.DateTimeField(default=None, null=True, blank=True) has_focus = models.BooleanField(default=False) + def __str__(self): + if not self.page: + return super(Topik, self).__str__() + return force_text(self.page) + class ItemTopik(models.Model): newsitem = models.ForeignKey('emissions.NewsItem', verbose_name=_('News Item'), null=True, blank=True) episode = models.ForeignKey('emissions.Episode', verbose_name=_('Episode'), null=True, blank=True) - topik = models.ForeignKey('Topik', verbose_name='Topik', - null=True, blank=True) + page = models.ForeignKey('data.Page', null=True, blank=True) + + +@register_cell_class +class TopikCell(CellBase): + topik = models.ForeignKey(Topik, null=True) + text = RichTextField(_('Text'), blank=True, null=True) + + template_name = 'panikombo/topik-cell.html' + + class Meta: + verbose_name = _('Topik') + + def get_additional_label(self): + if not self.topik: + return '' + return self.topik.page.title