bgneal@57: """ bgneal@57: import_old_band.py - For importing band models from the older version of this bgneal@57: website. bgneal@57: bgneal@57: """ bgneal@57: import datetime bgneal@89: import json bgneal@57: bgneal@57: from django.core.management.base import LabelCommand bgneal@57: from photologue.models import Photo bgneal@57: bgneal@57: from band.models import (Member, Gear, RecordLabel, Album, AlbumTrack, bgneal@57: LabelRelease, AlbumMerchant, Merchandise) bgneal@57: bgneal@57: bgneal@57: class Command(LabelCommand): bgneal@57: args = '' bgneal@57: help = 'Imports older band model objects in JSON format' bgneal@57: bgneal@57: members = {} bgneal@57: albums = {} bgneal@57: labels = {} bgneal@57: bgneal@57: def handle_label(self, filename, **options): bgneal@57: """ bgneal@57: Process the file of older model objects in JSON. Convert to the new model bgneal@57: scheme. bgneal@57: bgneal@57: """ bgneal@57: with open(filename, 'rb') as f: bgneal@57: items = json.load(f) bgneal@57: bgneal@57: for item in items: bgneal@57: if item['model'] == 'band.member': bgneal@57: self.process_member(item) bgneal@57: elif item['model'] == 'band.gear': bgneal@57: self.process_gear(item) bgneal@57: elif item['model'] == 'band.record_label': bgneal@57: self.process_record_label(item) bgneal@57: elif item['model'] == 'band.album': bgneal@57: self.process_album(item) bgneal@57: elif item['model'] == 'band.album_track': bgneal@57: self.process_track(item) bgneal@57: elif item['model'] == 'band.label_release': bgneal@57: self.process_release(item) bgneal@57: elif item['model'] == 'band.album_merchant': bgneal@57: self.process_merchant(item) bgneal@57: elif item['model'] == 'band.merchandise': bgneal@57: self.process_merch(item) bgneal@57: bgneal@57: def process_member(self, item): bgneal@57: bgneal@57: fields = item['fields'] bgneal@57: bgneal@57: start_date = datetime.datetime.strptime(fields['start_date'], '%Y-%m-%d') bgneal@57: bgneal@57: if fields['end_date'] == u'1985-01-01': bgneal@57: end_date = None bgneal@57: else: bgneal@57: end_date = datetime.datetime.strptime(fields['end_date'], '%Y-%m-%d') bgneal@57: bgneal@57: member = Member(id=item['pk'], bgneal@57: name=fields['name'], bgneal@57: nickname=fields['nickname'], bgneal@57: instrument=fields['instrument'], bgneal@57: bio=fields['bio'], bgneal@57: photo=fields['photo'], bgneal@57: order=int(fields['order']), bgneal@57: is_active=fields['is_active'], bgneal@57: start_date=start_date, bgneal@57: end_date=end_date, bgneal@57: email=fields['email']) bgneal@57: member.save() bgneal@57: self.members[member.id] = member bgneal@57: bgneal@57: def process_gear(self, item): bgneal@57: fields = item['fields'] bgneal@57: bgneal@57: gear = Gear(id=item['pk'], bgneal@57: member=self.members[int(fields['member'])], bgneal@57: item=fields['item']) bgneal@57: gear.save() bgneal@57: bgneal@57: def process_record_label(self, item): bgneal@57: fields = item['fields'] bgneal@57: bgneal@57: label = RecordLabel(id=item['pk'], bgneal@57: url=fields['url'], bgneal@57: name=fields['name']) bgneal@57: label.save() bgneal@57: self.labels[label.id] = label bgneal@57: bgneal@57: def process_album(self, item): bgneal@57: fields = item['fields'] bgneal@57: bgneal@57: photo = Photo.objects.get(id=fields['photo']) bgneal@57: bgneal@57: album = Album(id=item['pk'], bgneal@57: title=fields['title'], bgneal@57: photo=photo, bgneal@57: desc=fields['desc']) bgneal@57: album.save() bgneal@57: self.albums[album.id] = album bgneal@57: bgneal@57: def process_track(self, item): bgneal@57: fields = item['fields'] bgneal@57: bgneal@57: track = AlbumTrack(id=item['pk'], bgneal@57: album=self.albums[int(fields['album'])], bgneal@57: track_number=int(fields['track_number']), bgneal@57: track_name=fields['track_name']) bgneal@57: track.save() bgneal@57: bgneal@57: def process_release(self, item): bgneal@57: fields = item['fields'] bgneal@57: bgneal@57: release = LabelRelease(id=item['pk'], bgneal@57: record_label=self.labels[int(fields['record_label'])], bgneal@57: album=self.albums[int(fields['album'])], bgneal@57: catalog_number=fields['catalog_number'], bgneal@57: release_date=datetime.datetime.strptime( bgneal@57: fields['release_date'], '%Y-%m-%d')) bgneal@57: bgneal@57: release.save() bgneal@57: bgneal@57: def process_merchant(self, item): bgneal@57: fields = item['fields'] bgneal@57: bgneal@57: merchant = AlbumMerchant(id=item['pk'], bgneal@57: album=self.albums[int(fields['album'])], bgneal@57: url=fields['url'], bgneal@57: name=fields['name']) bgneal@57: bgneal@57: merchant.save() bgneal@57: bgneal@57: def process_merch(self, item): bgneal@57: fields = item['fields'] bgneal@57: bgneal@57: photo = Photo.objects.get(id=fields['photo']) bgneal@57: bgneal@57: merch = Merchandise(id=item['pk'], bgneal@57: name=fields['name'], bgneal@57: desc=fields['desc'], bgneal@57: price=fields['price'], bgneal@57: in_stock=fields['in_stock'], bgneal@57: photo=photo) bgneal@57: bgneal@57: merch.save()