annotate band/management/commands/import_old_band.py @ 151:762e46d0bb4a

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