bgneal@294: """ bgneal@294: import_old_topics.py - For importing forum topics (threads) from SG101 1.0 as bgneal@294: csv files. bgneal@294: bgneal@294: """ bgneal@294: from __future__ import with_statement bgneal@294: import csv bgneal@294: import optparse bgneal@294: import sys bgneal@294: from datetime 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 forums.models import Forum, Topic bgneal@294: from legacy.phpbb import unescape bgneal@294: bgneal@294: bgneal@294: class Command(LabelCommand): bgneal@294: args = '' bgneal@294: help = 'Imports forum topics from the old database in CSV format' bgneal@294: option_list = LabelCommand.option_list + ( bgneal@294: optparse.make_option("-p", "--progress", action="store_true", bgneal@294: help="Output a . after every 20 topics to show progress"), bgneal@294: ) 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 topic. bgneal@294: bgneal@294: """ bgneal@294: self.show_progress = options.get('progress') bgneal@294: self.users = {} bgneal@294: bgneal@294: # Create a mapping from the old database's forums to our bgneal@294: # forums bgneal@294: self.forums = {} bgneal@294: try: bgneal@294: self.forums[2] = Forum.objects.get(slug='suggestion-box') bgneal@294: self.forums[3] = Forum.objects.get(slug='surf-music') bgneal@294: self.forums[4] = Forum.objects.get(slug='surf-musician') bgneal@294: self.forums[5] = Forum.objects.get(slug='gear') bgneal@294: self.forums[6] = Forum.objects.get(slug='recording-corner') bgneal@294: self.forums[7] = Forum.objects.get(slug='shallow-end') bgneal@294: self.forums[8] = Forum.objects.get(slug='surfguitar101-website') bgneal@294: self.forums[9] = Forum.objects.get(id=15) bgneal@294: self.forums[10] = Forum.objects.get(slug='for-sale-trade') bgneal@294: self.forums[11] = Forum.objects.get(slug='musicians-gigs-wanted') bgneal@294: self.forums[12] = Forum.objects.get(slug='surf-videos') bgneal@294: self.forums[13] = Forum.objects.get(slug='sg101-podcast') bgneal@294: self.forums[14] = Forum.objects.get(slug='gigs') bgneal@294: self.forums[15] = Forum.objects.get(slug='music-reviews') bgneal@294: self.forums[18] = Forum.objects.get(slug='best-sg101') bgneal@294: except Forum.DoesNotExist: bgneal@294: sys.exit("Forum does not exist; check forum mapping.") bgneal@294: bgneal@294: try: bgneal@294: with open(filename, "rb") as f: bgneal@294: self.reader = csv.DictReader(f) bgneal@294: num_rows = 0 bgneal@294: try: bgneal@294: for row in self.reader: bgneal@294: self.process_row(row) bgneal@294: num_rows += 1 bgneal@294: if self.show_progress and num_rows % 20 == 0: bgneal@294: sys.stdout.write('.') bgneal@294: sys.stdout.flush() 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: print bgneal@294: bgneal@294: except IOError: bgneal@294: raise CommandError("Could not open file: %s" % filename) bgneal@294: bgneal@294: def process_row(self, row): bgneal@294: """ bgneal@294: Process one row from the CSV file: create a Story object for bgneal@294: the row and save it in the database. bgneal@294: bgneal@294: """ bgneal@294: row = dict((k, v if v != 'NULL' else '') for k, v in row.iteritems()) bgneal@294: bgneal@294: if row['topic_moved_id'] != '0': bgneal@294: return bgneal@294: bgneal@294: try: bgneal@294: user = User.objects.get(id=int(row['topic_poster'])) bgneal@294: except User.DoesNotExist: bgneal@294: print "Could not find user %s for topic %s; skipping." % ( bgneal@294: row['topic_poster'], row['topic_id']) bgneal@294: return bgneal@294: bgneal@294: creation_date = datetime.fromtimestamp(float(row['topic_time'])) bgneal@294: bgneal@294: title = row['topic_title'].decode('latin-1', 'replace') bgneal@294: bgneal@294: try: bgneal@294: forum = self.forums[int(row['forum_id'])] bgneal@294: except KeyError: bgneal@294: print 'skipping topic "%s"' % title bgneal@294: return bgneal@294: bgneal@294: topic = Topic(id=int(row['topic_id']), bgneal@294: forum=forum, bgneal@294: name=unescape(title), bgneal@294: creation_date=creation_date, bgneal@294: user=user, bgneal@294: view_count=int(row['topic_views']), bgneal@294: sticky=(int(row['topic_type']) != 0), bgneal@294: locked=(int(row['topic_status']) != 0), bgneal@294: update_date=creation_date) bgneal@294: bgneal@294: topic.save() bgneal@294: