forked from rc/aircox
fix diff update/delete on schedule change; fix match algorithms
This commit is contained in:
@ -1,9 +1,12 @@
|
||||
from django.db.models.signals import post_save, pre_save, pre_delete, m2m_changed
|
||||
from django.contrib.auth.models import User, Group, Permission
|
||||
import pytz
|
||||
|
||||
from django.dispatch import receiver
|
||||
from django.utils.translation import ugettext as _, ugettext_lazy
|
||||
from django.contrib.auth.models import User, Group, Permission
|
||||
from django.contrib.contenttypes.models import ContentType
|
||||
from django.db.models import F
|
||||
from django.db.models.signals import post_save, pre_save, pre_delete, m2m_changed
|
||||
from django.dispatch import receiver
|
||||
from django.utils import timezone as tz
|
||||
from django.utils.translation import ugettext as _, ugettext_lazy
|
||||
|
||||
import aircox.models as models
|
||||
import aircox.utils as utils
|
||||
@ -36,9 +39,10 @@ def user_default_groups(sender, instance, created, *args, **kwargs):
|
||||
|
||||
# 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):
|
||||
# TODO: case instance.program has changed
|
||||
# TODO: case instance.program | instance.frequency has changed
|
||||
if not instance.program.sync:
|
||||
return
|
||||
|
||||
@ -46,29 +50,28 @@ def schedule_post_saved(sender, instance, created, *args, **kwargs):
|
||||
if not initial or not instance.changed(['date','duration', 'frequency']):
|
||||
return
|
||||
|
||||
if not initial.get('date') or not initial.get('duration') or not initial.get('frequency'):
|
||||
if not initial.get('date') or not initial.get('duration') \
|
||||
or not initial.get('frequency'):
|
||||
return
|
||||
|
||||
# old schedule and timedelta
|
||||
old_sched = models.Schedule(
|
||||
program = instance.program,
|
||||
date = initial['date'],
|
||||
duration = initial['duration'],
|
||||
frequency = initial['frequency'],
|
||||
)
|
||||
delta = instance.date - old_sched.date
|
||||
old = models.Schedule(**{ key: initial.get(key)
|
||||
for key in ('date','timezone','duration','frequency')
|
||||
})
|
||||
start_delta = instance.local_date - old.local_date
|
||||
|
||||
# update diffusions...
|
||||
qs = models.Diffusion.objects.after(
|
||||
old.date = old.local_date.astimezone(pytz.UTC)
|
||||
|
||||
qs = models.Diffusion.objects.station(
|
||||
instance.program.station,
|
||||
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()
|
||||
|
||||
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)
|
||||
)
|
||||
return
|
||||
|
||||
@receiver(pre_delete, sender=models.Schedule)
|
||||
def schedule_pre_delete(sender, instance, *args, **kwargs):
|
||||
@ -79,17 +82,14 @@ def schedule_pre_delete(sender, instance, *args, **kwargs):
|
||||
if not initial or not instance.changed(['date','duration', 'frequency']):
|
||||
return
|
||||
|
||||
old_sched = models.Schedule(
|
||||
date = initial['date'],
|
||||
duration = initial['duration'],
|
||||
frequency = initial['frequency'],
|
||||
old = models.Schedule(**{ key: initial.get(key)
|
||||
for key in ('date','timezone','duration','frequency')
|
||||
})
|
||||
|
||||
qs = models.Diffusion.objects.station(
|
||||
instance.program.station,
|
||||
)
|
||||
|
||||
qs = models.Diffusion.objects.after(instance.program.station).filter(
|
||||
program = instance.program
|
||||
)
|
||||
for diff in qs:
|
||||
if not old_sched.match(diff.date):
|
||||
continue
|
||||
diff.delete()
|
||||
pks = [ item.pk for item in qs if old.match(item.date) ]
|
||||
qs.filter(pk__in = pks).delete()
|
||||
|
||||
|
Reference in New Issue
Block a user