bgneal@50: """ bgneal@50: import_old_videos.py - For importing video data from the older version of this bgneal@50: website. bgneal@50: bgneal@50: """ bgneal@50: import datetime bgneal@50: bgneal@50: from django.core.management.base import LabelCommand bgneal@50: from django.utils import simplejson as json bgneal@50: from django.utils.html import linebreaks bgneal@50: bgneal@50: from videos.models import Collection, Video bgneal@50: bgneal@50: bgneal@50: class Command(LabelCommand): bgneal@50: args = '' bgneal@50: help = 'Imports older video & video sets in JSON format' bgneal@50: bgneal@50: collections = {} bgneal@50: bgneal@50: def handle_label(self, filename, **options): bgneal@50: """ bgneal@50: Process the file of older video & video sets in JSON. Convert to the new bgneal@50: model scheme. bgneal@50: bgneal@50: """ bgneal@50: with open(filename, 'rb') as f: bgneal@50: items = json.load(f) bgneal@50: bgneal@50: for item in items: bgneal@50: if item['model'] == 'band.video_set': bgneal@50: self.process_set(item) bgneal@50: bgneal@50: for item in items: bgneal@50: if item['model'] == 'band.video': bgneal@50: self.process_video(item) bgneal@50: bgneal@50: def process_set(self, item): bgneal@50: bgneal@50: fields = item['fields'] bgneal@50: bgneal@50: description = linebreaks(fields['text'].strip()) bgneal@50: bgneal@50: # there are several sets with the same date, so to get the ordering bgneal@50: # right, add the pk as seconds. bgneal@50: bgneal@50: date_added = datetime.datetime.strptime(fields['date'], '%Y-%m-%d') bgneal@50: date_added += datetime.timedelta(seconds=int(item['pk'])) bgneal@50: bgneal@50: coll = Collection( bgneal@50: id=item['pk'], bgneal@50: title=fields['title'].strip(), bgneal@50: date_added=date_added, bgneal@50: description=description) bgneal@50: bgneal@50: coll.save() bgneal@50: self.collections[coll.pk] = coll bgneal@50: bgneal@50: def process_video(self, item): bgneal@50: bgneal@50: fields = item['fields'] bgneal@50: bgneal@50: video = Video( bgneal@50: id=item['pk'], bgneal@50: title=fields['title'].strip(), bgneal@50: embed_code=fields['embed_code'], bgneal@50: collection=self.collections[fields['video_set']], bgneal@50: ) bgneal@50: video.save()