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