comparison gpp/legacy/management/commands/import_old_potd_comments.py @ 537:eac0ce5e137d

Added a "fix-mode" for the potd comment importer command such that it will only create comments if they don't already exist.
author Brian Neal <bgneal@gmail.com>
date Sat, 31 Dec 2011 22:21:58 +0000
parents 4021ea1045f7
children
comparison
equal deleted inserted replaced
536:763eae227302 537:eac0ce5e137d
27 args = '<filename filename ...>' 27 args = '<filename filename ...>'
28 help = 'Imports POTD comments from the old database in CSV format' 28 help = 'Imports POTD comments from the old database in CSV format'
29 option_list = LabelCommand.option_list + ( 29 option_list = LabelCommand.option_list + (
30 optparse.make_option("-p", "--progress", action="store_true", 30 optparse.make_option("-p", "--progress", action="store_true",
31 help="Output a . after every 20 items to show progress"), 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"),
32 ) 34 )
33 md_writer = MarkdownWriter() 35 md_writer = MarkdownWriter()
34 36
35 def handle_label(self, filename, **options): 37 def handle_label(self, filename, **options):
36 """ 38 """
37 Process each line in the CSV file given by filename by 39 Process each line in the CSV file given by filename by
38 creating a new POTD comment. 40 creating a new POTD comment.
39 41
40 """ 42 """
41 self.show_progress = options.get('progress') 43 self.show_progress = options.get('progress')
44 self.fix_mode = options.get('fix_mode')
42 self.users = {} 45 self.users = {}
43 46
44 try: 47 try:
45 with open(filename, "rb") as f: 48 with open(filename, "rb") as f:
46 self.reader = csv.DictReader(f) 49 self.reader = csv.DictReader(f)
65 """ 68 """
66 Process one row from the CSV file: create a Comment object for 69 Process one row from the CSV file: create a Comment object for
67 the row and save it in the database. 70 the row and save it in the database.
68 71
69 """ 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
70 try: 83 try:
71 user = self._get_user(row['username'].decode('latin-1')) 84 user = self._get_user(row['username'].decode('latin-1'))
72 except User.DoesNotExist: 85 except User.DoesNotExist:
73 print "Could not find user %s for comment %s; skipping." % ( 86 print "Could not find user %s for comment %s; skipping." % (
74 row['username'], row['cid']) 87 row['username'], row['cid'])
81 print "Could not find photo %s for comment %s; skipping." % ( 94 print "Could not find photo %s for comment %s; skipping." % (
82 pid, row['cid']) 95 pid, row['cid'])
83 return 96 return
84 97
85 comment = Comment( 98 comment = Comment(
86 id=int(row['cid']) + ID_OFFSET, 99 id=comment_id,
87 content_type=ContentType.objects.get_for_model(photo), 100 content_type=ContentType.objects.get_for_model(photo),
88 object_id=photo.id, 101 object_id=photo.id,
89 user=user, 102 user=user,
90 comment=self.to_markdown(row['comment'].decode('latin-1')), 103 comment=self.to_markdown(row['comment'].decode('latin-1')),
91 creation_date=datetime.strptime(row['date'], "%Y-%m-%d %H:%M:%S"), 104 creation_date=datetime.strptime(row['date'], "%Y-%m-%d %H:%M:%S"),