bgneal@291: """ bgneal@291: import_old_news.py - For importing news stories from SG101 1.0 as csv files. bgneal@291: """ bgneal@291: from __future__ import with_statement bgneal@291: import csv bgneal@291: import optparse bgneal@291: import sys bgneal@291: from datetime import datetime bgneal@291: bgneal@291: from django.core.management.base import LabelCommand, CommandError bgneal@291: from django.contrib.auth.models import User bgneal@291: bgneal@291: from news.models import Category, Story bgneal@291: from legacy.phpbb import unescape bgneal@291: bgneal@291: KNOWN_USERNAME_CHANGES = { bgneal@291: 'cavefishbutchdelux': 'butchdelux', bgneal@291: 'Findicator1': 'WaveOhhh', bgneal@291: } bgneal@291: bgneal@291: class Command(LabelCommand): bgneal@291: args = '' bgneal@291: help = 'Imports news stories from the old database in CSV format' bgneal@291: option_list = LabelCommand.option_list + ( bgneal@291: optparse.make_option("-p", "--progress", action="store_true", bgneal@291: help="Output a . after every 20 stories to show progress"), bgneal@291: ) bgneal@291: bgneal@291: def handle_label(self, filename, **options): bgneal@291: """ bgneal@291: Process each line in the CSV file given by filename by bgneal@291: creating a new story. bgneal@291: bgneal@291: """ bgneal@291: self.show_progress = options.get('progress') bgneal@291: self.users = {} bgneal@291: bgneal@291: # Create a mapping from the old database's topics to our bgneal@291: # Categories. bgneal@291: self.topics = {} bgneal@291: try: bgneal@291: self.topics[2] = Category.objects.get(slug='site-news') bgneal@291: self.topics[3] = Category.objects.get(slug='bands') bgneal@291: self.topics[4] = Category.objects.get(slug='show-announcements') bgneal@291: self.topics[5] = Category.objects.get(slug='show-reports') bgneal@291: self.topics[6] = Category.objects.get(slug='gear') bgneal@291: self.topics[7] = Category.objects.get(slug='reviews') bgneal@291: self.topics[8] = Category.objects.get(slug='surf-scene-news') bgneal@291: self.topics[9] = Category.objects.get(slug='articles') bgneal@291: self.topics[10] = Category.objects.get(slug='interviews') bgneal@291: self.topics[11] = Category.objects.get(slug='tablature') bgneal@291: self.topics[12] = Category.objects.get(slug='featured-videos') bgneal@291: except Category.DoesNotExist: bgneal@291: sys.exit("Category does not exist; check topic mapping.") bgneal@291: bgneal@291: try: bgneal@291: with open(filename, "rb") as f: bgneal@291: self.reader = csv.DictReader(f) bgneal@291: num_rows = 0 bgneal@291: try: bgneal@291: for row in self.reader: bgneal@291: self.process_row(row) bgneal@291: num_rows += 1 bgneal@291: if self.show_progress and num_rows % 20 == 0: bgneal@291: sys.stdout.write('.') bgneal@291: sys.stdout.flush() bgneal@291: except csv.Error, e: bgneal@291: raise CommandError("CSV error: %s %s %s" % ( bgneal@291: filename, self.reader.line_num, e)) bgneal@291: bgneal@291: print bgneal@291: bgneal@291: except IOError: bgneal@291: raise CommandError("Could not open file: %s" % filename) bgneal@291: bgneal@291: def process_row(self, row): bgneal@291: """ bgneal@291: Process one row from the CSV file: create a Story object for bgneal@291: the row and save it in the database. bgneal@291: bgneal@291: """ bgneal@291: row = dict((k, v if v != 'NULL' else '') for k, v in row.iteritems()) bgneal@291: bgneal@291: try: bgneal@291: submitter = self._get_user(row['informant']) bgneal@291: except User.DoesNotExist: bgneal@291: print "Could not find user %s for story %s; skipping." % ( bgneal@291: row['informant'], row['sid']) bgneal@291: return bgneal@291: bgneal@291: story = Story(id=int(row['sid']), bgneal@291: title=unescape(row['title']), bgneal@291: submitter=submitter, bgneal@291: category=self.topics[int(row['topic'])], bgneal@291: short_text=row['hometext'], bgneal@291: long_text=row['bodytext'], bgneal@291: date_submitted=datetime.strptime(row['time'], "%Y-%m-%d %H:%M:%S"), bgneal@291: allow_comments=True) bgneal@291: bgneal@291: story.save() bgneal@291: bgneal@291: def _get_user(self, username): bgneal@291: """ bgneal@291: Returns the user object with the given username. bgneal@291: Throws User.DoesNotExist if not found. bgneal@291: bgneal@291: """ bgneal@291: try: bgneal@291: return self.users[username] bgneal@291: except KeyError: bgneal@291: pass bgneal@291: bgneal@291: try: bgneal@291: user = User.objects.get(username=username) bgneal@291: except User.DoesNotExist: bgneal@291: try: bgneal@291: user = User.objects.get(username=KNOWN_USERNAME_CHANGES[username]) bgneal@291: except KeyError: bgneal@291: raise User.DoesNotExist bgneal@291: bgneal@291: self.users[username] = user bgneal@291: return user