bgneal@48
|
1 """
|
bgneal@48
|
2 import_old_mp3.py - For importing mp3 data from the older version of this
|
bgneal@48
|
3 website.
|
bgneal@48
|
4
|
bgneal@48
|
5 """
|
bgneal@48
|
6 import datetime
|
bgneal@48
|
7
|
bgneal@48
|
8 from django.core.management.base import LabelCommand
|
bgneal@48
|
9 from django.utils import simplejson as json
|
bgneal@48
|
10 from django.utils.html import linebreaks
|
bgneal@48
|
11
|
bgneal@48
|
12 from mp3.models import Collection, Song
|
bgneal@48
|
13
|
bgneal@48
|
14
|
bgneal@48
|
15 class Command(LabelCommand):
|
bgneal@48
|
16 args = '<filename filename ...>'
|
bgneal@48
|
17 help = 'Imports older mp3 & mp3 sets in JSON format'
|
bgneal@48
|
18
|
bgneal@48
|
19 collections = {}
|
bgneal@48
|
20
|
bgneal@48
|
21 def handle_label(self, filename, **options):
|
bgneal@48
|
22 """
|
bgneal@48
|
23 Process the file of older mp3 & mp3 sets in JSON. Convert to the new
|
bgneal@48
|
24 model scheme.
|
bgneal@48
|
25
|
bgneal@48
|
26 """
|
bgneal@48
|
27 with open(filename, 'rb') as f:
|
bgneal@48
|
28 items = json.load(f)
|
bgneal@48
|
29
|
bgneal@48
|
30 for item in items:
|
bgneal@48
|
31 if item['model'] == 'band.mp3_set':
|
bgneal@48
|
32 self.process_mp3_set(item)
|
bgneal@48
|
33
|
bgneal@48
|
34 for item in items:
|
bgneal@48
|
35 if item['model'] == 'band.mp3':
|
bgneal@48
|
36 self.process_mp3(item)
|
bgneal@48
|
37
|
bgneal@48
|
38 def process_mp3_set(self, item):
|
bgneal@48
|
39
|
bgneal@48
|
40 fields = item['fields']
|
bgneal@48
|
41
|
bgneal@48
|
42 description = linebreaks(fields['text'].strip())
|
bgneal@48
|
43
|
bgneal@48
|
44 coll = Collection(
|
bgneal@48
|
45 id=item['pk'],
|
bgneal@48
|
46 title=fields['title'].strip(),
|
bgneal@48
|
47 date_added=datetime.datetime.strptime(fields['date'], '%Y-%m-%d'),
|
bgneal@48
|
48 description=description)
|
bgneal@48
|
49
|
bgneal@48
|
50 coll.save()
|
bgneal@48
|
51 self.collections[coll.pk] = coll
|
bgneal@48
|
52
|
bgneal@48
|
53 def process_mp3(self, item):
|
bgneal@48
|
54
|
bgneal@48
|
55 fields = item['fields']
|
bgneal@48
|
56
|
bgneal@48
|
57 song = Song(
|
bgneal@48
|
58 id=item['pk'],
|
bgneal@48
|
59 title=fields['title'].strip(),
|
bgneal@48
|
60 description=fields['desc'].strip(),
|
bgneal@48
|
61 file=fields['file'],
|
bgneal@48
|
62 collection=self.collections[fields['mp3_set']],
|
bgneal@48
|
63 )
|
bgneal@48
|
64 song.save()
|