]> git.0d.be Git - django-panik-combo.git/blob - panikombo/models.py
add support for backlinking topiks
[django-panik-combo.git] / panikombo / models.py
1 from datetime import date
2 import os
3
4 from django import template
5 from django.db import models
6 from django.utils.translation import ugettext_lazy as _
7
8 from taggit.managers import TaggableManager
9
10 from combo.data.models import CellBase
11 from combo.data.library import register_cell_class
12
13 from emissions.models import Episode, NewsItem
14
15 @register_cell_class
16 class SoundCell(CellBase):
17     soundfile = models.ForeignKey('emissions.SoundFile', null=True)
18
19     class Meta:
20         verbose_name = _('Sound')
21
22     def render(self, context):
23         tmpl = template.loader.get_template('panikombo/audio.html')
24         context['soundfile'] = self.soundfile
25         return tmpl.render(context)
26
27     def get_default_form_class(self):
28         from .forms import SoundCellForm
29         return SoundCellForm
30
31     def get_included_items(self):
32         if not self.soundfile:
33             return []
34         return [self.soundfile.episode]
35
36     def get_additional_label(self):
37         if self.soundfile:
38             if self.soundfile.fragment:
39                 return u'%s - %s - %s' % (
40                         self.soundfile.episode.emission.title,
41                         self.soundfile.episode.title,
42                         self.soundfile.title)
43             else:
44                 return u'%s - %s' % (
45                         self.soundfile.episode.emission.title,
46                         self.soundfile.episode.title)
47
48         return ''
49
50
51 @register_cell_class
52 class EpisodeCell(CellBase):
53     episode = models.ForeignKey('emissions.Episode', null=True)
54
55     class Meta:
56         verbose_name = _('Episode')
57
58     def render(self, context):
59         tmpl = template.loader.get_template('panikombo/episode.html')
60         context['episode'] = self.episode
61         context['soundfile'] = self.episode.main_sound
62         return tmpl.render(context)
63
64     def get_included_items(self):
65         if not self.episode:
66             return []
67         return [self.episode]
68
69     def get_default_form_class(self):
70         from .forms import EpisodeCellForm
71         return EpisodeCellForm
72
73     def get_additional_label(self):
74         if self.episode:
75             return u'%s - %s' % (
76                         self.episode.emission.title,
77                         self.episode.title)
78         return ''
79
80
81 @register_cell_class
82 class EpisodeAutoSelectionCell(CellBase):
83     title = models.CharField(_('Title'), max_length=50, blank=True)
84     tags = TaggableManager(_('Tags'), blank=True)
85     category = models.ForeignKey('emissions.Category', null=True, blank=True)
86
87     class Meta:
88         verbose_name = _('Automatic Episode Selection')
89
90     def get_included_items(self):
91         episodes_queryset = Episode.objects.select_related()
92         if self.category:
93             episodes_queryset = episodes_queryset.filter(emission__categories__in=[self.category.id])
94         if self.tags.count():
95             episodes_queryset = episodes_queryset.filter(tags__in=self.tags.all())
96         return episodes_queryset
97
98     def render(self, context):
99         tmpl = template.loader.get_template('panikombo/episode_auto_selection.html')
100         context['title'] = self.title
101
102         episodes_queryset = self.get_included_items()
103         episodes_queryset = episodes_queryset.extra(select={
104                         'first_diffusion': 'emissions_diffusion.datetime',
105                         },
106                         select_params=(False, True),
107                         where=['''datetime = (SELECT MIN(datetime)
108                                                 FROM emissions_diffusion
109                                                WHERE episode_id = emissions_episode.id)'''],
110                         tables=['emissions_diffusion'],
111                     ).order_by('-first_diffusion').distinct()
112         context['episodes'] = episodes_queryset
113         return tmpl.render(context)
114
115     def get_default_form_class(self):
116         from .forms import EpisodeAutoSelectionCellForm
117         return EpisodeAutoSelectionCellForm
118
119     def get_additional_label(self):
120         if self.title:
121             return self.title
122         return ''
123
124 @register_cell_class
125 class NewsItemAutoSelectionCell(CellBase):
126     title = models.CharField(_('Title'), max_length=50, blank=True)
127     tags = TaggableManager(_('Tags'), blank=True)
128     future = models.BooleanField(_('Future Events Only'), default=True)
129
130     class Meta:
131         verbose_name = _('Automatic Newsitem Selection')
132
133     def get_included_items(self):
134         newsitems_queryset = NewsItem.objects.select_related()
135         if self.tags.count():
136             newsitems_queryset = newsitems_queryset.filter(tags__in=self.tags.all())
137         if self.future:
138             newsitems_queryset = newsitems_queryset.filter(event_date__gte=date.today())
139         return newsitems_queryset
140
141     def render(self, context):
142         tmpl = template.loader.get_template('panikombo/newsitem_auto_selection.html')
143         context['title'] = self.title
144         context['newsitems'] = self.get_included_items()
145         return tmpl.render(context)
146
147     def get_default_form_class(self):
148         from .forms import NewsItemAutoSelectionCellForm
149         return NewsItemAutoSelectionCellForm
150
151     def get_additional_label(self):
152         if self.title:
153             return self.title
154         return ''
155
156
157 def get_topik_image_path(instance, filename):
158     return os.path.join('images', 'topik', instance.page.slug,
159             os.path.basename(filename))
160
161 class Topik(models.Model):
162     page = models.ForeignKey('data.Page')
163     image = models.ImageField(_('Image'),
164             upload_to=get_topik_image_path, max_length=250, null=True, blank=True)
165
166     # denormalized from Focus
167     got_focus = models.DateTimeField(default=None, null=True, blank=True)
168     has_focus = models.BooleanField(default=False)
169
170
171 class ItemTopik(models.Model):
172     newsitem = models.ForeignKey('emissions.NewsItem', verbose_name=_('News Item'),
173             null=True, blank=True)
174     episode = models.ForeignKey('emissions.Episode', verbose_name=_('Episode'),
175             null=True, blank=True)
176     topik = models.ForeignKey('Topik', verbose_name='Topik',
177             null=True, blank=True)