comparison gpp/legacy/management/commands/import_old_links.py @ 294:254db4cb6a86

Changes / scripts to import forums. Other tweaks and moving other import scripts to the legacy application.
author Brian Neal <bgneal@gmail.com>
date Wed, 05 Jan 2011 04:09:35 +0000
parents
children 0bf5a5677067
comparison
equal deleted inserted replaced
293:c92fb89dbc7d 294:254db4cb6a86
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.default_user = User.objects.get(pk=2)
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 get_user(self, username):
59 """
60 Return the user object for the given username.
61 If the user cannot be found, self.default_user is returned.
62
63 """
64 try:
65 return User.objects.get(username=username)
66 except User.DoesNotExist:
67 return self.default_user
68
69 def process_row(self, row):
70 """
71 Process one row from the CSV file: create an object for the row
72 and save it in the database.
73
74 """
75 link = Link(category=self.get_category(row),
76 title=row['title'].decode('latin-1'),
77 url=row['url'].decode('latin-1'),
78 description=row['description'].decode('latin-1'),
79 user=self.get_user(row['submitter']),
80 date_added=datetime.datetime.strptime(row['date'], "%Y-%m-%d %H:%M:%S"),
81 hits=int(row['hits']),
82 is_public=True)
83 link.save()