bgneal@294
|
1 """
|
bgneal@294
|
2 import_old_links.py - For importing links from SG101 1.0 as csv files.
|
bgneal@294
|
3 """
|
bgneal@294
|
4 import csv
|
bgneal@294
|
5 import datetime
|
bgneal@294
|
6
|
bgneal@294
|
7 from django.core.management.base import LabelCommand, CommandError
|
bgneal@294
|
8 from django.contrib.auth.models import User
|
bgneal@294
|
9
|
bgneal@294
|
10 from weblinks.models import Link, Category
|
bgneal@294
|
11
|
bgneal@294
|
12
|
bgneal@294
|
13 class Command(LabelCommand):
|
bgneal@294
|
14 args = '<filename filename ...>'
|
bgneal@294
|
15 help = 'Imports weblinks from the old database in CSV format'
|
bgneal@294
|
16
|
bgneal@294
|
17 def handle_label(self, filename, **options):
|
bgneal@294
|
18 """
|
bgneal@294
|
19 Process each line in the CSV file given by filename by
|
bgneal@294
|
20 creating a new weblink object and saving it to the database.
|
bgneal@294
|
21
|
bgneal@294
|
22 """
|
bgneal@294
|
23 self.cats = {}
|
bgneal@294
|
24 try:
|
bgneal@294
|
25 self.default_user = User.objects.get(pk=2)
|
bgneal@294
|
26 except User.DoesNotExist:
|
bgneal@294
|
27 raise CommandError("Need a default user with pk=1")
|
bgneal@294
|
28
|
bgneal@294
|
29 try:
|
bgneal@294
|
30 with open(filename, "rb") as f:
|
bgneal@294
|
31 self.reader = csv.DictReader(f)
|
bgneal@294
|
32 try:
|
bgneal@294
|
33 for row in self.reader:
|
bgneal@294
|
34 self.process_row(row)
|
bgneal@294
|
35 except csv.Error, e:
|
bgneal@294
|
36 raise CommandError("CSV error: %s %s %s" % (
|
bgneal@294
|
37 filename, self.reader.line_num, e))
|
bgneal@294
|
38
|
bgneal@294
|
39 except IOError:
|
bgneal@294
|
40 raise CommandError("Could not open file: %s" % filename)
|
bgneal@294
|
41
|
bgneal@294
|
42 def get_category(self, row):
|
bgneal@294
|
43 """
|
bgneal@294
|
44 Return the Category object for the row.
|
bgneal@294
|
45
|
bgneal@294
|
46 """
|
bgneal@294
|
47 cat_id = row['cid']
|
bgneal@294
|
48 if cat_id not in self.cats:
|
bgneal@294
|
49 try:
|
bgneal@294
|
50 cat = Category.objects.get(pk=cat_id)
|
bgneal@294
|
51 except Category.DoesNotExist:
|
bgneal@294
|
52 raise CommandError("Category does not exist: %s on line %s" % (
|
bgneal@294
|
53 cat_id, self.reader.line_num))
|
bgneal@294
|
54 else:
|
bgneal@294
|
55 self.cats[cat_id] = cat
|
bgneal@294
|
56 return self.cats[cat_id]
|
bgneal@294
|
57
|
bgneal@294
|
58 def get_user(self, username):
|
bgneal@294
|
59 """
|
bgneal@294
|
60 Return the user object for the given username.
|
bgneal@294
|
61 If the user cannot be found, self.default_user is returned.
|
bgneal@294
|
62
|
bgneal@294
|
63 """
|
bgneal@294
|
64 try:
|
bgneal@294
|
65 return User.objects.get(username=username)
|
bgneal@294
|
66 except User.DoesNotExist:
|
bgneal@294
|
67 return self.default_user
|
bgneal@294
|
68
|
bgneal@294
|
69 def process_row(self, row):
|
bgneal@294
|
70 """
|
bgneal@294
|
71 Process one row from the CSV file: create an object for the row
|
bgneal@294
|
72 and save it in the database.
|
bgneal@294
|
73
|
bgneal@294
|
74 """
|
bgneal@294
|
75 link = Link(category=self.get_category(row),
|
bgneal@294
|
76 title=row['title'].decode('latin-1'),
|
bgneal@294
|
77 url=row['url'].decode('latin-1'),
|
bgneal@294
|
78 description=row['description'].decode('latin-1'),
|
bgneal@294
|
79 user=self.get_user(row['submitter']),
|
bgneal@294
|
80 date_added=datetime.datetime.strptime(row['date'], "%Y-%m-%d %H:%M:%S"),
|
bgneal@294
|
81 hits=int(row['hits']),
|
bgneal@294
|
82 is_public=True)
|
bgneal@294
|
83 link.save()
|