Mercurial > public > sg101
comparison legacy/management/commands/import_old_news_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_news_comments.py@4021ea1045f7 |
children |
comparison
equal
deleted
inserted
replaced
580:c525f3e0b5d0 | 581:ee87ea74d46b |
---|---|
1 """ | |
2 import_old_news_comments.py - For importing comments on news stories from SG101 1.0 as csv files. | |
3 """ | |
4 from __future__ import with_statement | |
5 import csv | |
6 import optparse | |
7 import sys | |
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 comments.models import Comment | |
15 from news.models import Story | |
16 import legacy.data | |
17 from legacy.html2md import MarkdownWriter | |
18 | |
19 | |
20 class Command(LabelCommand): | |
21 args = '<filename filename ...>' | |
22 help = 'Imports news story comments from the old database in CSV format' | |
23 option_list = LabelCommand.option_list + ( | |
24 optparse.make_option("-p", "--progress", action="store_true", | |
25 help="Output a . after every 20 comments to show progress"), | |
26 ) | |
27 md_writer = MarkdownWriter() | |
28 | |
29 def handle_label(self, filename, **options): | |
30 """ | |
31 Process each line in the CSV file given by filename by | |
32 creating a new story comment. | |
33 | |
34 """ | |
35 self.show_progress = options.get('progress') | |
36 self.users = {} | |
37 | |
38 try: | |
39 with open(filename, "rb") as f: | |
40 self.reader = csv.DictReader(f) | |
41 num_rows = 0 | |
42 try: | |
43 for row in self.reader: | |
44 self.process_row(row) | |
45 num_rows += 1 | |
46 if self.show_progress and num_rows % 20 == 0: | |
47 sys.stdout.write('.') | |
48 sys.stdout.flush() | |
49 except csv.Error, e: | |
50 raise CommandError("CSV error: %s %s %s" % ( | |
51 filename, self.reader.line_num, e)) | |
52 | |
53 print | |
54 | |
55 except IOError: | |
56 raise CommandError("Could not open file: %s" % filename) | |
57 | |
58 def process_row(self, row): | |
59 """ | |
60 Process one row from the CSV file: create a Comment object for | |
61 the row and save it in the database. | |
62 | |
63 """ | |
64 row = dict((k, v if v != 'NULL' else '') for k, v in row.iteritems()) | |
65 | |
66 try: | |
67 user = self._get_user(row['name']) | |
68 except User.DoesNotExist: | |
69 print "Could not find user %s for comment %s; skipping." % ( | |
70 row['name'], row['tid']) | |
71 return | |
72 | |
73 try: | |
74 story = Story.objects.get(id=int(row['sid'])) | |
75 except Story.DoesNotExist: | |
76 print "Could not find story %s for comment %s; skipping." % ( | |
77 row['sid'], row['tid']) | |
78 return | |
79 | |
80 comment = Comment( | |
81 id=int(row['tid']), | |
82 content_type = ContentType.objects.get_for_model(story), | |
83 object_id = story.id, | |
84 user = user, | |
85 comment = self.to_markdown(row['comment']), | |
86 creation_date = datetime.strptime(row['date'], "%Y-%m-%d %H:%M:%S"), | |
87 ip_address = row['host_name'], | |
88 is_public = True, | |
89 is_removed = False, | |
90 ) | |
91 | |
92 comment.save() | |
93 | |
94 def _get_user(self, username): | |
95 """ | |
96 Returns the user object with the given username. | |
97 Throws User.DoesNotExist if not found. | |
98 | |
99 """ | |
100 try: | |
101 return self.users[username] | |
102 except KeyError: | |
103 pass | |
104 | |
105 try: | |
106 user = User.objects.get(username=username) | |
107 except User.DoesNotExist: | |
108 old_name = username.lower() | |
109 try: | |
110 user = User.objects.get( | |
111 username=legacy.data.KNOWN_USERNAME_CHANGES[old_name]) | |
112 except KeyError: | |
113 raise User.DoesNotExist | |
114 | |
115 self.users[username] = user | |
116 return user | |
117 | |
118 def to_markdown(self, s): | |
119 self.md_writer.reset() | |
120 | |
121 if not isinstance(s, unicode): | |
122 s = s.decode('latin-1', 'replace') | |
123 | |
124 self.md_writer.feed(s) | |
125 return self.md_writer.markdown() |