annotate gpp/weblinks/management/commands/import_old_links.py @ 274:2a3285e32062

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