issue #1: synchronise programs' schedules and later diffusions (update, delete); fix signal import in apps

This commit is contained in:
bkfox
2016-10-13 01:22:14 +02:00
parent bdd351d6d9
commit 3ab373097b
10 changed files with 134 additions and 31 deletions

View File

@ -0,0 +1,3 @@
default_app_config = 'aircox.apps.AircoxConfig'

View File

@ -81,9 +81,8 @@ class ProgramAdmin(NameableAdmin):
schedule.boolean = True
schedule.short_description = _("Schedule")
list_display = ('id', 'name', 'active', 'schedule')
fields = NameableAdmin.fields + [ 'active', 'station' ]
# TODO list_display
list_display = ('id', 'name', 'active', 'schedule', 'sync')
fields = NameableAdmin.fields + [ 'active', 'station','sync' ]
inlines = [ ScheduleInline, StreamInline ]
# SO#8074161

10
aircox/apps.py Normal file
View File

@ -0,0 +1,10 @@
from django.apps import AppConfig
class AircoxConfig(AppConfig):
name = 'aircox'
verbose_name = 'Aircox'
def ready(self):
import aircox.signals

View File

@ -128,16 +128,18 @@ class Command (BaseCommand):
'--update', action='store_true',
help='generate (unconfirmed) diffusions for the given month. '
'These diffusions must be confirmed manually by changing '
'their type to "normal"')
'their type to "normal"'
)
group.add_argument(
'--clean', action='store_true',
help='remove unconfirmed diffusions older than the given month')
help='remove unconfirmed diffusions older than the given month'
)
group.add_argument(
'--check', action='store_true',
help='check future unconfirmed diffusions from the given date '
'agains\'t schedules and remove it if that do not match any '
'schedule')
help='check unconfirmed later diffusions from the given '
'date again'\'t schedule. If no schedule is found, remove '
'it.'
)
group = parser.add_argument_group('date')
group.add_argument(
@ -161,7 +163,6 @@ class Command (BaseCommand):
'diffusions except those that conflicts with others'
)
def handle (self, *args, **options):
date = tz.datetime(year = options.get('year'),
month = options.get('month'),

View File

@ -294,6 +294,11 @@ class Program(Nameable):
default = True,
help_text = _('if not checked this program is no longer active')
)
sync = models.BooleanField(
_('syncronise'),
default = True,
help_text = _('update later diffusions according to schedule changes')
)
@property
def path(self):
@ -503,6 +508,24 @@ class Schedule(models.Model):
help_text = 'this schedule is a rerun of this one',
)
# initial cached data
__initial = None
def changed(self, fields = ['date','duration','frequency']):
initial = self._Schedule__initial
if not initial:
return
before, now = self.__initial, self.__dict__
before, now = {
f: getattr(before, f) for f in fields
if hasattr(before, f)
}, {
f: getattr(now, f) for f in fields
if hasattr(now, f)
}
return before == now
@property
def end(self):
return self.date + utils.to_timedelta(self.duration)
@ -627,6 +650,10 @@ class Schedule(models.Model):
]
return diffusions
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.__initial = self.__dict__.copy()
def __str__(self):
return ' | '.join([ '#' + str(self.id), self.program.name,
self.get_frequency_display(),

63
aircox/signals.py Normal file
View File

@ -0,0 +1,63 @@
from django.db.models.signals import post_save, pre_delete
from django.dispatch import receiver
from django.utils.translation import ugettext as _, ugettext_lazy
from django.contrib.contenttypes.models import ContentType
import aircox.models as models
import aircox.utils as utils
# FIXME: avoid copy of the code in schedule_post_saved and
# schedule_pre_delete
@receiver(post_save, sender=models.Schedule)
def schedule_post_saved(sender, instance, created, *args, **kwargs):
if not instance.program.sync:
return
initial = instance._Schedule__initial
if not initial or not instance.changed(['date','duration', 'frequency']):
return
# old schedule and timedelta
old_sched = models.Schedule(
date = initial['date'],
duration = initial['duration'],
frequency = initial['frequency'],
)
delta = instance.date - old_sched.date
# update diffusions...
qs = models.Diffusion.objects.get_after().filter(
program = instance.program
)
for diff in qs:
if not old_sched.match(diff.date):
continue
diff.start += delta
diff.end = diff.start + utils.to_timedelta(instance.duration)
diff.save()
@receiver(pre_delete, sender=models.Schedule)
def schedule_pre_delete(sender, instance, *args, **kwargs):
if not instance.program.sync:
return
initial = instance._Schedule__initial
if not initial or not instance.changed(['date','duration', 'frequency']):
return
old_sched = models.Schedule(
date = initial['date'],
duration = initial['duration'],
frequency = initial['frequency'],
)
qs = models.Diffusion.objects.get_after().filter(
program = instance.program
)
for diff in qs:
if not old_sched.match(diff.date):
continue
diff.delete()