annotate gpp/legacy/management/commands/import_old_potd_comments.py @ 535:4021ea1045f7

Adding import commands for the 1.0 POTD & comments.
author Brian Neal <bgneal@gmail.com>
date Sat, 31 Dec 2011 21:34:17 +0000
parents
children eac0ce5e137d
rev   line source
bgneal@535 1 """
bgneal@535 2 import_old_potd_comments.py - For importing comments on POTD's from SG101 1.0
bgneal@535 3 as csv files.
bgneal@535 4
bgneal@535 5 """
bgneal@535 6 from __future__ import with_statement
bgneal@535 7 import csv
bgneal@535 8 import optparse
bgneal@535 9 import sys
bgneal@535 10 from datetime import datetime
bgneal@535 11
bgneal@535 12 from django.core.management.base import LabelCommand, CommandError
bgneal@535 13 from django.contrib.auth.models import User
bgneal@535 14 from django.contrib.contenttypes.models import ContentType
bgneal@535 15
bgneal@535 16 from comments.models import Comment
bgneal@535 17 from potd.models import Photo
bgneal@535 18 import legacy.data
bgneal@535 19 from legacy.html2md import MarkdownWriter
bgneal@535 20
bgneal@535 21
bgneal@535 22 PHOTO_ID_OFFSET = 100
bgneal@535 23 ID_OFFSET = 3000
bgneal@535 24
bgneal@535 25
bgneal@535 26 class Command(LabelCommand):
bgneal@535 27 args = '<filename filename ...>'
bgneal@535 28 help = 'Imports POTD comments from the old database in CSV format'
bgneal@535 29 option_list = LabelCommand.option_list + (
bgneal@535 30 optparse.make_option("-p", "--progress", action="store_true",
bgneal@535 31 help="Output a . after every 20 items to show progress"),
bgneal@535 32 )
bgneal@535 33 md_writer = MarkdownWriter()
bgneal@535 34
bgneal@535 35 def handle_label(self, filename, **options):
bgneal@535 36 """
bgneal@535 37 Process each line in the CSV file given by filename by
bgneal@535 38 creating a new POTD comment.
bgneal@535 39
bgneal@535 40 """
bgneal@535 41 self.show_progress = options.get('progress')
bgneal@535 42 self.users = {}
bgneal@535 43
bgneal@535 44 try:
bgneal@535 45 with open(filename, "rb") as f:
bgneal@535 46 self.reader = csv.DictReader(f)
bgneal@535 47 num_rows = 0
bgneal@535 48 try:
bgneal@535 49 for row in self.reader:
bgneal@535 50 self.process_row(row)
bgneal@535 51 num_rows += 1
bgneal@535 52 if self.show_progress and num_rows % 20 == 0:
bgneal@535 53 sys.stdout.write('.')
bgneal@535 54 sys.stdout.flush()
bgneal@535 55 except csv.Error, e:
bgneal@535 56 raise CommandError("CSV error: %s %s %s" % (
bgneal@535 57 filename, self.reader.line_num, e))
bgneal@535 58
bgneal@535 59 print
bgneal@535 60
bgneal@535 61 except IOError:
bgneal@535 62 raise CommandError("Could not open file: %s" % filename)
bgneal@535 63
bgneal@535 64 def process_row(self, row):
bgneal@535 65 """
bgneal@535 66 Process one row from the CSV file: create a Comment object for
bgneal@535 67 the row and save it in the database.
bgneal@535 68
bgneal@535 69 """
bgneal@535 70 try:
bgneal@535 71 user = self._get_user(row['username'].decode('latin-1'))
bgneal@535 72 except User.DoesNotExist:
bgneal@535 73 print "Could not find user %s for comment %s; skipping." % (
bgneal@535 74 row['username'], row['cid'])
bgneal@535 75 return
bgneal@535 76
bgneal@535 77 pid = int(row['pid']) + PHOTO_ID_OFFSET
bgneal@535 78 try:
bgneal@535 79 photo = Photo.objects.get(id=pid)
bgneal@535 80 except Photo.DoesNotExist:
bgneal@535 81 print "Could not find photo %s for comment %s; skipping." % (
bgneal@535 82 pid, row['cid'])
bgneal@535 83 return
bgneal@535 84
bgneal@535 85 comment = Comment(
bgneal@535 86 id=int(row['cid']) + ID_OFFSET,
bgneal@535 87 content_type=ContentType.objects.get_for_model(photo),
bgneal@535 88 object_id=photo.id,
bgneal@535 89 user=user,
bgneal@535 90 comment=self.to_markdown(row['comment'].decode('latin-1')),
bgneal@535 91 creation_date=datetime.strptime(row['date'], "%Y-%m-%d %H:%M:%S"),
bgneal@535 92 ip_address='192.0.2.0', # TEST-NET
bgneal@535 93 is_public=True,
bgneal@535 94 is_removed=False,
bgneal@535 95 )
bgneal@535 96
bgneal@535 97 comment.save()
bgneal@535 98
bgneal@535 99 def _get_user(self, username):
bgneal@535 100 """
bgneal@535 101 Returns the user object with the given username.
bgneal@535 102 Throws User.DoesNotExist if not found.
bgneal@535 103
bgneal@535 104 """
bgneal@535 105 try:
bgneal@535 106 return self.users[username]
bgneal@535 107 except KeyError:
bgneal@535 108 pass
bgneal@535 109
bgneal@535 110 try:
bgneal@535 111 user = User.objects.get(username=username)
bgneal@535 112 except User.DoesNotExist:
bgneal@535 113 old_name = username.lower()
bgneal@535 114 try:
bgneal@535 115 user = User.objects.get(
bgneal@535 116 username=legacy.data.KNOWN_USERNAME_CHANGES[old_name])
bgneal@535 117 except KeyError:
bgneal@535 118 raise User.DoesNotExist
bgneal@535 119
bgneal@535 120 self.users[username] = user
bgneal@535 121 return user
bgneal@535 122
bgneal@535 123 def to_markdown(self, s):
bgneal@535 124
bgneal@535 125 s = s.replace('\n', '\n<br />')
bgneal@535 126 self.md_writer.reset()
bgneal@535 127 self.md_writer.feed(s)
bgneal@535 128 return self.md_writer.markdown()