view videos/management/commands/import_old_videos.py @ 130:3062c547bb90

For Django 1.6: new test discovery plus reverse now does urlquote(). My base64 keys were padded with '=' and these got quoted when doing a reverse to generate the URL. So changed the test to look for a quoted version of the key. This will change the URLs sent to users, but I believe it will all be taken care of by Django.
author Brian Neal <bgneal@gmail.com>
date Tue, 24 Dec 2013 16:47:27 -0600
parents b7cdfdde3999
children
line wrap: on
line source
"""
import_old_videos.py - For importing video data from the older version of this
website.

"""
import datetime
import json

from django.core.management.base import LabelCommand
from django.utils.html import linebreaks

from videos.models import Collection, Video


class Command(LabelCommand):
    args = '<filename filename ...>'
    help = 'Imports older video & video sets in JSON format'

    collections = {}

    def handle_label(self, filename, **options):
        """
        Process the file of older video & video sets in JSON. Convert to the new
        model scheme.

        """
        with open(filename, 'rb') as f:
            items = json.load(f)

        for item in items:
            if item['model'] == 'band.video_set':
                self.process_set(item)

        for item in items:
            if item['model'] == 'band.video':
                self.process_video(item)

    def process_set(self, item):

        fields = item['fields']

        description = linebreaks(fields['text'].strip())

        # there are several sets with the same date, so to get the ordering
        # right, add the pk as seconds.

        date_added = datetime.datetime.strptime(fields['date'], '%Y-%m-%d')
        date_added += datetime.timedelta(seconds=int(item['pk']))

        coll = Collection(
                id=item['pk'],
                title=fields['title'].strip(),
                date_added=date_added,
                description=description)

        coll.save()
        self.collections[coll.pk] = coll

    def process_video(self, item):

        fields = item['fields']

        video = Video(
                id=item['pk'],
                title=fields['title'].strip(),
                embed_code=fields['embed_code'],
                collection=self.collections[fields['video_set']],
                )
        video.save()