view band/management/commands/import_old_band.py @ 95:7b52e8ef01ec

For Django 1.5: Remove usage of django.contrib.markup. Since we were only using Textile in one place, decided to stop using it altogether. Wrote a management command, untextile, to convert the gallery models' description fields from textile to raw HTML. This should be run one time before going live with Django 1.5.
author Brian Neal <bgneal@gmail.com>
date Wed, 28 Aug 2013 19:22:23 -0500
parents b7cdfdde3999
children
line wrap: on
line source
"""
import_old_band.py - For importing band models from the older version of this
website.

"""
import datetime
import json

from django.core.management.base import LabelCommand
from photologue.models import Photo

from band.models import (Member, Gear, RecordLabel, Album, AlbumTrack,
        LabelRelease, AlbumMerchant, Merchandise)


class Command(LabelCommand):
    args = '<filename filename ...>'
    help = 'Imports older band model objects in JSON format'

    members = {}
    albums = {}
    labels = {}

    def handle_label(self, filename, **options):
        """
        Process the file of older model objects 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.member':
                self.process_member(item)
            elif item['model'] == 'band.gear':
                self.process_gear(item)
            elif item['model'] == 'band.record_label':
                self.process_record_label(item)
            elif item['model'] == 'band.album':
                self.process_album(item)
            elif item['model'] == 'band.album_track':
                self.process_track(item)
            elif item['model'] == 'band.label_release':
                self.process_release(item)
            elif item['model'] == 'band.album_merchant':
                self.process_merchant(item)
            elif item['model'] == 'band.merchandise':
                self.process_merch(item)

    def process_member(self, item):

        fields = item['fields']

        start_date = datetime.datetime.strptime(fields['start_date'], '%Y-%m-%d')

        if fields['end_date'] == u'1985-01-01':
            end_date = None
        else:
            end_date = datetime.datetime.strptime(fields['end_date'], '%Y-%m-%d')

        member = Member(id=item['pk'],
                name=fields['name'],
                nickname=fields['nickname'],
                instrument=fields['instrument'],
                bio=fields['bio'],
                photo=fields['photo'],
                order=int(fields['order']),
                is_active=fields['is_active'],
                start_date=start_date,
                end_date=end_date,
                email=fields['email'])
        member.save()
        self.members[member.id] = member

    def process_gear(self, item):
        fields = item['fields']

        gear = Gear(id=item['pk'],
                member=self.members[int(fields['member'])],
                item=fields['item'])
        gear.save()

    def process_record_label(self, item):
        fields = item['fields']

        label = RecordLabel(id=item['pk'],
                url=fields['url'],
                name=fields['name'])
        label.save()
        self.labels[label.id] = label
        
    def process_album(self, item):
        fields = item['fields']

        photo = Photo.objects.get(id=fields['photo'])

        album = Album(id=item['pk'],
                title=fields['title'],
                photo=photo,
                desc=fields['desc'])
        album.save()
        self.albums[album.id] = album

    def process_track(self, item):
        fields = item['fields']

        track = AlbumTrack(id=item['pk'],
                album=self.albums[int(fields['album'])],
                track_number=int(fields['track_number']),
                track_name=fields['track_name'])
        track.save()

    def process_release(self, item):
        fields = item['fields']

        release = LabelRelease(id=item['pk'],
                record_label=self.labels[int(fields['record_label'])],
                album=self.albums[int(fields['album'])],
                catalog_number=fields['catalog_number'],
                release_date=datetime.datetime.strptime(
                    fields['release_date'], '%Y-%m-%d'))

        release.save()

    def process_merchant(self, item):
        fields = item['fields']

        merchant = AlbumMerchant(id=item['pk'],
                album=self.albums[int(fields['album'])],
                url=fields['url'],
                name=fields['name'])

        merchant.save()

    def process_merch(self, item):
        fields = item['fields']

        photo = Photo.objects.get(id=fields['photo'])

        merch = Merchandise(id=item['pk'],
                name=fields['name'],
                desc=fields['desc'],
                price=fields['price'],
                in_stock=fields['in_stock'],
                photo=photo)

        merch.save()