annotate videos/management/commands/import_old_videos.py @ 150:a9d39fafacaf

Break news and press out on the navbar.
author Brian Neal <bgneal@gmail.com>
date Fri, 04 Jul 2014 14:25:36 -0500
parents b7cdfdde3999
children
rev   line source
bgneal@50 1 """
bgneal@50 2 import_old_videos.py - For importing video data from the older version of this
bgneal@50 3 website.
bgneal@50 4
bgneal@50 5 """
bgneal@50 6 import datetime
bgneal@89 7 import json
bgneal@50 8
bgneal@50 9 from django.core.management.base import LabelCommand
bgneal@50 10 from django.utils.html import linebreaks
bgneal@50 11
bgneal@50 12 from videos.models import Collection, Video
bgneal@50 13
bgneal@50 14
bgneal@50 15 class Command(LabelCommand):
bgneal@50 16 args = '<filename filename ...>'
bgneal@50 17 help = 'Imports older video & video sets in JSON format'
bgneal@50 18
bgneal@50 19 collections = {}
bgneal@50 20
bgneal@50 21 def handle_label(self, filename, **options):
bgneal@50 22 """
bgneal@50 23 Process the file of older video & video sets in JSON. Convert to the new
bgneal@50 24 model scheme.
bgneal@50 25
bgneal@50 26 """
bgneal@50 27 with open(filename, 'rb') as f:
bgneal@50 28 items = json.load(f)
bgneal@50 29
bgneal@50 30 for item in items:
bgneal@50 31 if item['model'] == 'band.video_set':
bgneal@50 32 self.process_set(item)
bgneal@50 33
bgneal@50 34 for item in items:
bgneal@50 35 if item['model'] == 'band.video':
bgneal@50 36 self.process_video(item)
bgneal@50 37
bgneal@50 38 def process_set(self, item):
bgneal@50 39
bgneal@50 40 fields = item['fields']
bgneal@50 41
bgneal@50 42 description = linebreaks(fields['text'].strip())
bgneal@50 43
bgneal@50 44 # there are several sets with the same date, so to get the ordering
bgneal@50 45 # right, add the pk as seconds.
bgneal@50 46
bgneal@50 47 date_added = datetime.datetime.strptime(fields['date'], '%Y-%m-%d')
bgneal@50 48 date_added += datetime.timedelta(seconds=int(item['pk']))
bgneal@50 49
bgneal@50 50 coll = Collection(
bgneal@50 51 id=item['pk'],
bgneal@50 52 title=fields['title'].strip(),
bgneal@50 53 date_added=date_added,
bgneal@50 54 description=description)
bgneal@50 55
bgneal@50 56 coll.save()
bgneal@50 57 self.collections[coll.pk] = coll
bgneal@50 58
bgneal@50 59 def process_video(self, item):
bgneal@50 60
bgneal@50 61 fields = item['fields']
bgneal@50 62
bgneal@50 63 video = Video(
bgneal@50 64 id=item['pk'],
bgneal@50 65 title=fields['title'].strip(),
bgneal@50 66 embed_code=fields['embed_code'],
bgneal@50 67 collection=self.collections[fields['video_set']],
bgneal@50 68 )
bgneal@50 69 video.save()