view mp3/management/commands/import_old_mp3.py @ 163:7fc4123fef70

Stop passing callable to queryset. This is now deprecated. The usage here was most likely a mistake that has worked all these years. This change was for upgrading to Django 1.7.
author Brian Neal <bgneal@gmail.com>
date Thu, 02 Apr 2015 19:17:47 -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()