Mercurial > public > sg101
comparison gpp/legacy/management/commands/import_old_news.py @ 291:a6357f2bcbbc
Added a legacy command to import old news stories.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Fri, 24 Dec 2010 20:45:33 +0000 |
parents | |
children | 2367c4795c92 |
comparison
equal
deleted
inserted
replaced
290:64c188a9d31f | 291:a6357f2bcbbc |
---|---|
1 """ | |
2 import_old_news.py - For importing 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 | |
13 from news.models import Category, Story | |
14 from legacy.phpbb import unescape | |
15 | |
16 KNOWN_USERNAME_CHANGES = { | |
17 'cavefishbutchdelux': 'butchdelux', | |
18 'Findicator1': 'WaveOhhh', | |
19 } | |
20 | |
21 class Command(LabelCommand): | |
22 args = '<filename filename ...>' | |
23 help = 'Imports news stories from the old database in CSV format' | |
24 option_list = LabelCommand.option_list + ( | |
25 optparse.make_option("-p", "--progress", action="store_true", | |
26 help="Output a . after every 20 stories to show progress"), | |
27 ) | |
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. | |
33 | |
34 """ | |
35 self.show_progress = options.get('progress') | |
36 self.users = {} | |
37 | |
38 # Create a mapping from the old database's topics to our | |
39 # Categories. | |
40 self.topics = {} | |
41 try: | |
42 self.topics[2] = Category.objects.get(slug='site-news') | |
43 self.topics[3] = Category.objects.get(slug='bands') | |
44 self.topics[4] = Category.objects.get(slug='show-announcements') | |
45 self.topics[5] = Category.objects.get(slug='show-reports') | |
46 self.topics[6] = Category.objects.get(slug='gear') | |
47 self.topics[7] = Category.objects.get(slug='reviews') | |
48 self.topics[8] = Category.objects.get(slug='surf-scene-news') | |
49 self.topics[9] = Category.objects.get(slug='articles') | |
50 self.topics[10] = Category.objects.get(slug='interviews') | |
51 self.topics[11] = Category.objects.get(slug='tablature') | |
52 self.topics[12] = Category.objects.get(slug='featured-videos') | |
53 except Category.DoesNotExist: | |
54 sys.exit("Category does not exist; check topic mapping.") | |
55 | |
56 try: | |
57 with open(filename, "rb") as f: | |
58 self.reader = csv.DictReader(f) | |
59 num_rows = 0 | |
60 try: | |
61 for row in self.reader: | |
62 self.process_row(row) | |
63 num_rows += 1 | |
64 if self.show_progress and num_rows % 20 == 0: | |
65 sys.stdout.write('.') | |
66 sys.stdout.flush() | |
67 except csv.Error, e: | |
68 raise CommandError("CSV error: %s %s %s" % ( | |
69 filename, self.reader.line_num, e)) | |
70 | |
71 print | |
72 | |
73 except IOError: | |
74 raise CommandError("Could not open file: %s" % filename) | |
75 | |
76 def process_row(self, row): | |
77 """ | |
78 Process one row from the CSV file: create a Story object for | |
79 the row and save it in the database. | |
80 | |
81 """ | |
82 row = dict((k, v if v != 'NULL' else '') for k, v in row.iteritems()) | |
83 | |
84 try: | |
85 submitter = self._get_user(row['informant']) | |
86 except User.DoesNotExist: | |
87 print "Could not find user %s for story %s; skipping." % ( | |
88 row['informant'], row['sid']) | |
89 return | |
90 | |
91 story = Story(id=int(row['sid']), | |
92 title=unescape(row['title']), | |
93 submitter=submitter, | |
94 category=self.topics[int(row['topic'])], | |
95 short_text=row['hometext'], | |
96 long_text=row['bodytext'], | |
97 date_submitted=datetime.strptime(row['time'], "%Y-%m-%d %H:%M:%S"), | |
98 allow_comments=True) | |
99 | |
100 story.save() | |
101 | |
102 def _get_user(self, username): | |
103 """ | |
104 Returns the user object with the given username. | |
105 Throws User.DoesNotExist if not found. | |
106 | |
107 """ | |
108 try: | |
109 return self.users[username] | |
110 except KeyError: | |
111 pass | |
112 | |
113 try: | |
114 user = User.objects.get(username=username) | |
115 except User.DoesNotExist: | |
116 try: | |
117 user = User.objects.get(username=KNOWN_USERNAME_CHANGES[username]) | |
118 except KeyError: | |
119 raise User.DoesNotExist | |
120 | |
121 self.users[username] = user | |
122 return user |