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