Mercurial > public > sg101
comparison legacy/management/commands/import_old_potd.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.py@4021ea1045f7 |
children |
comparison
equal
deleted
inserted
replaced
580:c525f3e0b5d0 | 581:ee87ea74d46b |
---|---|
1 """ | |
2 import_old_potd.py - For importing POTD's from SG101 1.0 as csv files. | |
3 | |
4 """ | |
5 from __future__ import with_statement | |
6 import csv | |
7 import optparse | |
8 import sys | |
9 from datetime import datetime | |
10 | |
11 from django.core.management.base import LabelCommand, CommandError | |
12 from django.contrib.auth.models import User | |
13 | |
14 from potd.models import Photo | |
15 from legacy.phpbb import unescape | |
16 import legacy.data | |
17 | |
18 | |
19 ID_OFFSET = 100 | |
20 | |
21 | |
22 class PathError(Exception): | |
23 pass | |
24 | |
25 def convert_path(old_path): | |
26 """ | |
27 Converts the old POTD path to a new one. | |
28 | |
29 """ | |
30 if old_path.startswith('images/potd/'): | |
31 return "potd/1.0/%s" % old_path[12:] | |
32 else: | |
33 raise PathError("Unknown path %s" % old_path) | |
34 | |
35 | |
36 class Command(LabelCommand): | |
37 args = '<filename filename ...>' | |
38 help = "Imports POTD's from the old database in CSV format" | |
39 option_list = LabelCommand.option_list + ( | |
40 optparse.make_option("-p", "--progress", action="store_true", | |
41 help="Output a . after every 20 items to show progress"), | |
42 ) | |
43 | |
44 def handle_label(self, filename, **options): | |
45 """ | |
46 Process each line in the CSV file given by filename by | |
47 creating a new Photo | |
48 | |
49 """ | |
50 self.show_progress = options.get('progress') | |
51 self.users = {} | |
52 | |
53 try: | |
54 with open(filename, "rb") as f: | |
55 self.reader = csv.DictReader(f) | |
56 num_rows = 0 | |
57 try: | |
58 for row in self.reader: | |
59 self.process_row(row) | |
60 num_rows += 1 | |
61 if self.show_progress and num_rows % 20 == 0: | |
62 sys.stdout.write('.') | |
63 sys.stdout.flush() | |
64 except csv.Error, e: | |
65 raise CommandError("CSV error: %s %s %s" % ( | |
66 filename, self.reader.line_num, e)) | |
67 | |
68 print | |
69 | |
70 except IOError: | |
71 raise CommandError("Could not open file: %s" % filename) | |
72 | |
73 def process_row(self, row): | |
74 """ | |
75 Process one row from the CSV file: create a Photo object for | |
76 the row and save it in the database. | |
77 | |
78 """ | |
79 try: | |
80 submitter = self._get_user(row['submitted_by'].decode('latin-1')) | |
81 except User.DoesNotExist: | |
82 print "Could not find user %s for potd %s; skipping." % ( | |
83 row['submitted_by'], row['pid']) | |
84 return | |
85 | |
86 desc = row['description'].decode('latin-1').replace('\n', '\n<br />') | |
87 | |
88 try: | |
89 photo = Photo( | |
90 id=int(row['pid']) + ID_OFFSET, | |
91 photo=convert_path(row['photo_path']), | |
92 thumb=convert_path(row['thumb_path']), | |
93 caption=unescape(row['title'].decode('latin-1')), | |
94 description=desc, | |
95 user=submitter, | |
96 date_added=datetime.strptime(row['date_added'], | |
97 "%Y-%m-%d %H:%M:%S"), | |
98 potd_count=int(row['chosen_count'])) | |
99 except PathError, ex: | |
100 self.stderr.write("\n%s, skipping\n" % ex) | |
101 return | |
102 | |
103 photo.save() | |
104 | |
105 def _get_user(self, username): | |
106 """ | |
107 Returns the user object with the given username. | |
108 Throws User.DoesNotExist if not found. | |
109 | |
110 """ | |
111 try: | |
112 return self.users[username] | |
113 except KeyError: | |
114 pass | |
115 | |
116 try: | |
117 user = User.objects.get(username=username) | |
118 except User.DoesNotExist: | |
119 old_name = username.lower() | |
120 try: | |
121 user = User.objects.get( | |
122 username=legacy.data.KNOWN_USERNAME_CHANGES[old_name]) | |
123 except KeyError: | |
124 raise User.DoesNotExist | |
125 | |
126 self.users[username] = user | |
127 return user |