From 1471381dfe74411934ec51f88bb5c4e7e47f2c36 Mon Sep 17 00:00:00 2001
From: Ernesto Rico-Schmidt <e.rico.schmidt@gmail.com>
Date: Wed, 6 Jan 2016 20:02:27 +0100
Subject: added new model fields and updated admin


diff --git a/program/admin.py b/program/admin.py
index 3086dd5..8a9799c 100644
--- a/program/admin.py
+++ b/program/admin.py
@@ -28,6 +28,11 @@ class ShowTopicAdmin(admin.ModelAdmin):
     prepopulated_fields = {'slug': ('topic',)}
 
 
+class HostAdmin(admin.ModelAdmin):
+    list_display = ('name',)
+    list_filter = ('always_visible', 'is_active')
+
+
 class NoteAdmin(admin.ModelAdmin):
     date_hierarchy = 'start'
     list_display = ('title', 'show', 'start', 'status')
@@ -58,8 +63,8 @@ class TimeSlotInline(admin.TabularInline):
 class ProgramSlotAdmin(admin.ModelAdmin):
     actions = ('renew',)
     inlines = (TimeSlotInline,)
-    list_display = ('show', 'byweekday', 'rrule', 'tstart', 'tend', 'until', 'timeslot_count')
-    list_filter = ('byweekday', 'rrule', 'is_repetition')
+    list_display = ('show', 'byweekday', 'rrule', 'tstart', 'tend', 'until')
+    list_filter = ('byweekday', 'rrule', 'is_repetition', 'is_active')
     ordering = ('byweekday', 'dstart')
     save_on_top = True
     search_fields = ('show__name',)
@@ -83,8 +88,8 @@ class ProgramSlotInline(admin.TabularInline):
 class ShowAdmin(admin.ModelAdmin):
     filter_horizontal = ('hosts', 'owners', 'musicfocus', 'showinformation', 'showtopic')
     inlines = (ProgramSlotInline,)
-    list_display = ('name', 'short_description', 'broadcastformat', 'has_active_programslots')
-    list_filter = ('broadcastformat', 'showinformation', 'showtopic', 'musicfocus')
+    list_display = ('name', 'short_description', 'broadcastformat')
+    list_filter = ('broadcastformat', 'showinformation', 'showtopic', 'musicfocus', 'is_active')
     ordering = ('slug',)
     prepopulated_fields = {'slug': ('name',)}
     search_fields = ('name', 'short_description', 'description')
@@ -94,13 +99,11 @@ class ShowAdmin(admin.ModelAdmin):
         'musicfocus',
     )
 
-
 admin.site.register(BroadcastFormat, BroadcastFormatAdmin)
 admin.site.register(MusicFocus, MusicFocusAdmin)
 admin.site.register(ShowInformation, ShowInformationAdmin)
 admin.site.register(ShowTopic, ShowTopicAdmin)
+admin.site.register(Host, HostAdmin)
 admin.site.register(Note, NoteAdmin)
 admin.site.register(ProgramSlot, ProgramSlotAdmin)
 admin.site.register(Show, ShowAdmin)
-
-admin.site.register(Host)
diff --git a/program/models.py b/program/models.py
index 8013b84..11ec7f1 100644
--- a/program/models.py
+++ b/program/models.py
@@ -211,6 +211,7 @@ class MusicFocus(models.Model):
 class Host(models.Model):
     name = models.CharField(_("Name"), max_length=128)
     always_visible = models.BooleanField(_("Always visible"), default=False)
+    is_active = models.BooleanField(_("Is active"), default=True, editable=False)
     email = models.EmailField(_("E-Mail"), blank=True)
     website = models.URLField(_("Website"), blank=True)
 
@@ -223,7 +224,7 @@ class Host(models.Model):
         return u'%s' % self.name
 
     def get_absolute_url(self):
-        return reverse('host-detail', args=[self.id])
+        return reverse('host-detail', args=[str(self.id)])
 
 
 class Show(models.Model):
@@ -242,6 +243,7 @@ class Show(models.Model):
     description = tinymce_models.HTMLField(_("Description"), blank=True, null=True)
     email = models.EmailField(_("E-Mail"), blank=True, null=True)
     website = models.URLField(_("Website"), blank=True, null=True)
+    is_active = models.BooleanField(_("Is active"), default=True, editable=False)
     cba_series_id = models.IntegerField(_("CBA series ID"), blank=True, null=True)
     automation_id = models.IntegerField(_("Automation ID"), blank=True, null=True, choices=get_automation_id_choices())
     created = models.DateTimeField(auto_now_add=True, editable=False)
@@ -258,12 +260,6 @@ class Show(models.Model):
     def get_absolute_url(self):
         return reverse('show-detail', args=[self.slug])
 
-    def has_active_programslots(self):
-        return self.programslots.filter(until__gt=date.today()).count() > 0
-
-    has_active_programslots.boolean = True
-    has_active_programslots.short_description = _("Has active program slots")
-
 
 class RRule(models.Model):
     FREQ_CHOICES = (
@@ -312,6 +308,7 @@ class ProgramSlot(models.Model):
     tstart = models.TimeField(_("Start time"))
     tend = models.TimeField(_("End time"))
     until = models.DateField(_("Last date"))
+    is_active = models.BooleanField(_("Is active"), default=True, editable=False)
     is_repetition = models.BooleanField(_("Is repetition"), default=False)
     automation_id = models.IntegerField(_("Automation ID"), blank=True, null=True, choices=get_automation_id_choices())
     created = models.DateTimeField(auto_now_add=True, editable=False)
@@ -350,6 +347,9 @@ class ProgramSlot(models.Model):
         else:
             old = False
 
+        self.is_active = self.until > date.today()
+        self.show.is_active = self.until > date.today()
+
         super(ProgramSlot, self).save(*args, **kwargs)
 
         if self.rrule.freq == 0:
@@ -389,29 +389,16 @@ class ProgramSlot(models.Model):
 
         if not old:
             for k in range(min(len(starts), len(ends))):
-                timeslot = TimeSlot.objects.create(programslot=self, start=starts[k], end=ends[k])
+                TimeSlot.objects.create(programslot=self, start=starts[k], end=ends[k])
         elif self.until > old.until:
             for k in range(min(len(starts), len(ends))):
                 if starts[k].date() > old.until:
-                    timeslot = TimeSlot.objects.create(programslot=self, start=starts[k], end=ends[k])
-
-    def timeslot_count(self):
-        return self.timeslots.count()
-
-    timeslot_count.description = _("Time slot count")
-
-    def has_active_timeslot(self):
-        if self.timeslots.count() > 0:
-            start = self.timeslots.all().order_by("start")[0].start
-            end = self.timeslots.all().order_by("-end")[0].end
-            now = datetime.now()
-            return (start < now and end > now)
-        else:
-            return False
+                    TimeSlot.objects.create(programslot=self, start=starts[k], end=ends[k])
 
 
 class TimeSlotManager(models.Manager):
-    def get_or_create_current(self):
+    @staticmethod
+    def get_or_create_current():
         try:
             return TimeSlot.objects.get(start__lte=datetime.now(), end__gt=datetime.now())
         except MultipleObjectsReturned:
@@ -421,13 +408,19 @@ class TimeSlotManager(models.Manager):
             today = date.today().weekday()
             default = Show.objects.get(pk=1)
 
-            previous = TimeSlot.objects.filter(end__lte=datetime.now()).order_by('-start')[0]
-            next = TimeSlot.objects.filter(start__gte=datetime.now())[0]
+            previous_timeslot = TimeSlot.objects.filter(end__lte=datetime.now()).order_by('-start')[0]
+            next_timeslot = TimeSlot.objects.filter(start__gte=datetime.now())[0]
 
-            dstart, tstart = previous.end.date(), previous.end.time()
-            until, tend = next.start.date(), next.start.time()
+            dstart, tstart = previous_timeslot.end.date(), previous_timeslot.end.time()
+            until, tend = next_timeslot.start.date(), next_timeslot.start.time()
 
-            new_programslot = ProgramSlot(rrule=once, byweekday=today, show=default, dstart=dstart, tstart=tstart, tend=tend, until=until)
+            new_programslot = ProgramSlot(rrule=once,
+                                          byweekday=today,
+                                          show=default,
+                                          dstart=dstart,
+                                          tstart=tstart,
+                                          tend=tend,
+                                          until=until)
 
             try:
                 new_programslot.validate_unique()
@@ -437,14 +430,16 @@ class TimeSlotManager(models.Manager):
             else:
                 return new_programslot.timeslots.all()[0]
 
-    def get_day_timeslots(self, day):
+    @staticmethod
+    def get_day_timeslots(day):
         today = datetime.combine(day, time(6, 0))
         tomorrow = today + timedelta(days=1)
 
         return TimeSlot.objects.filter(Q(start__lte=today, end__gte=today) |
                                        Q(start__gt=today, start__lt=tomorrow)).exclude(end=today)
 
-    def get_24h_timeslots(self, start):
+    @staticmethod
+    def get_24h_timeslots(start):
         end = start + timedelta(hours=24)
 
         return TimeSlot.objects.filter(Q(start__lte=start, end__gte=start) |
@@ -475,7 +470,7 @@ class TimeSlot(models.Model):
         super(TimeSlot, self).save(*args, **kwargs)
 
     def get_absolute_url(self):
-        return reverse('timeslot-detail', args=[self.id])
+        return reverse('timeslot-detail', args=[str(self.id)])
 
 
 class Note(models.Model):
-- 
cgit v0.10.2