comparison band/management/commands/import_old_band.py @ 71:e2868ad47a1e

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