Mercurial > public > madeira
comparison videos/management/commands/import_old_videos.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/videos/management/commands/import_old_videos.py@5913ddcebea4 |
children | b7cdfdde3999 |
comparison
equal
deleted
inserted
replaced
70:f26cdda0ad8b | 71:e2868ad47a1e |
---|---|
1 """ | |
2 import_old_videos.py - For importing video 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 videos.models import Collection, Video | |
13 | |
14 | |
15 class Command(LabelCommand): | |
16 args = '<filename filename ...>' | |
17 help = 'Imports older video & video sets in JSON format' | |
18 | |
19 collections = {} | |
20 | |
21 def handle_label(self, filename, **options): | |
22 """ | |
23 Process the file of older video & video 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.video_set': | |
32 self.process_set(item) | |
33 | |
34 for item in items: | |
35 if item['model'] == 'band.video': | |
36 self.process_video(item) | |
37 | |
38 def process_set(self, item): | |
39 | |
40 fields = item['fields'] | |
41 | |
42 description = linebreaks(fields['text'].strip()) | |
43 | |
44 # there are several sets with the same date, so to get the ordering | |
45 # right, add the pk as seconds. | |
46 | |
47 date_added = datetime.datetime.strptime(fields['date'], '%Y-%m-%d') | |
48 date_added += datetime.timedelta(seconds=int(item['pk'])) | |
49 | |
50 coll = Collection( | |
51 id=item['pk'], | |
52 title=fields['title'].strip(), | |
53 date_added=date_added, | |
54 description=description) | |
55 | |
56 coll.save() | |
57 self.collections[coll.pk] = coll | |
58 | |
59 def process_video(self, item): | |
60 | |
61 fields = item['fields'] | |
62 | |
63 video = Video( | |
64 id=item['pk'], | |
65 title=fields['title'].strip(), | |
66 embed_code=fields['embed_code'], | |
67 collection=self.collections[fields['video_set']], | |
68 ) | |
69 video.save() |