6 from pytz import timezone
8 from django.core.files import File
9 from django.core.management.base import BaseCommand, CommandError
10 from django.utils.timezone import make_naive, utc
12 from emissions.models import Emission, Episode, SoundFile, Diffusion
14 def construct_ruby_object(loader, suffix, node):
15 return loader.construct_yaml_map(node)
17 def construct_ruby_sym(loader, node):
18 return loader.construct_yaml_str(node)
20 yaml.add_multi_constructor(u"!ruby/object:", construct_ruby_object)
21 yaml.add_constructor(u"!ruby/sym", construct_ruby_sym)
23 def get_as_html(text):
24 return textile.textile(text)
26 class Command(BaseCommand):
27 def add_arguments(self, parser):
28 parser.add_argument('--dirname', metavar='DIRNAME')
29 parser.add_argument('--edition', metavar='EDITION')
31 def handle(self, dirname, edition, *args, **kwargs):
32 print dirname, edition
33 emission, created = Emission.objects.get_or_create(
34 slug='edition-%s' % edition,
36 'title': u'Édition %s' % edition,
37 'text': u'<p>Une bien belle année.</p>'
39 show = yaml.load(open(os.path.join(dirname, 'show.yaml')))
40 emission.title = show['attributes']['name']
41 emission.text = get_as_html(show['attributes']['description'])
42 emission.archived = True
45 europe_brussels = timezone('Europe/Brussels')
46 episodes = yaml.load(open(os.path.join(dirname, 'episodes.yaml')))
48 for image_data in yaml.load(open(os.path.join(dirname, 'images.yaml'))):
49 images[image_data['attributes']['id']] = image_data['attributes']['content_uid']
52 for episode_data in episodes:
53 episode, created = Episode.objects.get_or_create(
54 slug=episode_data['attributes']['slug'],
57 'title': episode_data['attributes']['title'],
59 episode.diffusions().delete()
60 broadcasted_at = utc.localize(episode_data['attributes']['broadcasted_at']).astimezone(europe_brussels)
61 Diffusion(episode=episode, datetime=make_naive(broadcasted_at)).save()
62 episode.text = get_as_html(episode_data['attributes']['description'])
63 if not episode.image and episode_data['attributes'].get('image_id'):
64 episode.image = File(open(os.path.join(dirname, 'images', images[episode_data['attributes']['image_id']])))
66 episode_ids[episode_data['attributes']['id']] = episode.id
69 for soundfile_data in yaml.load(open(os.path.join(dirname, 'contents.yaml'))):
70 audiobank_id = soundfile_data['attributes']['audiobank_id']
71 filepath = os.path.join(dirname, 'casts', '%s.ogg' % audiobank_id)
72 if not os.path.exists(filepath):
74 episode = episode_ids[soundfile_data['attributes']['episode_id']]
75 soundfile, created = SoundFile.objects.get_or_create(
77 title=soundfile_data['attributes']['name'],
78 fragment=not(soundfile_data['attributes']['principal']))
79 soundfile.podcastable = True
80 soundfile.file = File(open(filepath))