replace diffusion.duration by diffusion.end

This commit is contained in:
bkfox 2016-01-04 21:18:59 +01:00
parent e75195e98c
commit 314e59de79
4 changed files with 42 additions and 62 deletions

View File

@ -101,7 +101,7 @@ class Monitor:
def expected_diffusion (station, date, on_air): def expected_diffusion (station, date, on_air):
""" """
Return which diffusion should be played now and is not playing Return which diffusion should be played now and is not playing
on the given station on the given station.
""" """
r = [ programs.Diffusion.get_prev(station, date), r = [ programs.Diffusion.get_prev(station, date),
programs.Diffusion.get_next(station, date) ] programs.Diffusion.get_next(station, date) ]
@ -109,9 +109,7 @@ class Monitor:
for diffusion in r if diffusion.count() ] for diffusion in r if diffusion.count() ]
for diffusion in r: for diffusion in r:
duration = to_timedelta(diffusion.archives_duration()) if diffusion.end < date:
end_at = diffusion.date + duration
if end_at < date:
continue continue
diffusion.playlist = [ sound.path diffusion.playlist = [ sound.path

View File

@ -95,17 +95,17 @@ class DiffusionAdmin (admin.ModelAdmin):
return ', '.join([ str(d) for d in obj.get_conflicts()]) return ', '.join([ str(d) for d in obj.get_conflicts()])
return '' return ''
list_display = ('id', 'type', 'date', 'program', 'initial', 'archives', 'conflicts') list_display = ('id', 'type', 'start', 'end', 'program', 'initial', 'archives', 'conflicts')
list_filter = ('type', 'date', 'program') list_filter = ('type', 'start', 'program')
list_editable = ('type', 'date') list_editable = ('type', 'start', 'end')
fields = ['type', 'date', 'duration', 'initial', 'program', 'sounds'] fields = ['type', 'start', 'end', 'initial', 'program', 'sounds']
def get_form(self, request, obj=None, **kwargs): def get_form(self, request, obj=None, **kwargs):
if request.user.has_perm('aircox_program.programming'): if request.user.has_perm('aircox_program.programming'):
self.readonly_fields = [] self.readonly_fields = []
else: else:
self.readonly_fields = ['program', 'date', 'duration'] self.readonly_fields = ['program', 'start', 'end']
if obj and obj.initial: if obj and obj.initial:
self.readonly_fields += ['program', 'sounds'] self.readonly_fields += ['program', 'sounds']

View File

@ -94,19 +94,19 @@ class Actions:
@staticmethod @staticmethod
def clean (date): def clean (date):
qs = Diffusion.objects.filter(type = Diffusion.Type['unconfirmed'], qs = Diffusion.objects.filter(type = Diffusion.Type['unconfirmed'],
date__lt = date) start__lt = date)
logger.info('[clean] %d diffusions will be removed', qs.count()) logger.info('[clean] %d diffusions will be removed', qs.count())
qs.delete() qs.delete()
@staticmethod @staticmethod
def check (date): def check (date):
qs = Diffusion.objects.filter(type = Diffusion.Type['unconfirmed'], qs = Diffusion.objects.filter(type = Diffusion.Type['unconfirmed'],
date__gt = date) start__gt = date)
items = [] items = []
for diffusion in qs: for diffusion in qs:
schedules = Schedule.objects.filter(program = diffusion.program) schedules = Schedule.objects.filter(program = diffusion.program)
for schedule in schedules: for schedule in schedules:
if schedule.match(diffusion.date): if schedule.match(diffusion.start):
break break
else: else:
items.append(diffusion.id) items.append(diffusion.id)
@ -152,9 +152,9 @@ class Command (BaseCommand):
' (if next month from today' ' (if next month from today'
) )
group = parser.add_argument_group('mode') group = parser.add_argument_group('options')
group.add_argument( group.add_argument(
'--approval', type=str, choices=['manual', 'auto'], '--mode', type=str, choices=['manual', 'auto'],
default='auto', default='auto',
help='manual means that all generated diffusions are unconfirmed, ' help='manual means that all generated diffusions are unconfirmed, '
'thus must be approved manually; auto confirmes all ' 'thus must be approved manually; auto confirmes all '

View File

@ -368,14 +368,16 @@ class Schedule (models.Model):
If exclude_saved, exclude all diffusions that are yet in the database. If exclude_saved, exclude all diffusions that are yet in the database.
""" """
dates = self.dates_of_month(date) dates = self.dates_of_month(date)
saved = Diffusion.objects.filter(date__in = dates, saved = Diffusion.objects.filter(start__in = dates,
program = self.program) program = self.program)
diffusions = [] diffusions = []
duration = utils.to_timedelta(self.duration)
# existing diffusions # existing diffusions
for item in saved: for item in saved:
if item.date in dates: if item.start in dates:
dates.remove(item.date) dates.remove(item.start)
if not exclude_saved: if not exclude_saved:
diffusions.append(item) diffusions.append(item)
@ -385,7 +387,7 @@ class Schedule (models.Model):
if self.initial: if self.initial:
first_date -= self.date - self.initial.date first_date -= self.date - self.initial.date
first_diffusion = Diffusion.objects.filter(date = first_date, first_diffusion = Diffusion.objects.filter(start = first_date,
program = self.program) program = self.program)
first_diffusion = first_diffusion[0] if first_diffusion.count() \ first_diffusion = first_diffusion[0] if first_diffusion.count() \
else None else None
@ -393,8 +395,8 @@ class Schedule (models.Model):
program = self.program, program = self.program,
type = Diffusion.Type['unconfirmed'], type = Diffusion.Type['unconfirmed'],
initial = first_diffusion if self.initial else None, initial = first_diffusion if self.initial else None,
date = date, start = date,
duration = self.duration, end = date + duration,
)) ))
return diffusions return diffusions
@ -581,11 +583,16 @@ class Diffusion (models.Model):
blank = True, null = True, blank = True, null = True,
help_text = _('the diffusion is a rerun of this one') help_text = _('the diffusion is a rerun of this one')
) )
date = models.DateTimeField( _('start of the diffusion') ) start = models.DateTimeField( _('start of the diffusion') )
duration = models.TimeField( end = models.DateTimeField( _('end of the diffusion') )
_('duration'),
help_text = _('regular duration'), @property
) def duration (self):
return self.end - self.start
@property
def date (self):
return self.start
def archives_duration (self): def archives_duration (self):
""" """
@ -596,7 +603,7 @@ class Diffusion (models.Model):
r = [ sound.duration r = [ sound.duration
for sound in sounds.filter(type = Sound.Type['archive']) for sound in sounds.filter(type = Sound.Type['archive'])
if sound.duration ] if sound.duration ]
return utils.time_sum(r) if r else self.duration return utils.time_sum(r)
def get_archives (self): def get_archives (self):
""" """
@ -613,10 +620,10 @@ class Diffusion (models.Model):
Return a queryset with the upcoming diffusions, ordered by Return a queryset with the upcoming diffusions, ordered by
+date +date
""" """
filter_args['date__gte'] = date_or_default(date) filter_args['start__gte'] = date_or_default(date)
if station: if station:
filter_args['program__station'] = station filter_args['program__station'] = station
return cl.objects.filter(**filter_args).order_by('date') return cl.objects.filter(**filter_args).order_by('start')
@classmethod @classmethod
def get_prev (cl, station = None, date = None, **filter_args): def get_prev (cl, station = None, date = None, **filter_args):
@ -624,46 +631,21 @@ class Diffusion (models.Model):
Return a queryset with the previous diffusion, ordered by Return a queryset with the previous diffusion, ordered by
-date -date
""" """
filter_args['date__lte'] = date_or_default(date) filter_args['start__lte'] = date_or_default(date)
if station: if station:
filter_args['program__station'] = station filter_args['program__station'] = station
return cl.objects.filter(**filter_args).order_by('-date') return cl.objects.filter(**filter_args).order_by('-start')
def get_conflicts (self): def get_conflicts (self):
""" """
Return a list of conflictual diffusions, based on the scheduled duration. Return a list of conflictual diffusions, based on the scheduled duration.
Note: for performance reason, check next and prev are limited to a
certain amount of diffusions.
""" """
r = [] r = Diffusion.objects.filter(
# prev models.Q(start__lte = self.start,
qs = self.get_prev(self.program.station, self.date) end__gte = self.start) |
count = 0 models.Q(start__gte = self.start,
for diff in qs: start__lte = self.end)
if diff.pk == self.pk: )
continue
end = diff.date + utils.to_timedelta(diff.duration)
if end > self.date:
r.append(diff)
continue
count+=1
if count > 5: break
# next
end = self.date + utils.to_timedelta(self.duration)
qs = self.get_next(self.program.station, self.date)
count = 0
for diff in qs:
if diff.pk == self.pk:
continue
if diff.date < end and diff not in r:
r.append(diff)
continue
count+=1
if count > 5: break
return r return r
def save (self, *args, **kwargs): def save (self, *args, **kwargs):