add episode auto selection cell
authorFrédéric Péters <fpeters@0d.be>
Mon, 25 May 2015 12:21:36 +0000 (14:21 +0200)
committerFrédéric Péters <fpeters@0d.be>
Mon, 25 May 2015 12:21:36 +0000 (14:21 +0200)
panikombo/forms.py
panikombo/migrations/0003_episodeautoselectioncell.py [new file with mode: 0644]
panikombo/models.py

index ed3991a..d9f60ec 100644 (file)
@@ -1,9 +1,10 @@
 from django import forms
 from django_select2.widgets import HeavySelect2Widget, convert_to_js_string_arr
+from taggit.forms import TagWidget
 
 from emissions.models import SoundFile, Episode
 
-from .models import SoundCell, EpisodeCell
+from .models import SoundCell, EpisodeCell, EpisodeAutoSelectionCell
 from .views import soundfiles, episodes
 
 class SoundFileWidget(HeavySelect2Widget):
@@ -44,3 +45,10 @@ class EpisodeCellForm(forms.ModelForm):
     def __init__(self, *args, **kwargs):
         super(EpisodeCellForm, self).__init__(*args, **kwargs)
         self.fields['episode'].widget = EpisodeWidget(data_view=episodes)
+
+
+class EpisodeAutoSelectionCellForm(forms.ModelForm):
+    class Meta:
+        model = EpisodeAutoSelectionCell
+        fields = ('title', 'tags', 'category')
+        widgets = {'tags': TagWidget()}
diff --git a/panikombo/migrations/0003_episodeautoselectioncell.py b/panikombo/migrations/0003_episodeautoselectioncell.py
new file mode 100644 (file)
index 0000000..70663a7
--- /dev/null
@@ -0,0 +1,38 @@
+# -*- coding: utf-8 -*-
+from __future__ import unicode_literals
+
+from django.db import models, migrations
+import taggit.managers
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('auth', '0001_initial'),
+        ('data', '0005_auto_20150226_0903'),
+        ('emissions', '0003_newsitem_event_date'),
+        ('taggit', '0001_initial'),
+        ('panikombo', '0002_episodecell'),
+    ]
+
+    operations = [
+        migrations.CreateModel(
+            name='EpisodeAutoSelectionCell',
+            fields=[
+                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
+                ('placeholder', models.CharField(max_length=20)),
+                ('order', models.PositiveIntegerField()),
+                ('slug', models.SlugField(verbose_name='Slug', blank=True)),
+                ('public', models.BooleanField(default=True, verbose_name='Public')),
+                ('title', models.CharField(max_length=50, verbose_name='Title', blank=True)),
+                ('category', models.ForeignKey(blank=True, to='emissions.Category', null=True)),
+                ('groups', models.ManyToManyField(to='auth.Group', verbose_name='Groups', blank=True)),
+                ('page', models.ForeignKey(to='data.Page')),
+                ('tags', taggit.managers.TaggableManager(to='taggit.Tag', through='taggit.TaggedItem', blank=True, help_text='A comma-separated list of tags.', verbose_name='Tags')),
+            ],
+            options={
+                'verbose_name': 'Automatic Episode Selection',
+            },
+            bases=(models.Model,),
+        ),
+    ]
index 0f74a5b..0c63576 100644 (file)
@@ -2,9 +2,13 @@ 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
+
 @register_cell_class
 class SoundCell(CellBase):
     soundfile = models.ForeignKey('emissions.SoundFile', null=True)
@@ -59,3 +63,39 @@ class EpisodeCell(CellBase):
                         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:
+            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