annotate band/management/commands/import_old_band.py @ 83:4a64d34019af

Credit Jonpaul Balak with the Tiki Magazine quote on home page.
author Brian Neal <bgneal@gmail.com>
date Sat, 15 Dec 2012 12:26:14 -0600
parents e2868ad47a1e
children b7cdfdde3999
rev   line source
bgneal@57 1 """
bgneal@57 2 import_old_band.py - For importing band models from the older version of this
bgneal@57 3 website.
bgneal@57 4
bgneal@57 5 """
bgneal@57 6 import datetime
bgneal@57 7
bgneal@57 8 from django.core.management.base import LabelCommand
bgneal@57 9 from django.utils import simplejson as json
bgneal@57 10 from django.utils.html import linebreaks
bgneal@57 11 from photologue.models import Photo
bgneal@57 12
bgneal@57 13 from band.models import (Member, Gear, RecordLabel, Album, AlbumTrack,
bgneal@57 14 LabelRelease, AlbumMerchant, Merchandise)
bgneal@57 15
bgneal@57 16
bgneal@57 17 class Command(LabelCommand):
bgneal@57 18 args = '<filename filename ...>'
bgneal@57 19 help = 'Imports older band model objects in JSON format'
bgneal@57 20
bgneal@57 21 members = {}
bgneal@57 22 albums = {}
bgneal@57 23 labels = {}
bgneal@57 24
bgneal@57 25 def handle_label(self, filename, **options):
bgneal@57 26 """
bgneal@57 27 Process the file of older model objects in JSON. Convert to the new model
bgneal@57 28 scheme.
bgneal@57 29
bgneal@57 30 """
bgneal@57 31 with open(filename, 'rb') as f:
bgneal@57 32 items = json.load(f)
bgneal@57 33
bgneal@57 34 for item in items:
bgneal@57 35 if item['model'] == 'band.member':
bgneal@57 36 self.process_member(item)
bgneal@57 37 elif item['model'] == 'band.gear':
bgneal@57 38 self.process_gear(item)
bgneal@57 39 elif item['model'] == 'band.record_label':
bgneal@57 40 self.process_record_label(item)
bgneal@57 41 elif item['model'] == 'band.album':
bgneal@57 42 self.process_album(item)
bgneal@57 43 elif item['model'] == 'band.album_track':
bgneal@57 44 self.process_track(item)
bgneal@57 45 elif item['model'] == 'band.label_release':
bgneal@57 46 self.process_release(item)
bgneal@57 47 elif item['model'] == 'band.album_merchant':
bgneal@57 48 self.process_merchant(item)
bgneal@57 49 elif item['model'] == 'band.merchandise':
bgneal@57 50 self.process_merch(item)
bgneal@57 51
bgneal@57 52 def process_member(self, item):
bgneal@57 53
bgneal@57 54 fields = item['fields']
bgneal@57 55
bgneal@57 56 start_date = datetime.datetime.strptime(fields['start_date'], '%Y-%m-%d')
bgneal@57 57
bgneal@57 58 if fields['end_date'] == u'1985-01-01':
bgneal@57 59 end_date = None
bgneal@57 60 else:
bgneal@57 61 end_date = datetime.datetime.strptime(fields['end_date'], '%Y-%m-%d')
bgneal@57 62
bgneal@57 63 member = Member(id=item['pk'],
bgneal@57 64 name=fields['name'],
bgneal@57 65 nickname=fields['nickname'],
bgneal@57 66 instrument=fields['instrument'],
bgneal@57 67 bio=fields['bio'],
bgneal@57 68 photo=fields['photo'],
bgneal@57 69 order=int(fields['order']),
bgneal@57 70 is_active=fields['is_active'],
bgneal@57 71 start_date=start_date,
bgneal@57 72 end_date=end_date,
bgneal@57 73 email=fields['email'])
bgneal@57 74 member.save()
bgneal@57 75 self.members[member.id] = member
bgneal@57 76
bgneal@57 77 def process_gear(self, item):
bgneal@57 78 fields = item['fields']
bgneal@57 79
bgneal@57 80 gear = Gear(id=item['pk'],
bgneal@57 81 member=self.members[int(fields['member'])],
bgneal@57 82 item=fields['item'])
bgneal@57 83 gear.save()
bgneal@57 84
bgneal@57 85 def process_record_label(self, item):
bgneal@57 86 fields = item['fields']
bgneal@57 87
bgneal@57 88 label = RecordLabel(id=item['pk'],
bgneal@57 89 url=fields['url'],
bgneal@57 90 name=fields['name'])
bgneal@57 91 label.save()
bgneal@57 92 self.labels[label.id] = label
bgneal@57 93
bgneal@57 94 def process_album(self, item):
bgneal@57 95 fields = item['fields']
bgneal@57 96
bgneal@57 97 photo = Photo.objects.get(id=fields['photo'])
bgneal@57 98
bgneal@57 99 album = Album(id=item['pk'],
bgneal@57 100 title=fields['title'],
bgneal@57 101 photo=photo,
bgneal@57 102 desc=fields['desc'])
bgneal@57 103 album.save()
bgneal@57 104 self.albums[album.id] = album
bgneal@57 105
bgneal@57 106 def process_track(self, item):
bgneal@57 107 fields = item['fields']
bgneal@57 108
bgneal@57 109 track = AlbumTrack(id=item['pk'],
bgneal@57 110 album=self.albums[int(fields['album'])],
bgneal@57 111 track_number=int(fields['track_number']),
bgneal@57 112 track_name=fields['track_name'])
bgneal@57 113 track.save()
bgneal@57 114
bgneal@57 115 def process_release(self, item):
bgneal@57 116 fields = item['fields']
bgneal@57 117
bgneal@57 118 release = LabelRelease(id=item['pk'],
bgneal@57 119 record_label=self.labels[int(fields['record_label'])],
bgneal@57 120 album=self.albums[int(fields['album'])],
bgneal@57 121 catalog_number=fields['catalog_number'],
bgneal@57 122 release_date=datetime.datetime.strptime(
bgneal@57 123 fields['release_date'], '%Y-%m-%d'))
bgneal@57 124
bgneal@57 125 release.save()
bgneal@57 126
bgneal@57 127 def process_merchant(self, item):
bgneal@57 128 fields = item['fields']
bgneal@57 129
bgneal@57 130 merchant = AlbumMerchant(id=item['pk'],
bgneal@57 131 album=self.albums[int(fields['album'])],
bgneal@57 132 url=fields['url'],
bgneal@57 133 name=fields['name'])
bgneal@57 134
bgneal@57 135 merchant.save()
bgneal@57 136
bgneal@57 137 def process_merch(self, item):
bgneal@57 138 fields = item['fields']
bgneal@57 139
bgneal@57 140 photo = Photo.objects.get(id=fields['photo'])
bgneal@57 141
bgneal@57 142 merch = Merchandise(id=item['pk'],
bgneal@57 143 name=fields['name'],
bgneal@57 144 desc=fields['desc'],
bgneal@57 145 price=fields['price'],
bgneal@57 146 in_stock=fields['in_stock'],
bgneal@57 147 photo=photo)
bgneal@57 148
bgneal@57 149 merch.save()