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