work on monitor command

This commit is contained in:
bkfox 2015-06-30 17:31:01 +02:00
parent 45178862db
commit 2b78ba19de
3 changed files with 65 additions and 6 deletions

View File

@ -1,3 +1,5 @@
import argparse
from django.core.management.base import BaseCommand, CommandError
from django.utils import timezone
import programs.models as models
@ -17,6 +19,14 @@ class Model:
self.post = post
def to_string (self):
return '\n'.join(
[ ' - required: {}'.format(', '.join(self.required))
, ' - optional: {}'.format(', '.join(self.optional))
, (self.post is AddTags and ' - tags available\n') or
'\n'
])
def check_or_raise (self, options):
for req in self.required:
if req not in options:
@ -145,11 +155,24 @@ models = {
}
, AddTags
)
, 'episode': Model( models.Episode
, { 'title': str }
, { 'subtitle': str, 'can_comment': bool, 'date': DateTime
, 'parent_id': int, 'public': bool
}
, AddTags
)
, 'schedule': Model( models.Schedule
, { 'parent_id': int, 'date': DateTime, 'duration': Time
, 'frequency': int }
, { 'rerun': bool }
)
, 'soundfile': Model( models.SoundFile
, { 'parent_id': int, 'date': DateTime, 'file': str
, 'duration': Time}
, { 'fragment': bool, 'embed': str, 'removed': bool }
, AddTags
)
}
@ -158,6 +181,7 @@ class Command (BaseCommand):
help='Create, update, delete or dump an element of the given model.' \
' If no action is given, dump it'
def add_arguments (self, parser):
parser.add_argument( 'model', type=str
, metavar="MODEL"
@ -210,6 +234,13 @@ class Command (BaseCommand):
group.add_argument('--frequency', type=int)
group.add_argument('--rerun', action='store_true')
# fields
parser.formatter_class=argparse.RawDescriptionHelpFormatter
parser.epilog = 'available fields per model:'
for name, model in models.items():
parser.epilog += '\n ' + model.model.type() + ': \n' \
+ model.to_string()
def handle (self, *args, **options):
model = options.get('model')

View File

@ -280,6 +280,21 @@ class SoundFile (Metadata):
)
def get_mtime (self):
"""
Get the last modification date from file
"""
mtime = os.stat(self.file.path).st_mtime
mtime = timezone.datetime.fromtimestamp(mtime)
return timezone.make_aware(mtime, timezone.get_current_timezone())
def save (self, *args, **kwargs):
if not self.pk:
self.date = self.get_mtime()
super(SoundFile, self).save(*args, **kwargs)
def __upload_path (self, filename):
if self.parent and self.parent.parent:
path = self.parent.parent.path
@ -297,6 +312,7 @@ class SoundFile (Metadata):
verbose_name_plural = _('Sounds')
class Schedule (Model):
parent = models.ForeignKey( 'Program', blank = True, null = True )
date = models.DateTimeField(_('start'))
@ -312,12 +328,12 @@ class Schedule (Model):
rerun = models.BooleanField(_('rerun'), default = False)
def match_date (self, at = timezone.datetime.today()):
def match_date (self, at = timezone.datetime.today(), check_time = False):
"""
Return True if the given datetime matches the schedule
"""
if self.date.weekday() == at.weekday() and self.match_week(date):
return self.date.time() == at.date.time()
if self.date.weekday() == at.weekday() and self.match_week(at):
return (check_time and self.date.time() == at.date.time()) or True
return False
@ -401,7 +417,6 @@ class Schedule (Model):
class Article (Publication):
parent = models.ForeignKey(
'self'
@ -451,12 +466,24 @@ class Program (Publication):
)
@property
def path(self):
def path (self):
return os.path.join( settings.AIRCOX_PROGRAMS_DIR
, slugify(self.title + '_' + str(self.id))
)
def find_schedules (self, date):
"""
Return schedules that match a given date
"""
schedules = Schedule.objects.filter(parent = self)
r = []
for schedule in schedules:
if schedule.match_date(date):
r.append(schedule)
return r
class Meta:
verbose_name = _('Program')
verbose_name_plural = _('Programs')

View File

@ -10,6 +10,7 @@ ensure('AIRCOX_PROGRAMS_DIR',
os.path.join(settings.MEDIA_ROOT, 'programs'))
ensure('AIRCOX_SOUNDFILE_DEFAULT_DIR',
os.path.join(AIRCOX_PROGRAMS_DIR + 'default'))
ensure('AIRCOX_SOUNDFILE_EXT',
('ogg','flac','wav','mp3','opus'))