]> git.0d.be Git - django-panik-combo.git/blob - panikombo/management/commands/map-item-topik.py
27994999d5742421219f18edeeacacc3e9a97ecd
[django-panik-combo.git] / panikombo / management / commands / map-item-topik.py
1 from django.core.management.base import BaseCommand, CommandError
2
3 from emissions.models import NewsItem, Episode
4
5 from combo.data.models import CellBase
6 from panikombo.models import Topik, ItemTopik
7
8 class Command(BaseCommand):
9     def handle(self, *args, **kwargs):
10         remaining_item_topik = set(ItemTopik.objects.all())
11         for topik in Topik.objects.all():
12             cells = CellBase.get_cells(page=topik.page)
13             for cell in cells:
14                 if not hasattr(cell, 'get_included_items'):
15                     continue
16                 for item in cell.get_included_items():
17                     kwargs = {'topik': topik}
18                     if isinstance(item, NewsItem):
19                         kwargs['newsitem'] = item
20                     elif isinstance(item, Episode):
21                         kwargs['episode'] = item
22                     else:
23                         continue
24                     obj, created = ItemTopik.objects.get_or_create(**kwargs)
25                     if obj in remaining_item_topik:
26                         remaining_item_topik.remove(obj)
27         for item_topik in remaining_item_topik:
28             item_topik.delete()