view mp3/management/commands/import_old_mp3.py @ 114:4f44b56b43de

Don't show link to news story on detail view.
author Brian Neal <bgneal@gmail.com>
date Sun, 20 Oct 2013 14:07:08 -0500
parents b7cdfdde3999
children
line wrap: on
line source
"""
import_old_mp3.py - For importing mp3 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 mp3.models import Collection, Song


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

    collections = {}

    def handle_label(self, filename, **options):
        """
        Process the file of older mp3 & mp3 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.mp3_set':
                self.process_mp3_set(item)

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

    def process_mp3_set(self, item):

        fields = item['fields']

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

        coll = Collection(
                id=item['pk'],
                title=fields['title'].strip(),
                date_added=datetime.datetime.strptime(fields['date'], '%Y-%m-%d'),
                description=description)

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

    def process_mp3(self, item):

        fields = item['fields']

        song = Song(
                id=item['pk'],
                title=fields['title'].strip(),
                description=fields['desc'].strip(),
                file=fields['file'],
                collection=self.collections[fields['mp3_set']],
                )
        song.save()