bgneal@412: """ bgneal@412: import_old_download_comments.py - For importing download comments from SG101 1.0 bgneal@412: as csv files. bgneal@412: bgneal@412: """ bgneal@412: from __future__ import with_statement bgneal@412: import csv bgneal@412: from datetime import datetime bgneal@412: bgneal@412: from django.core.management.base import LabelCommand, CommandError bgneal@412: from django.contrib.auth.models import User bgneal@412: from django.contrib.contenttypes.models import ContentType bgneal@412: bgneal@412: from downloads.models import Download, VoteRecord bgneal@412: from comments.models import Comment bgneal@412: from legacy.html2md import MarkdownWriter bgneal@412: import legacy.data bgneal@412: bgneal@412: bgneal@412: class Command(LabelCommand): bgneal@412: args = '' bgneal@412: help = 'Imports download comments from the old database in CSV format' bgneal@412: md_writer = MarkdownWriter() bgneal@412: bgneal@412: def handle_label(self, filename, **options): bgneal@412: """ bgneal@412: Process each line in the CSV file given by filename by bgneal@412: creating a new object and saving it to the database. bgneal@412: bgneal@412: """ bgneal@412: try: bgneal@412: with open(filename, "rb") as f: bgneal@412: self.reader = csv.DictReader(f) bgneal@412: try: bgneal@412: for row in self.reader: bgneal@412: self.process_row(row) bgneal@412: except csv.Error, e: bgneal@412: raise CommandError("CSV error: %s %s %s" % ( bgneal@412: filename, self.reader.line_num, e)) bgneal@412: bgneal@412: except IOError: bgneal@412: raise CommandError("Could not open file: %s" % filename) bgneal@412: bgneal@412: def process_row(self, row): bgneal@412: """ bgneal@412: Process one row from the CSV file: create an object for the row bgneal@412: and save it in the database. bgneal@412: bgneal@412: """ bgneal@412: dl_id = int(row['ratinglid']) bgneal@412: if dl_id in (1, 2, 3, 4): bgneal@412: return bgneal@412: bgneal@412: try: bgneal@412: dl = Download.objects.get(pk=dl_id) bgneal@412: except Download.DoesNotExist: bgneal@412: return bgneal@412: bgneal@412: try: bgneal@412: user = User.objects.get(username=row['ratinguser']) bgneal@412: except User.DoesNotExist: bgneal@412: try: bgneal@412: user = User.objects.get( bgneal@412: username=legacy.data.KNOWN_USERNAME_CHANGES[row['ratinguser']]) bgneal@412: except (User.DoesNotExist, KeyError): bgneal@412: return bgneal@412: bgneal@412: vote_date = datetime.strptime(row['ratingtimestamp'], "%Y-%m-%d %H:%M:%S") bgneal@412: bgneal@412: comment_text = row['ratingcomments'].decode('latin-1').strip() bgneal@412: if comment_text: bgneal@412: comment = Comment( bgneal@412: content_type=ContentType.objects.get_for_model(dl), bgneal@412: object_id=dl.id, bgneal@412: user=user, bgneal@412: comment=comment_text, bgneal@412: creation_date=vote_date, bgneal@412: ip_address = row['ratinghostname'], bgneal@412: is_public = True, bgneal@412: is_removed = False, bgneal@412: ) bgneal@412: comment.save() bgneal@412: bgneal@412: vr = VoteRecord(download=dl, user=user, vote_date=vote_date) bgneal@412: vr.save() bgneal@412: bgneal@412: def to_markdown(self, s): bgneal@412: self.md_writer.reset() bgneal@412: self.md_writer.feed(s) bgneal@412: return self.md_writer.markdown()