add Topik object, wrapper around Page
[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_additional_label(self):
32         if self.soundfile:
33             if self.soundfile.fragment:
34                 return u'%s - %s - %s' % (
35                         self.soundfile.episode.emission.title,
36                         self.soundfile.episode.title,
37                         self.soundfile.title)
38             else:
39                 return u'%s - %s' % (
40                         self.soundfile.episode.emission.title,
41                         self.soundfile.episode.title)
42
43         return ''
44
45
46 @register_cell_class
47 class EpisodeCell(CellBase):
48     episode = models.ForeignKey('emissions.Episode', null=True)
49
50     class Meta:
51         verbose_name = _('Episode')
52
53     def render(self, context):
54         tmpl = template.loader.get_template('panikombo/episode.html')
55         context['episode'] = self.episode
56         context['soundfile'] = self.episode.main_sound
57         return tmpl.render(context)
58
59     def get_default_form_class(self):
60         from .forms import EpisodeCellForm
61         return EpisodeCellForm
62
63     def get_additional_label(self):
64         if self.episode:
65             return u'%s - %s' % (
66                         self.episode.emission.title,
67                         self.episode.title)
68         return ''
69
70
71 @register_cell_class
72 class EpisodeAutoSelectionCell(CellBase):
73     title = models.CharField(_('Title'), max_length=50, blank=True)
74     tags = TaggableManager(_('Tags'), blank=True)
75     category = models.ForeignKey('emissions.Category', null=True, blank=True)
76
77     class Meta:
78         verbose_name = _('Automatic Episode Selection')
79
80     def render(self, context):
81         tmpl = template.loader.get_template('panikombo/episode_auto_selection.html')
82         context['title'] = self.title
83
84         episodes_queryset = Episode.objects.select_related()
85         if self.category:
86             episodes_queryset = episodes_queryset.filter(emission__categories__in=[self.category.id])
87         if self.tags.count():
88             episodes_queryset = episodes_queryset.filter(tags__in=self.tags.all())
89
90         episodes_queryset = episodes_queryset.extra(select={
91                         'first_diffusion': 'emissions_diffusion.datetime',
92                         },
93                         select_params=(False, True),
94                         where=['''datetime = (SELECT MIN(datetime)
95                                                 FROM emissions_diffusion
96                                                WHERE episode_id = emissions_episode.id)'''],
97                         tables=['emissions_diffusion'],
98                     ).order_by('-first_diffusion').distinct()
99         context['episodes'] = episodes_queryset
100         return tmpl.render(context)
101
102     def get_default_form_class(self):
103         from .forms import EpisodeAutoSelectionCellForm
104         return EpisodeAutoSelectionCellForm
105
106     def get_additional_label(self):
107         if self.title:
108             return self.title
109         return ''
110
111 @register_cell_class
112 class NewsItemAutoSelectionCell(CellBase):
113     title = models.CharField(_('Title'), max_length=50, blank=True)
114     tags = TaggableManager(_('Tags'), blank=True)
115     future = models.BooleanField(_('Future Events Only'), default=True)
116
117     class Meta:
118         verbose_name = _('Automatic Newsitem Selection')
119
120     def render(self, context):
121         tmpl = template.loader.get_template('panikombo/newsitem_auto_selection.html')
122         context['title'] = self.title
123
124         newsitems_queryset = NewsItem.objects.select_related()
125         if self.tags.count():
126             newsitems_queryset = newsitems_queryset.filter(tags__in=self.tags.all())
127         if self.future:
128             newsitems_queryset = newsitems_queryset.filter(event_date__gte=date.today())
129
130         context['newsitems'] = newsitems_queryset
131         return tmpl.render(context)
132
133     def get_default_form_class(self):
134         from .forms import NewsItemAutoSelectionCellForm
135         return NewsItemAutoSelectionCellForm
136
137     def get_additional_label(self):
138         if self.title:
139             return self.title
140         return ''
141
142
143 def get_topik_image_path(instance, filename):
144     return os.path.join('images', 'topik', instance.page.slug,
145             os.path.basename(filename))
146
147 class Topik(models.Model):
148     page = models.ForeignKey('data.Page')
149     image = models.ImageField(_('Image'),
150             upload_to=get_topik_image_path, max_length=250, null=True, blank=True)
151
152     # denormalized from Focus
153     got_focus = models.DateTimeField(default=None, null=True, blank=True)
154     has_focus = models.BooleanField(default=False)