comparison legacy/management/commands/import_old_podcasts.py @ 581:ee87ea74d46b

For Django 1.4, rearranged project structure for new manage.py.
author Brian Neal <bgneal@gmail.com>
date Sat, 05 May 2012 17:10:48 -0500
parents gpp/legacy/management/commands/import_old_podcasts.py@0bf5a5677067
children
comparison
equal deleted inserted replaced
580:c525f3e0b5d0 581:ee87ea74d46b
1 """
2 import_old_podcasts.py - For importing podcasts from SG101 1.0 as csv files.
3 """
4 from __future__ import with_statement
5 import csv
6 import datetime
7
8 from django.core.management.base import LabelCommand, CommandError
9
10 from podcast.models import Channel, Item
11
12
13 class Command(LabelCommand):
14 args = '<filename filename ...>'
15 help = 'Imports podcasts from the old database in CSV format'
16
17 def handle_label(self, filename, **options):
18 """
19 Process each line in the CSV file given by filename by
20 creating a new weblink object and saving it to the database.
21
22 """
23 try:
24 self.channel = Channel.objects.get(pk=1)
25 except Channel.DoesNotExist:
26 raise CommandError("Need a default channel with pk=1")
27
28 try:
29 with open(filename, "rb") as f:
30 self.reader = csv.DictReader(f)
31 try:
32 for row in self.reader:
33 self.process_row(row)
34 except csv.Error, e:
35 raise CommandError("CSV error: %s %s %s" % (
36 filename, self.reader.line_num, e))
37
38 except IOError:
39 raise CommandError("Could not open file: %s" % filename)
40
41 def process_row(self, row):
42 """
43 Process one row from the CSV file: create an object for the row
44 and save it in the database.
45
46 """
47 item = Item(channel=self.channel,
48 title=row['title'],
49 author=row['author'],
50 subtitle=row['subtitle'],
51 summary=row['summary'],
52 enclosure_url=row['enclosure_url'],
53 alt_enclosure_url='',
54 enclosure_length=int(row['enclosure_length']),
55 enclosure_type=row['enclosure_type'],
56 guid=row['guid'],
57 pubdate=datetime.datetime.strptime(row['pubdate'],
58 "%Y-%m-%d %H:%M:%S"),
59 duration=row['duration'],
60 keywords=row['keywords'],
61 explicit=row['explicit'])
62
63 item.save()