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):
"""
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),
programs.Diffusion.get_next(station, date) ]
@ -109,13 +109,11 @@ class Monitor:
for diffusion in r if diffusion.count() ]
for diffusion in r:
duration = to_timedelta(diffusion.archives_duration())
end_at = diffusion.date + duration
if end_at < date:
if diffusion.end < date:
continue
diffusion.playlist = [ sound.path
for sound in diffusion.get_archives() ]
for sound in diffusion.get_archives() ]
if diffusion.playlist and on_air not in diffusion.playlist:
return diffusion

View File

@ -95,17 +95,17 @@ class DiffusionAdmin (admin.ModelAdmin):
return ', '.join([ str(d) for d in obj.get_conflicts()])
return ''
list_display = ('id', 'type', 'date', 'program', 'initial', 'archives', 'conflicts')
list_filter = ('type', 'date', 'program')
list_editable = ('type', 'date')
list_display = ('id', 'type', 'start', 'end', 'program', 'initial', 'archives', 'conflicts')
list_filter = ('type', 'start', 'program')
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):
if request.user.has_perm('aircox_program.programming'):
self.readonly_fields = []
else:
self.readonly_fields = ['program', 'date', 'duration']
self.readonly_fields = ['program', 'start', 'end']
if obj and obj.initial:
self.readonly_fields += ['program', 'sounds']

View File

@ -94,19 +94,19 @@ class Actions:
@staticmethod
def clean (date):
qs = Diffusion.objects.filter(type = Diffusion.Type['unconfirmed'],
date__lt = date)
start__lt = date)
logger.info('[clean] %d diffusions will be removed', qs.count())
qs.delete()
@staticmethod
def check (date):
qs = Diffusion.objects.filter(type = Diffusion.Type['unconfirmed'],
date__gt = date)
start__gt = date)
items = []
for diffusion in qs:
schedules = Schedule.objects.filter(program = diffusion.program)
for schedule in schedules:
if schedule.match(diffusion.date):
if schedule.match(diffusion.start):
break
else:
items.append(diffusion.id)
@ -152,9 +152,9 @@ class Command (BaseCommand):
' (if next month from today'
)
group = parser.add_argument_group('mode')
group = parser.add_argument_group('options')
group.add_argument(
'--approval', type=str, choices=['manual', 'auto'],
'--mode', type=str, choices=['manual', 'auto'],
default='auto',
help='manual means that all generated diffusions are unconfirmed, '
'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.
"""
dates = self.dates_of_month(date)
saved = Diffusion.objects.filter(date__in = dates,
saved = Diffusion.objects.filter(start__in = dates,
program = self.program)
diffusions = []
duration = utils.to_timedelta(self.duration)
# existing diffusions
for item in saved:
if item.date in dates:
dates.remove(item.date)
if item.start in dates:
dates.remove(item.start)
if not exclude_saved:
diffusions.append(item)
@ -385,7 +387,7 @@ class Schedule (models.Model):
if self.initial:
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)
first_diffusion = first_diffusion[0] if first_diffusion.count() \
else None
@ -393,8 +395,8 @@ class Schedule (models.Model):
program = self.program,
type = Diffusion.Type['unconfirmed'],
initial = first_diffusion if self.initial else None,
date = date,
duration = self.duration,
start = date,
end = date + duration,
))
return diffusions
@ -581,11 +583,16 @@ class Diffusion (models.Model):
blank = True, null = True,
help_text = _('the diffusion is a rerun of this one')
)
date = models.DateTimeField( _('start of the diffusion') )
duration = models.TimeField(
_('duration'),
help_text = _('regular duration'),
)
start = models.DateTimeField( _('start of the diffusion') )
end = models.DateTimeField( _('end of the diffusion') )
@property
def duration (self):
return self.end - self.start
@property
def date (self):
return self.start
def archives_duration (self):
"""
@ -596,7 +603,7 @@ class Diffusion (models.Model):
r = [ sound.duration
for sound in sounds.filter(type = Sound.Type['archive'])
if sound.duration ]
return utils.time_sum(r) if r else self.duration
return utils.time_sum(r)
def get_archives (self):
"""
@ -613,10 +620,10 @@ class Diffusion (models.Model):
Return a queryset with the upcoming diffusions, ordered by
+date
"""
filter_args['date__gte'] = date_or_default(date)
filter_args['start__gte'] = date_or_default(date)
if 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
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
-date
"""
filter_args['date__lte'] = date_or_default(date)
filter_args['start__lte'] = date_or_default(date)
if 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):
"""
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 = []
# prev
qs = self.get_prev(self.program.station, self.date)
count = 0
for diff in qs:
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
r = Diffusion.objects.filter(
models.Q(start__lte = self.start,
end__gte = self.start) |
models.Q(start__gte = self.start,
start__lte = self.end)
)
return r
def save (self, *args, **kwargs):