# HG changeset patch # User Brian Neal # Date 1334107822 18000 # Node ID 5ff9c130f47f9235c225030fa90d35ffe347872c # Parent 4579bbb6e053d023a19354b4cad5fd5a24f1284d Added management command to import old band model objects. Use related_name on band model foreign keys for better naming. Raw SQL query in gigs was using old band table. diff -r 4579bbb6e053 -r 5ff9c130f47f madeira/band/management/commands/import_old_band.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/madeira/band/management/commands/import_old_band.py Tue Apr 10 20:30:22 2012 -0500 @@ -0,0 +1,149 @@ +""" +import_old_band.py - For importing band models from the older version of this +website. + +""" +import datetime + +from django.core.management.base import LabelCommand +from django.utils import simplejson as json +from django.utils.html import linebreaks +from photologue.models import Photo + +from band.models import (Member, Gear, RecordLabel, Album, AlbumTrack, + LabelRelease, AlbumMerchant, Merchandise) + + +class Command(LabelCommand): + args = '' + 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() diff -r 4579bbb6e053 -r 5ff9c130f47f madeira/band/models.py --- a/madeira/band/models.py Sat Apr 07 16:28:27 2012 -0500 +++ b/madeira/band/models.py Tue Apr 10 20:30:22 2012 -0500 @@ -58,7 +58,7 @@ class AlbumTrack(models.Model): - album = models.ForeignKey(Album) + album = models.ForeignKey(Album, related_name='tracks') track_number = models.SmallIntegerField() track_name = models.CharField(max_length=64) @@ -72,7 +72,7 @@ class LabelRelease(models.Model): record_label = models.ForeignKey(RecordLabel) - album = models.ForeignKey(Album) + album = models.ForeignKey(Album, related_name='labels') catalog_number = models.CharField(max_length=32) release_date = models.DateField() @@ -85,7 +85,7 @@ class AlbumMerchant(models.Model): - album = models.ForeignKey(Album) + album = models.ForeignKey(Album, related_name='merchants') name = models.CharField(max_length=64) url = models.URLField(verify_exists=False, max_length=200) diff -r 4579bbb6e053 -r 5ff9c130f47f madeira/gigs/views.py --- a/madeira/gigs/views.py Sat Apr 07 16:28:27 2012 -0500 +++ b/madeira/gigs/views.py Tue Apr 10 20:30:22 2012 -0500 @@ -31,7 +31,7 @@ bands = dict((band.id, band) for band in Band.objects.all()) cursor = connection.cursor() - cursor.execute('SELECT * FROM band_gig_bands') + cursor.execute('SELECT * FROM gigs_gig_bands') gig_bands = collections.defaultdict(list) for row in cursor.fetchall(): gig_bands[row[1]].append(bands[row[2]]) diff -r 4579bbb6e053 -r 5ff9c130f47f madeira/templates/band/buy.html --- a/madeira/templates/band/buy.html Sat Apr 07 16:28:27 2012 -0500 +++ b/madeira/templates/band/buy.html Tue Apr 10 20:30:22 2012 -0500 @@ -7,9 +7,9 @@

{{ album.title }}

- {% if album.label_release_set %} + {% if album.labels %}