bgneal@48: """ bgneal@48: import_old_mp3.py - For importing mp3 data from the older version of this bgneal@48: website. bgneal@48: bgneal@48: """ bgneal@48: import datetime bgneal@48: bgneal@48: from django.core.management.base import LabelCommand bgneal@48: from django.utils import simplejson as json bgneal@48: from django.utils.html import linebreaks bgneal@48: bgneal@48: from mp3.models import Collection, Song bgneal@48: bgneal@48: bgneal@48: class Command(LabelCommand): bgneal@48: args = '' bgneal@48: help = 'Imports older mp3 & mp3 sets in JSON format' bgneal@48: bgneal@48: collections = {} bgneal@48: bgneal@48: def handle_label(self, filename, **options): bgneal@48: """ bgneal@48: Process the file of older mp3 & mp3 sets in JSON. Convert to the new bgneal@48: model scheme. bgneal@48: bgneal@48: """ bgneal@48: with open(filename, 'rb') as f: bgneal@48: items = json.load(f) bgneal@48: bgneal@48: for item in items: bgneal@48: if item['model'] == 'band.mp3_set': bgneal@48: self.process_mp3_set(item) bgneal@48: bgneal@48: for item in items: bgneal@48: if item['model'] == 'band.mp3': bgneal@48: self.process_mp3(item) bgneal@48: bgneal@48: def process_mp3_set(self, item): bgneal@48: bgneal@48: fields = item['fields'] bgneal@48: bgneal@48: description = linebreaks(fields['text'].strip()) bgneal@48: bgneal@48: coll = Collection( bgneal@48: id=item['pk'], bgneal@48: title=fields['title'].strip(), bgneal@48: date_added=datetime.datetime.strptime(fields['date'], '%Y-%m-%d'), bgneal@48: description=description) bgneal@48: bgneal@48: coll.save() bgneal@48: self.collections[coll.pk] = coll bgneal@48: bgneal@48: def process_mp3(self, item): bgneal@48: bgneal@48: fields = item['fields'] bgneal@48: bgneal@48: song = Song( bgneal@48: id=item['pk'], bgneal@48: title=fields['title'].strip(), bgneal@48: description=fields['desc'].strip(), bgneal@48: file=fields['file'], bgneal@48: collection=self.collections[fields['mp3_set']], bgneal@48: ) bgneal@48: song.save()