changeset 57:5ff9c130f47f

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.
author Brian Neal <bgneal@gmail.com>
date Tue, 10 Apr 2012 20:30:22 -0500
parents 4579bbb6e053
children db9c66873680
files madeira/band/management/__init__.py madeira/band/management/commands/__init__.py madeira/band/management/commands/import_old_band.py madeira/band/models.py madeira/gigs/views.py madeira/templates/band/buy.html
diffstat 4 files changed, 158 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- /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 = '<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()
--- 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)
 
--- 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]])
--- 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 @@
    <p>
    <img class="right" src="{{ album.photo.image.url }}" alt="{{ album.title }}" title="{{ album.title }}" />
    </p>
-   {% if album.label_release_set %}
+   {% if album.labels %}
       <ul>
-      {% for release in album.label_release_set.all %}
+      {% for release in album.labels.all %}
          <li><a href="{{ release.record_label.url }}">{{ release.record_label.name }}</a> 
             {{ release.catalog_number }}, {{ release.release_date|date:"F d, Y" }}</li>
       {% endfor %}
@@ -18,14 +18,14 @@
    {{ album.desc|safe|linebreaks }}
    <p>Track listing:</p>
    <ol>
-   {% for track in album.album_track_set.all %}
+   {% for track in album.tracks.all %}
       <li>{{ track.track_name }}</li>
    {% endfor %}
    </ol>
-   {% if album.album_merchant_set %}
+   {% if album.merchants %}
       <p>Buy {{ album.title }} at:</p>
       <ul>
-      {% for merchant in album.album_merchant_set.all %}
+      {% for merchant in album.merchants.all %}
          <li><a href="{{ merchant.url }}">{{ merchant.name }}</a></li>
       {% endfor %}
       </ul>