add support for backlinking topiks
authorFrédéric Péters <fpeters@0d.be>
Sun, 31 May 2015 09:59:48 +0000 (11:59 +0200)
committerFrédéric Péters <fpeters@0d.be>
Sun, 31 May 2015 09:59:48 +0000 (11:59 +0200)
panikombo/management/__init__.py [new file with mode: 0644]
panikombo/management/commands/__init__.py [new file with mode: 0644]
panikombo/management/commands/map-item-topik.py [new file with mode: 0644]
panikombo/migrations/0006_itemtopik.py [new file with mode: 0644]
panikombo/models.py

diff --git a/panikombo/management/__init__.py b/panikombo/management/__init__.py
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/panikombo/management/commands/__init__.py b/panikombo/management/commands/__init__.py
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/panikombo/management/commands/map-item-topik.py b/panikombo/management/commands/map-item-topik.py
new file mode 100644 (file)
index 0000000..2799499
--- /dev/null
@@ -0,0 +1,28 @@
+from django.core.management.base import BaseCommand, CommandError
+
+from emissions.models import NewsItem, Episode
+
+from combo.data.models import CellBase
+from panikombo.models import Topik, ItemTopik
+
+class Command(BaseCommand):
+    def handle(self, *args, **kwargs):
+        remaining_item_topik = set(ItemTopik.objects.all())
+        for topik in Topik.objects.all():
+            cells = CellBase.get_cells(page=topik.page)
+            for cell in cells:
+                if not hasattr(cell, 'get_included_items'):
+                    continue
+                for item in cell.get_included_items():
+                    kwargs = {'topik': topik}
+                    if isinstance(item, NewsItem):
+                        kwargs['newsitem'] = item
+                    elif isinstance(item, Episode):
+                        kwargs['episode'] = item
+                    else:
+                        continue
+                    obj, created = ItemTopik.objects.get_or_create(**kwargs)
+                    if obj in remaining_item_topik:
+                        remaining_item_topik.remove(obj)
+        for item_topik in remaining_item_topik:
+            item_topik.delete()
diff --git a/panikombo/migrations/0006_itemtopik.py b/panikombo/migrations/0006_itemtopik.py
new file mode 100644 (file)
index 0000000..69db3e1
--- /dev/null
@@ -0,0 +1,27 @@
+# -*- coding: utf-8 -*-
+from __future__ import unicode_literals
+
+from django.db import models, migrations
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('emissions', '0004_focus_page'),
+        ('panikombo', '0005_topik'),
+    ]
+
+    operations = [
+        migrations.CreateModel(
+            name='ItemTopik',
+            fields=[
+                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
+                ('episode', models.ForeignKey(verbose_name='Episode', blank=True, to='emissions.Episode', null=True)),
+                ('newsitem', models.ForeignKey(verbose_name='News Item', blank=True, to='emissions.NewsItem', null=True)),
+                ('topik', models.ForeignKey(verbose_name=b'Topik', blank=True, to='panikombo.Topik', null=True)),
+            ],
+            options={
+            },
+            bases=(models.Model,),
+        ),
+    ]
index 4f9baeb..102a5ed 100644 (file)
@@ -28,6 +28,11 @@ class SoundCell(CellBase):
         from .forms import SoundCellForm
         return SoundCellForm
 
+    def get_included_items(self):
+        if not self.soundfile:
+            return []
+        return [self.soundfile.episode]
+
     def get_additional_label(self):
         if self.soundfile:
             if self.soundfile.fragment:
@@ -56,6 +61,11 @@ class EpisodeCell(CellBase):
         context['soundfile'] = self.episode.main_sound
         return tmpl.render(context)
 
+    def get_included_items(self):
+        if not self.episode:
+            return []
+        return [self.episode]
+
     def get_default_form_class(self):
         from .forms import EpisodeCellForm
         return EpisodeCellForm
@@ -77,16 +87,19 @@ class EpisodeAutoSelectionCell(CellBase):
     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
-
+    def get_included_items(self):
         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())
+        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',
                         },
@@ -117,17 +130,18 @@ class NewsItemAutoSelectionCell(CellBase):
     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
-
+    def get_included_items(self):
         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())
+        return newsitems_queryset
 
-        context['newsitems'] = 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_default_form_class(self):
@@ -152,3 +166,12 @@ class Topik(models.Model):
     # denormalized from Focus
     got_focus = models.DateTimeField(default=None, null=True, blank=True)
     has_focus = models.BooleanField(default=False)
+
+
+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)