forked from rc/aircox
work on schedule gen bug -- split Schedule's date and time
This commit is contained in:
parent
9ad47b795a
commit
343279a777
|
@ -156,7 +156,7 @@ class ScheduleAdmin(admin.ModelAdmin):
|
||||||
program_name.short_description = _('Program')
|
program_name.short_description = _('Program')
|
||||||
|
|
||||||
def day(self, obj):
|
def day(self, obj):
|
||||||
return obj.date.strftime('%A')
|
return '' # obj.date.strftime('%A')
|
||||||
day.short_description = _('Day')
|
day.short_description = _('Day')
|
||||||
|
|
||||||
def rerun(self, obj):
|
def rerun(self, obj):
|
||||||
|
@ -165,8 +165,8 @@ class ScheduleAdmin(admin.ModelAdmin):
|
||||||
rerun.boolean = True
|
rerun.boolean = True
|
||||||
|
|
||||||
list_filter = ['frequency', 'program']
|
list_filter = ['frequency', 'program']
|
||||||
list_display = ['id', 'program_name', 'frequency', 'day', 'date', 'duration', 'rerun']
|
list_display = ['id', 'program_name', 'frequency', 'day', 'date', 'time', 'timezone', 'duration', 'rerun']
|
||||||
list_editable = ['frequency', 'date', 'duration']
|
list_editable = ['frequency', 'date', 'time', 'timezone', 'duration']
|
||||||
|
|
||||||
|
|
||||||
@admin.register(Track)
|
@admin.register(Track)
|
||||||
|
|
|
@ -1,18 +1,20 @@
|
||||||
import datetime
|
import datetime
|
||||||
import calendar
|
import logging
|
||||||
import os
|
import os
|
||||||
import shutil
|
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.db import models
|
||||||
from django.template.defaultfilters import slugify
|
from django.template.defaultfilters import slugify
|
||||||
from django.utils.translation import ugettext as _, ugettext_lazy
|
from django.utils.translation import ugettext as _, ugettext_lazy
|
||||||
from django.utils import timezone as tz
|
from django.utils import timezone as tz
|
||||||
from django.utils.html import strip_tags
|
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
|
from taggit.managers import TaggableManager
|
||||||
|
|
||||||
|
@ -482,12 +484,19 @@ class Schedule(models.Model):
|
||||||
Program,
|
Program,
|
||||||
verbose_name = _('related program'),
|
verbose_name = _('related program'),
|
||||||
)
|
)
|
||||||
date = models.DateTimeField(
|
time = models.TimeField(
|
||||||
|
_('time'),
|
||||||
|
blank = True, null = True,
|
||||||
|
help_text = _('start time'),
|
||||||
|
)
|
||||||
|
date = models.DateField(
|
||||||
_('date'),
|
_('date'),
|
||||||
help_text = _('date of the first diffusion')
|
blank = True, null = True,
|
||||||
|
help_text = _('date of the first diffusion'),
|
||||||
)
|
)
|
||||||
timezone = models.CharField(
|
timezone = models.CharField(
|
||||||
_('timezone'),
|
_('timezone'),
|
||||||
|
choices = [(x, x) for x in pytz.all_timezones],
|
||||||
max_length = 100, blank=True,
|
max_length = 100, blank=True,
|
||||||
help_text = _('timezone used for the date')
|
help_text = _('timezone used for the date')
|
||||||
)
|
)
|
||||||
|
@ -567,15 +576,15 @@ class Schedule(models.Model):
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def end(self):
|
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):
|
def match(self, date = None, check_time = True):
|
||||||
"""
|
"""
|
||||||
Return True if the given datetime matches the schedule
|
Return True if the given datetime matches the schedule
|
||||||
"""
|
"""
|
||||||
date = utils.date_or_default(date)
|
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 \
|
||||||
if self.date.weekday() != date.weekday() or not self.match_week(date):
|
not self.match_week(date):
|
||||||
return False
|
return False
|
||||||
|
|
||||||
if not check_time:
|
if not check_time:
|
||||||
|
@ -599,6 +608,7 @@ class Schedule(models.Model):
|
||||||
date = utils.date_or_default(date)
|
date = utils.date_or_default(date)
|
||||||
date += tz.timedelta(days = self.date.weekday() - date.weekday() )
|
date += tz.timedelta(days = self.date.weekday() - date.weekday() )
|
||||||
|
|
||||||
|
# FIXME this case
|
||||||
if self.frequency == Schedule.Frequency.one_on_two:
|
if self.frequency == Schedule.Frequency.one_on_two:
|
||||||
# cf notes in date_of_month
|
# cf notes in date_of_month
|
||||||
diff = utils.cast_date(date, False) - utils.cast_date(self.date, False)
|
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
|
Return a new datetime with schedule time. Timezone is handled
|
||||||
using `schedule.timezone`.
|
using `schedule.timezone`.
|
||||||
"""
|
"""
|
||||||
local_date = self.local_date
|
time = self.time or self.date
|
||||||
date = tz.datetime(date.year, date.month, date.day,
|
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.localize(date)
|
||||||
date = self.tz.normalize(date)
|
date = self.tz.normalize(date)
|
||||||
return date
|
return date
|
||||||
|
@ -633,8 +643,9 @@ class Schedule(models.Model):
|
||||||
if self.frequency == Schedule.Frequency.ponctual:
|
if self.frequency == Schedule.Frequency.ponctual:
|
||||||
return []
|
return []
|
||||||
|
|
||||||
# set to 12h in order to avoid potential bugs with dst
|
# first day of month
|
||||||
date = utils.date_or_default(date, True).replace(day=1, hour=12)
|
date = utils.date_or_default(date, to_datetime = False) \
|
||||||
|
.replace(day=1)
|
||||||
freq = self.frequency
|
freq = self.frequency
|
||||||
|
|
||||||
# last of the month
|
# last of the month
|
||||||
|
@ -676,7 +687,7 @@ class Schedule(models.Model):
|
||||||
week += 1;
|
week += 1;
|
||||||
return [self.normalize(date) for date in dates]
|
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
|
Return a list of Diffusion instances, from month of the given date, that
|
||||||
can be not in the database.
|
can be not in the database.
|
||||||
|
@ -721,7 +732,7 @@ class Schedule(models.Model):
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return ' | '.join([ '#' + str(self.id), self.program.name,
|
return ' | '.join([ '#' + str(self.id), self.program.name,
|
||||||
self.get_frequency_display(),
|
self.get_frequency_display(),
|
||||||
self.date.strftime('%a %H:%M') ])
|
self.time.strftime('%a %H:%M') ])
|
||||||
|
|
||||||
def save(self, *args, **kwargs):
|
def save(self, *args, **kwargs):
|
||||||
if self.initial:
|
if self.initial:
|
||||||
|
@ -729,8 +740,6 @@ class Schedule(models.Model):
|
||||||
self.duration = self.initial.duration
|
self.duration = self.initial.duration
|
||||||
if not self.frequency:
|
if not self.frequency:
|
||||||
self.frequency = self.initial.frequency
|
self.frequency = self.initial.frequency
|
||||||
|
|
||||||
self.timezone = self.date.tzinfo.zone
|
|
||||||
super().save(*args, **kwargs)
|
super().save(*args, **kwargs)
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
|
|
|
@ -44,6 +44,7 @@ def user_default_groups(sender, instance, created, *args, **kwargs):
|
||||||
|
|
||||||
@receiver(post_save, sender=models.Schedule)
|
@receiver(post_save, sender=models.Schedule)
|
||||||
def schedule_post_saved(sender, instance, created, *args, **kwargs):
|
def schedule_post_saved(sender, instance, created, *args, **kwargs):
|
||||||
|
return
|
||||||
# TODO: case instance.program | instance.frequency has changed
|
# TODO: case instance.program | instance.frequency has changed
|
||||||
if not instance.program.sync:
|
if not instance.program.sync:
|
||||||
return
|
return
|
||||||
|
@ -53,16 +54,18 @@ def schedule_post_saved(sender, instance, created, *args, **kwargs):
|
||||||
return
|
return
|
||||||
|
|
||||||
if not initial.get('date') or not initial.get('duration') \
|
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
|
return
|
||||||
|
|
||||||
# old schedule and timedelta
|
# old schedule and timedelta
|
||||||
old = models.Schedule(**{ key: initial.get(key)
|
old = models.Schedule(**{ key: initial.get(key)
|
||||||
for key in ('date','timezone','duration','frequency')
|
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(
|
qs = models.Diffusion.objects.station(
|
||||||
instance.program.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) ]
|
pks = [ item.pk for item in qs if old.match(item.date) ]
|
||||||
qs.filter(pk__in = pks).update(
|
qs.filter(pk__in = pks).update(
|
||||||
start = F('start') + start_delta,
|
start = F('start') + delta,
|
||||||
end = F('start') + start_delta + utils.to_timedelta(instance.duration)
|
end = F('start') + delta + utils.to_timedelta(instance.duration)
|
||||||
)
|
)
|
||||||
return
|
return
|
||||||
|
|
||||||
|
|
|
@ -4,7 +4,6 @@
|
||||||
# This file is distributed under the same license as the Aircox package.
|
# This file is distributed under the same license as the Aircox package.
|
||||||
# Aarys, 2016.
|
# Aarys, 2016.
|
||||||
#
|
#
|
||||||
#, fuzzy
|
|
||||||
msgid ""
|
msgid ""
|
||||||
msgstr ""
|
msgstr ""
|
||||||
"Project-Id-Version: Aircox 0.1\n"
|
"Project-Id-Version: Aircox 0.1\n"
|
||||||
|
|
Loading…
Reference in New Issue
Block a user