bgneal@294: """ bgneal@294: import_old_links.py - For importing links from SG101 1.0 as csv files. bgneal@294: """ bgneal@333: from __future__ import with_statement bgneal@294: import csv bgneal@294: import datetime bgneal@294: bgneal@294: from django.core.management.base import LabelCommand, CommandError bgneal@294: from django.contrib.auth.models import User bgneal@294: bgneal@294: from weblinks.models import Link, Category bgneal@294: bgneal@294: bgneal@294: class Command(LabelCommand): bgneal@294: args = '' bgneal@294: help = 'Imports weblinks from the old database in CSV format' bgneal@294: bgneal@294: def handle_label(self, filename, **options): bgneal@294: """ bgneal@294: Process each line in the CSV file given by filename by bgneal@294: creating a new weblink object and saving it to the database. bgneal@294: bgneal@294: """ bgneal@294: self.cats = {} bgneal@294: try: bgneal@294: self.default_user = User.objects.get(pk=2) bgneal@294: except User.DoesNotExist: bgneal@294: raise CommandError("Need a default user with pk=1") bgneal@294: bgneal@294: try: bgneal@294: with open(filename, "rb") as f: bgneal@294: self.reader = csv.DictReader(f) bgneal@294: try: bgneal@294: for row in self.reader: bgneal@294: self.process_row(row) bgneal@294: except csv.Error, e: bgneal@294: raise CommandError("CSV error: %s %s %s" % ( bgneal@294: filename, self.reader.line_num, e)) bgneal@294: bgneal@294: except IOError: bgneal@294: raise CommandError("Could not open file: %s" % filename) bgneal@294: bgneal@294: def get_category(self, row): bgneal@294: """ bgneal@294: Return the Category object for the row. bgneal@294: bgneal@294: """ bgneal@294: cat_id = row['cid'] bgneal@294: if cat_id not in self.cats: bgneal@294: try: bgneal@294: cat = Category.objects.get(pk=cat_id) bgneal@294: except Category.DoesNotExist: bgneal@294: raise CommandError("Category does not exist: %s on line %s" % ( bgneal@294: cat_id, self.reader.line_num)) bgneal@294: else: bgneal@294: self.cats[cat_id] = cat bgneal@294: return self.cats[cat_id] bgneal@294: bgneal@294: def get_user(self, username): bgneal@294: """ bgneal@294: Return the user object for the given username. bgneal@294: If the user cannot be found, self.default_user is returned. bgneal@294: bgneal@294: """ bgneal@294: try: bgneal@294: return User.objects.get(username=username) bgneal@294: except User.DoesNotExist: bgneal@294: return self.default_user bgneal@294: bgneal@294: def process_row(self, row): bgneal@294: """ bgneal@294: Process one row from the CSV file: create an object for the row bgneal@294: and save it in the database. bgneal@294: bgneal@294: """ bgneal@294: link = Link(category=self.get_category(row), bgneal@294: title=row['title'].decode('latin-1'), bgneal@294: url=row['url'].decode('latin-1'), bgneal@294: description=row['description'].decode('latin-1'), bgneal@294: user=self.get_user(row['submitter']), bgneal@294: date_added=datetime.datetime.strptime(row['date'], "%Y-%m-%d %H:%M:%S"), bgneal@294: hits=int(row['hits']), bgneal@294: is_public=True) bgneal@294: link.save()