add minimalistic 'episode' cell type
authorFrédéric Péters <fpeters@0d.be>
Wed, 20 May 2015 11:48:37 +0000 (13:48 +0200)
committerFrédéric Péters <fpeters@0d.be>
Wed, 20 May 2015 11:48:37 +0000 (13:48 +0200)
panikombo/forms.py
panikombo/migrations/0002_episodecell.py [new file with mode: 0644]
panikombo/models.py
panikombo/views.py

index 57f0329..ed3991a 100644 (file)
@@ -1,10 +1,10 @@
 from django import forms
 from django_select2.widgets import HeavySelect2Widget, convert_to_js_string_arr
 
-from emissions.models import SoundFile
+from emissions.models import SoundFile, Episode
 
-from .models import SoundCell
-from .views import soundfiles
+from .models import SoundCell, EpisodeCell
+from .views import soundfiles, episodes
 
 class SoundFileWidget(HeavySelect2Widget):
     def render_texts(self, selected_choices, choices):
@@ -25,3 +25,22 @@ class SoundCellForm(forms.ModelForm):
     def __init__(self, *args, **kwargs):
         super(SoundCellForm, self).__init__(*args, **kwargs)
         self.fields['soundfile'].widget = SoundFileWidget(data_view=soundfiles)
+
+
+class EpisodeWidget(HeavySelect2Widget):
+    def render_texts(self, selected_choices, choices):
+        queryset = Episode.objects.filter(id__in=selected_choices)
+        def fmt(episode):
+            return '%s - %s' % (episode.emission.title, episode.title)
+        texts = [fmt(episode) for episode in queryset.select_related()]
+        return convert_to_js_string_arr(texts)
+
+
+class EpisodeCellForm(forms.ModelForm):
+    class Meta:
+        model = EpisodeCell
+        fields = ('episode', )
+
+    def __init__(self, *args, **kwargs):
+        super(EpisodeCellForm, self).__init__(*args, **kwargs)
+        self.fields['episode'].widget = EpisodeWidget(data_view=episodes)
diff --git a/panikombo/migrations/0002_episodecell.py b/panikombo/migrations/0002_episodecell.py
new file mode 100644 (file)
index 0000000..712f20b
--- /dev/null
@@ -0,0 +1,34 @@
+# -*- coding: utf-8 -*-
+from __future__ import unicode_literals
+
+from django.db import models, migrations
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('auth', '0001_initial'),
+        ('emissions', '0003_newsitem_event_date'),
+        ('data', '0005_auto_20150226_0903'),
+        ('panikombo', '0001_initial'),
+    ]
+
+    operations = [
+        migrations.CreateModel(
+            name='EpisodeCell',
+            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')),
+                ('episode', models.ForeignKey(to='emissions.Episode', null=True)),
+                ('groups', models.ManyToManyField(to='auth.Group', verbose_name='Groups', blank=True)),
+                ('page', models.ForeignKey(to='data.Page')),
+            ],
+            options={
+                'verbose_name': 'Episode',
+            },
+            bases=(models.Model,),
+        ),
+    ]
index 656b2cf..0f74a5b 100644 (file)
@@ -34,3 +34,28 @@ class SoundCell(CellBase):
                         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 ''
index 9fc516d..47262ad 100644 (file)
@@ -1,7 +1,7 @@
 from django.db.models import Q
 import django_select2
 
-from emissions.models import SoundFile
+from emissions.models import SoundFile, Episode
 
 class SoundFilesView(django_select2.views.Select2View):
     def get_results(self, request, terms, page, context):
@@ -17,3 +17,16 @@ class SoundFilesView(django_select2.views.Select2View):
         return ('nil', False, ((x.id, fmt(x)) for x in queryset.select_related()))
 
 soundfiles = SoundFilesView.as_view()
+
+
+class EpisodesView(django_select2.views.Select2View):
+    def get_results(self, request, terms, page, context):
+        queryset = Episode.objects.all()
+        for term in terms.split():
+            queryset = queryset.filter(Q(title__icontains=term) |
+                    Q(emission__title__icontains=term))
+        def fmt(episode):
+            return '%s - %s' % (episode.emission.title, episode.title)
+        return ('nil', False, ((x.id, fmt(x)) for x in queryset.select_related()))
+
+episodes = EpisodesView.as_view()