Mercurial > public > sg101
comparison gpp/weblinks/management/commands/import_old_links.py @ 237:1085dc38399e
In support of #92, create management commands to import link and podcast data from the old site in CSV format.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Sat, 11 Sep 2010 21:07:28 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
236:953c71f382df | 237:1085dc38399e |
---|---|
1 """ | |
2 import_old_links.py - For importing links from SG101 1.0 as csv files. | |
3 """ | |
4 import csv | |
5 import datetime | |
6 | |
7 from django.core.management.base import LabelCommand, CommandError | |
8 from django.contrib.auth.models import User | |
9 | |
10 from weblinks.models import Link, Category | |
11 | |
12 | |
13 class Command(LabelCommand): | |
14 args = '<filename filename ...>' | |
15 help = 'Imports weblinks 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 self.cats = {} | |
24 try: | |
25 self.user = User.objects.get(pk=1) | |
26 except User.DoesNotExist: | |
27 raise CommandError("Need a default user with pk=1") | |
28 | |
29 try: | |
30 with open(filename, "rb") as f: | |
31 self.reader = csv.DictReader(f) | |
32 try: | |
33 for row in self.reader: | |
34 self.process_row(row) | |
35 except csv.Error, e: | |
36 raise CommandError("CSV error: %s %s %s" % ( | |
37 filename, self.reader.line_num, e)) | |
38 | |
39 except IOError: | |
40 raise CommandError("Could not open file: %s" % filename) | |
41 | |
42 def get_category(self, row): | |
43 """ | |
44 Return the Category object for the row. | |
45 | |
46 """ | |
47 cat_id = row['cid'] | |
48 if cat_id not in self.cats: | |
49 try: | |
50 cat = Category.objects.get(pk=cat_id) | |
51 except Category.DoesNotExist: | |
52 raise CommandError("Category does not exist: %s on line %s" % ( | |
53 cat_id, self.reader.line_num)) | |
54 else: | |
55 self.cats[cat_id] = cat | |
56 return self.cats[cat_id] | |
57 | |
58 def process_row(self, row): | |
59 """ | |
60 Process one row from the CSV file: create an object for the row | |
61 and save it in the database. | |
62 | |
63 """ | |
64 link = Link(category=self.get_category(row), | |
65 title=row['title'], | |
66 url=row['url'], | |
67 description=row['description'], | |
68 user=self.user, | |
69 date_added=datetime.datetime.strptime(row['date'], "%Y-%m-%d %H:%M:%S"), | |
70 hits=int(row['hits']), | |
71 is_public=True) | |
72 link.save() |