forked from rc/aircox
new management tool: add
This commit is contained in:
143
programs/management/commands/add.py
Normal file
143
programs/management/commands/add.py
Normal file
@ -0,0 +1,143 @@
|
||||
from django.core.management.base import BaseCommand, CommandError
|
||||
from django.utils import timezone
|
||||
import programs.models as models
|
||||
|
||||
|
||||
class Model:
|
||||
# dict: key is the argument name, value is the constructor
|
||||
required = {}
|
||||
optional = {}
|
||||
model = None
|
||||
|
||||
|
||||
def __init__ (self, model, required = {}, optional = {}, post = None):
|
||||
self.model = model
|
||||
self.required = required
|
||||
self.optional = optional
|
||||
self.post = post
|
||||
|
||||
|
||||
def check_or_raise (self, options):
|
||||
for req in self.required:
|
||||
if req not in options:
|
||||
raise ValueError('required argument ' + req + ' is missing')
|
||||
|
||||
|
||||
def get_kargs (self, options):
|
||||
kargs = {}
|
||||
|
||||
for i in self.required:
|
||||
if options.get(i):
|
||||
fn = self.required[i]
|
||||
kargs[i] = fn(options[i])
|
||||
|
||||
for i in self.optional:
|
||||
if options.get(i):
|
||||
print(i, options)
|
||||
fn = self.optional[i]
|
||||
kargs[i] = fn(options[i])
|
||||
|
||||
return kargs
|
||||
|
||||
|
||||
def make (self, options):
|
||||
self.check_or_raise(options)
|
||||
|
||||
kargs = self.get_kargs(options)
|
||||
instance = self.model(**kargs)
|
||||
instance.save()
|
||||
|
||||
if self.post:
|
||||
self.post(instance, options)
|
||||
|
||||
print(instance.__dict__)
|
||||
|
||||
|
||||
def DateTime (string):
|
||||
dt = timezone.datetime.strptime(string, '%Y-%m-%d %H:%M:%S')
|
||||
return timezone.make_aware(dt, timezone.get_current_timezone())
|
||||
|
||||
|
||||
def Time (string):
|
||||
dt = timezone.datetime.strptime(string, '%H:%M')
|
||||
return timezone.datetime.time(dt)
|
||||
|
||||
|
||||
|
||||
def AddTags (instance, options):
|
||||
if options.get('tags'):
|
||||
instance.tags.add(*options['tags'])
|
||||
|
||||
|
||||
models = {
|
||||
'program': Model( models.Program
|
||||
, { 'title': str }
|
||||
, { 'subtitle': str, 'can_comment': bool, 'date': DateTime
|
||||
, 'parent_id': int, 'public': bool
|
||||
, 'url': str, 'email': str, 'non_stop': bool
|
||||
}
|
||||
, AddTags
|
||||
)
|
||||
, 'article': Model( models.Article
|
||||
, { 'title': str }
|
||||
, { 'subtitle': str, 'can_comment': bool, 'date': DateTime
|
||||
, 'parent_id': int, 'public': bool
|
||||
, 'static_page': bool, 'focus': bool
|
||||
}
|
||||
, AddTags
|
||||
)
|
||||
, 'schedule': Model( models.Schedule
|
||||
, { 'parent_id': int, 'date': DateTime, 'duration': Time
|
||||
, 'frequency': int }
|
||||
, { 'rerun': bool }
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
|
||||
class Command (BaseCommand):
|
||||
help="Add an element of the given model"
|
||||
|
||||
|
||||
def add_arguments (self, parser):
|
||||
parser.add_argument( 'model', type=str
|
||||
, metavar="MODEL"
|
||||
, help="model to add. It must be in [schedule,program,article]")
|
||||
|
||||
# publication/generic
|
||||
parser.add_argument('--parent_id', type=str)
|
||||
parser.add_argument('--title', type=str)
|
||||
parser.add_argument('--subtitle', type=str)
|
||||
parser.add_argument('--can_comment',action='store_true')
|
||||
parser.add_argument('--public', action='store_true')
|
||||
parser.add_argument( '--date', type=str
|
||||
, help='a valid date time (Y/m/d H:m:s')
|
||||
parser.add_argument('--tags', type=str, nargs='+')
|
||||
|
||||
# program
|
||||
parser.add_argument('--url', type=str)
|
||||
parser.add_argument('--email', type=str)
|
||||
parser.add_argument('--non_stop', type=int)
|
||||
|
||||
# article
|
||||
parser.add_argument('--static_page',action='store_true')
|
||||
parser.add_argument('--focus', action='store_true')
|
||||
|
||||
# schedule
|
||||
parser.add_argument('--duration', type=str)
|
||||
parser.add_argument('--frequency', type=int)
|
||||
parser.add_argument('--rerun', action='store_true')
|
||||
|
||||
|
||||
def handle (self, *args, **options):
|
||||
model = options.get('model')
|
||||
if not model:
|
||||
return
|
||||
|
||||
model = model.lower()
|
||||
if model not in models:
|
||||
raise ValueError("model {} is not supported".format(str(model)))
|
||||
|
||||
models[model].make(options)
|
||||
|
||||
|
@ -1,32 +0,0 @@
|
||||
import os
|
||||
|
||||
from django.core.management.base import BaseCommand, CommandError
|
||||
import programs.models as models
|
||||
import programs.settings
|
||||
|
||||
|
||||
class Command (BaseCommand):
|
||||
help= "Take a look at the programs directory to check on new podcasts"
|
||||
|
||||
|
||||
def handle (self, *args, **options):
|
||||
programs = models.Program.objects.filter(schedule__isnull = True)
|
||||
|
||||
for program in programs:
|
||||
self.scan(program, program.path + '/public', public = True)
|
||||
self.scan(program, program.path + '/podcasts', embed = True)
|
||||
self.scan(program, program.path + '/private')
|
||||
|
||||
|
||||
def scan (self, program, path, public = False, embed = False):
|
||||
try:
|
||||
for filename in os.listdir(path):
|
||||
long_filename = path + '/' + filename
|
||||
|
||||
# check for new sound files
|
||||
# stat the sound files
|
||||
# match sound files against episodes - if not found, create it
|
||||
# upload public podcasts to mixcloud if required
|
||||
except:
|
||||
pass
|
||||
|
@ -1,72 +0,0 @@
|
||||
import datetime
|
||||
|
||||
from django.core.management.base import BaseCommand, CommandError
|
||||
from django.utils import timezone, dateformat
|
||||
|
||||
import programs.models as models
|
||||
import programs.settings
|
||||
|
||||
|
||||
class Diffusion:
|
||||
ref = None
|
||||
date_start = None
|
||||
date_end = None
|
||||
|
||||
def __init__ (self, ref, date_start, date_end):
|
||||
self.ref = ref
|
||||
self.date_start = date_start
|
||||
self.date_end = date_end
|
||||
|
||||
def __lt__ (self, d):
|
||||
return self.date_start < d.date_start and \
|
||||
self.date_end < d.date_end
|
||||
|
||||
|
||||
|
||||
class Command (BaseCommand):
|
||||
help= "check sounds to diffuse"
|
||||
|
||||
diffusions = set()
|
||||
|
||||
def handle(self, *args, **options):
|
||||
self.get_next_events()
|
||||
self.get_next_episodes()
|
||||
|
||||
for diffusion in self.diffusions:
|
||||
print( diffusion.ref.__str__()
|
||||
, diffusion.date_start
|
||||
, diffusion.date_end)
|
||||
|
||||
|
||||
|
||||
def get_next_episodes (self):
|
||||
schedules = models.Schedule.objects.filter()
|
||||
for schedule in schedules:
|
||||
date = schedule.next_date()
|
||||
if not date:
|
||||
continue
|
||||
|
||||
dt = datetime.timedelta( hours = schedule.duration.hour
|
||||
, minutes = schedule.duration.minute
|
||||
, seconds = schedule.duration.second )
|
||||
|
||||
ref = models.Episode.objects.filter(date = date)[:1]
|
||||
if not ref:
|
||||
ref = ( schedule.parent, )
|
||||
|
||||
diffusion = Diffusion(ref[0], date, date + dt)
|
||||
self.diffusions.add(diffusion)
|
||||
|
||||
|
||||
def get_next_events (self):
|
||||
events = models.Event.objects.filter(date_end__gt = timezone.now(),
|
||||
canceled = False) \
|
||||
.extra(order_by = ['date'])[:10]
|
||||
for event in events:
|
||||
diffusion = Diffusion(event, event.date, event.date_end)
|
||||
self.diffusions.add(diffusion)
|
||||
|
||||
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user