annotate legacy/management/commands/import_old_download_comments.py @ 655:d9d6b4b8bab7

Update fabric files for new server.
author Brian Neal <bgneal@gmail.com>
date Sat, 11 May 2013 12:56:41 -0500
parents ee87ea74d46b
children
rev   line source
bgneal@412 1 """
bgneal@412 2 import_old_download_comments.py - For importing download comments from SG101 1.0
bgneal@412 3 as csv files.
bgneal@412 4
bgneal@412 5 """
bgneal@412 6 from __future__ import with_statement
bgneal@412 7 import csv
bgneal@412 8 from datetime import datetime
bgneal@412 9
bgneal@412 10 from django.core.management.base import LabelCommand, CommandError
bgneal@412 11 from django.contrib.auth.models import User
bgneal@412 12 from django.contrib.contenttypes.models import ContentType
bgneal@412 13
bgneal@412 14 from downloads.models import Download, VoteRecord
bgneal@412 15 from comments.models import Comment
bgneal@412 16 from legacy.html2md import MarkdownWriter
bgneal@412 17 import legacy.data
bgneal@412 18
bgneal@412 19
bgneal@412 20 class Command(LabelCommand):
bgneal@412 21 args = '<filename filename ...>'
bgneal@412 22 help = 'Imports download comments from the old database in CSV format'
bgneal@412 23 md_writer = MarkdownWriter()
bgneal@412 24
bgneal@412 25 def handle_label(self, filename, **options):
bgneal@412 26 """
bgneal@412 27 Process each line in the CSV file given by filename by
bgneal@412 28 creating a new object and saving it to the database.
bgneal@412 29
bgneal@412 30 """
bgneal@412 31 try:
bgneal@412 32 with open(filename, "rb") as f:
bgneal@412 33 self.reader = csv.DictReader(f)
bgneal@412 34 try:
bgneal@412 35 for row in self.reader:
bgneal@412 36 self.process_row(row)
bgneal@412 37 except csv.Error, e:
bgneal@412 38 raise CommandError("CSV error: %s %s %s" % (
bgneal@412 39 filename, self.reader.line_num, e))
bgneal@412 40
bgneal@412 41 except IOError:
bgneal@412 42 raise CommandError("Could not open file: %s" % filename)
bgneal@412 43
bgneal@412 44 def process_row(self, row):
bgneal@412 45 """
bgneal@412 46 Process one row from the CSV file: create an object for the row
bgneal@412 47 and save it in the database.
bgneal@412 48
bgneal@412 49 """
bgneal@412 50 dl_id = int(row['ratinglid'])
bgneal@412 51 if dl_id in (1, 2, 3, 4):
bgneal@412 52 return
bgneal@412 53
bgneal@412 54 try:
bgneal@412 55 dl = Download.objects.get(pk=dl_id)
bgneal@412 56 except Download.DoesNotExist:
bgneal@412 57 return
bgneal@412 58
bgneal@412 59 try:
bgneal@412 60 user = User.objects.get(username=row['ratinguser'])
bgneal@412 61 except User.DoesNotExist:
bgneal@535 62 old_name = row['ratinguser'].lower()
bgneal@412 63 try:
bgneal@412 64 user = User.objects.get(
bgneal@535 65 username=legacy.data.KNOWN_USERNAME_CHANGES[old_name])
bgneal@412 66 except (User.DoesNotExist, KeyError):
bgneal@412 67 return
bgneal@412 68
bgneal@412 69 vote_date = datetime.strptime(row['ratingtimestamp'], "%Y-%m-%d %H:%M:%S")
bgneal@412 70
bgneal@412 71 comment_text = row['ratingcomments'].decode('latin-1').strip()
bgneal@412 72 if comment_text:
bgneal@412 73 comment = Comment(
bgneal@412 74 content_type=ContentType.objects.get_for_model(dl),
bgneal@412 75 object_id=dl.id,
bgneal@412 76 user=user,
bgneal@412 77 comment=comment_text,
bgneal@412 78 creation_date=vote_date,
bgneal@412 79 ip_address = row['ratinghostname'],
bgneal@412 80 is_public = True,
bgneal@412 81 is_removed = False,
bgneal@412 82 )
bgneal@412 83 comment.save()
bgneal@412 84
bgneal@412 85 vr = VoteRecord(download=dl, user=user, vote_date=vote_date)
bgneal@412 86 vr.save()
bgneal@412 87
bgneal@412 88 def to_markdown(self, s):
bgneal@412 89 self.md_writer.reset()
bgneal@412 90 self.md_writer.feed(s)
bgneal@412 91 return self.md_writer.markdown()