comparison ygroup/management/commands/sync_ygroup_posts.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/ygroup/management/commands/sync_ygroup_posts.py@0c18dfb1da1c
children
comparison
equal deleted inserted replaced
580:c525f3e0b5d0 581:ee87ea74d46b
1 """
2 sync_ygroup_posts.py - A management command to synchronize the yahoo group
3 archives by recomputing the de-normalized fields in the post objects.
4
5 """
6 import optparse
7
8 from django.core.management.base import NoArgsCommand, CommandError
9 from django.core.urlresolvers import reverse
10
11 from ygroup.models import Thread, Post
12 import ygroup.views
13
14
15 class Command(NoArgsCommand):
16 help = """\
17 This command synchronizes the ygroup application's post objects
18 by updating their de-normalized fields.
19 """
20 option_list = NoArgsCommand.option_list + (
21 optparse.make_option("-p", "--progress", action="store_true",
22 help="Output a . after every 100 posts to show progress"),
23 )
24
25 def handle_noargs(self, **opts):
26
27 show_progress = opts.get('progress', False) or False
28
29 threads = {}
30 self.stdout.write("Processing threads...\n")
31 for thread in Thread.objects.iterator():
32 threads[thread.id] = [reverse('ygroup-thread_view', args=[thread.id]),
33 list(Post.objects.filter(thread=thread).values_list('id', flat=True))]
34
35 self.stdout.write("Processing posts...\n")
36 n = 0
37 for post in Post.objects.iterator():
38 thread = threads[post.thread.id]
39 pos = thread[1].index(post.id)
40 page = pos / ygroup.views.POSTS_PER_PAGE + 1
41 if page == 1:
42 post.thread_url = thread[0] + '#p%d' % (post.id, )
43 else:
44 post.thread_url = thread[0] + '?page=%d#p%d' % (page, post.id)
45 post.save()
46
47 n += 1
48 if show_progress and n % 100 == 0:
49 self.stdout.write('.')
50 self.stdout.flush()
51
52 self.stdout.write('\n')
53