From 343279a777f55d43b13c9de3f449e459e0140a10 Mon Sep 17 00:00:00 2001 From: bkfox Date: Fri, 15 Dec 2017 23:32:39 +0100 Subject: [PATCH] work on schedule gen bug -- split Schedule's date and time --- aircox/admin.py | 6 +-- aircox/management/commands/diffusions.py | 4 +- aircox/models.py | 47 +++++++++++++--------- aircox/signals.py | 13 +++--- aircox_cms/locale/fr/LC_MESSAGES/django.po | 1 - 5 files changed, 41 insertions(+), 30 deletions(-) diff --git a/aircox/admin.py b/aircox/admin.py index 004bdbf..7198724 100755 --- a/aircox/admin.py +++ b/aircox/admin.py @@ -156,7 +156,7 @@ class ScheduleAdmin(admin.ModelAdmin): program_name.short_description = _('Program') def day(self, obj): - return obj.date.strftime('%A') + return '' # obj.date.strftime('%A') day.short_description = _('Day') def rerun(self, obj): @@ -165,8 +165,8 @@ class ScheduleAdmin(admin.ModelAdmin): rerun.boolean = True list_filter = ['frequency', 'program'] - list_display = ['id', 'program_name', 'frequency', 'day', 'date', 'duration', 'rerun'] - list_editable = ['frequency', 'date', 'duration'] + list_display = ['id', 'program_name', 'frequency', 'day', 'date', 'time', 'timezone', 'duration', 'rerun'] + list_editable = ['frequency', 'date', 'time', 'timezone', 'duration'] @admin.register(Track) diff --git a/aircox/management/commands/diffusions.py b/aircox/management/commands/diffusions.py index e614e91..d52f3fc 100755 --- a/aircox/management/commands/diffusions.py +++ b/aircox/management/commands/diffusions.py @@ -66,7 +66,7 @@ class Actions: qs.delete() @staticmethod - def check (date): + def check(date): qs = Diffusion.objects.filter(type = Diffusion.Type.unconfirmed, start__gt = date) items = [] @@ -83,7 +83,7 @@ class Actions: Diffusion.objects.filter(id__in = items).delete() -class Command (BaseCommand): +class Command(BaseCommand): help= __doc__ def add_arguments (self, parser): diff --git a/aircox/models.py b/aircox/models.py index 7c1221b..a8613ba 100755 --- a/aircox/models.py +++ b/aircox/models.py @@ -1,18 +1,20 @@ import datetime -import calendar +import logging import os import shutil -import logging -from enum import IntEnum +import calendar +from enum import IntEnum +import pytz + +from django.conf import settings as main_settings +from django.contrib.contenttypes.fields import GenericForeignKey, GenericRelation +from django.contrib.contenttypes.models import ContentType from django.db import models from django.template.defaultfilters import slugify from django.utils.translation import ugettext as _, ugettext_lazy from django.utils import timezone as tz from django.utils.html import strip_tags -from django.contrib.contenttypes.fields import GenericForeignKey, GenericRelation -from django.contrib.contenttypes.models import ContentType -from django.conf import settings as main_settings from taggit.managers import TaggableManager @@ -482,12 +484,19 @@ class Schedule(models.Model): Program, verbose_name = _('related program'), ) - date = models.DateTimeField( + time = models.TimeField( + _('time'), + blank = True, null = True, + help_text = _('start time'), + ) + date = models.DateField( _('date'), - help_text = _('date of the first diffusion') + blank = True, null = True, + help_text = _('date of the first diffusion'), ) timezone = models.CharField( _('timezone'), + choices = [(x, x) for x in pytz.all_timezones], max_length = 100, blank=True, help_text = _('timezone used for the date') ) @@ -567,15 +576,15 @@ class Schedule(models.Model): @property def end(self): - return self.date + utils.to_timedelta(self.duration) + return self.time + utils.to_timedelta(self.duration) def match(self, date = None, check_time = True): """ Return True if the given datetime matches the schedule """ date = utils.date_or_default(date) - print('match...', date, self.date, self.match_week(date), self.date.weekday() == date.weekday()) - if self.date.weekday() != date.weekday() or not self.match_week(date): + if self.date.weekday() != date.weekday() or \ + not self.match_week(date): return False if not check_time: @@ -599,6 +608,7 @@ class Schedule(models.Model): date = utils.date_or_default(date) date += tz.timedelta(days = self.date.weekday() - date.weekday() ) + # FIXME this case if self.frequency == Schedule.Frequency.one_on_two: # cf notes in date_of_month diff = utils.cast_date(date, False) - utils.cast_date(self.date, False) @@ -618,9 +628,9 @@ class Schedule(models.Model): Return a new datetime with schedule time. Timezone is handled using `schedule.timezone`. """ - local_date = self.local_date + time = self.time or self.date date = tz.datetime(date.year, date.month, date.day, - local_date.hour, local_date.minute, 0, 0) + time.hour, time.minute, 0, 0) date = self.tz.localize(date) date = self.tz.normalize(date) return date @@ -633,8 +643,9 @@ class Schedule(models.Model): if self.frequency == Schedule.Frequency.ponctual: return [] - # set to 12h in order to avoid potential bugs with dst - date = utils.date_or_default(date, True).replace(day=1, hour=12) + # first day of month + date = utils.date_or_default(date, to_datetime = False) \ + .replace(day=1) freq = self.frequency # last of the month @@ -676,7 +687,7 @@ class Schedule(models.Model): week += 1; return [self.normalize(date) for date in dates] - def diffusions_of_month(self, date, exclude_saved = False): + def diffusions_of_month(self, date = None, exclude_saved = False): """ Return a list of Diffusion instances, from month of the given date, that can be not in the database. @@ -721,7 +732,7 @@ class Schedule(models.Model): def __str__(self): return ' | '.join([ '#' + str(self.id), self.program.name, self.get_frequency_display(), - self.date.strftime('%a %H:%M') ]) + self.time.strftime('%a %H:%M') ]) def save(self, *args, **kwargs): if self.initial: @@ -729,8 +740,6 @@ class Schedule(models.Model): self.duration = self.initial.duration if not self.frequency: self.frequency = self.initial.frequency - - self.timezone = self.date.tzinfo.zone super().save(*args, **kwargs) class Meta: diff --git a/aircox/signals.py b/aircox/signals.py index 093ed6a..b480d14 100755 --- a/aircox/signals.py +++ b/aircox/signals.py @@ -44,6 +44,7 @@ def user_default_groups(sender, instance, created, *args, **kwargs): @receiver(post_save, sender=models.Schedule) def schedule_post_saved(sender, instance, created, *args, **kwargs): + return # TODO: case instance.program | instance.frequency has changed if not instance.program.sync: return @@ -53,16 +54,18 @@ def schedule_post_saved(sender, instance, created, *args, **kwargs): return if not initial.get('date') or not initial.get('duration') \ - or not initial.get('frequency'): + or not initial.get('frequency') or \ + initial.frequency != instance.frequency: return # old schedule and timedelta old = models.Schedule(**{ key: initial.get(key) for key in ('date','timezone','duration','frequency') }) - start_delta = instance.local_date - old.local_date - old.date = old.local_date.astimezone(pytz.UTC) + # change: day, time, duration, frequence => TODO + delta = (instance.date - old.date) + \ + (instance.time - old.time) qs = models.Diffusion.objects.station( instance.program.station, @@ -70,8 +73,8 @@ def schedule_post_saved(sender, instance, created, *args, **kwargs): pks = [ item.pk for item in qs if old.match(item.date) ] qs.filter(pk__in = pks).update( - start = F('start') + start_delta, - end = F('start') + start_delta + utils.to_timedelta(instance.duration) + start = F('start') + delta, + end = F('start') + delta + utils.to_timedelta(instance.duration) ) return diff --git a/aircox_cms/locale/fr/LC_MESSAGES/django.po b/aircox_cms/locale/fr/LC_MESSAGES/django.po index 4787e6e..4f6fe7d 100755 --- a/aircox_cms/locale/fr/LC_MESSAGES/django.po +++ b/aircox_cms/locale/fr/LC_MESSAGES/django.po @@ -4,7 +4,6 @@ # This file is distributed under the same license as the Aircox package. # Aarys, 2016. # -#, fuzzy msgid "" msgstr "" "Project-Id-Version: Aircox 0.1\n"