Mercurial > public > sg101
annotate forums/management/commands/sync_forums.py @ 949:b8c3863e34d2
Update requirements.txt for Django 1.7.8 upgrade.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Sat, 16 May 2015 15:02:31 -0500 |
parents | ee87ea74d46b |
children |
rev | line source |
---|---|
bgneal@294 | 1 """ |
bgneal@294 | 2 sync_forums.py - A management command to synchronize the forums by recomputing |
bgneal@294 | 3 the de-normalized fields in the forum and topic objects. |
bgneal@294 | 4 |
bgneal@294 | 5 """ |
bgneal@294 | 6 import optparse |
bgneal@294 | 7 |
bgneal@294 | 8 from django.core.management.base import NoArgsCommand, CommandError |
bgneal@294 | 9 |
bgneal@294 | 10 from forums.models import Forum |
bgneal@294 | 11 from forums.models import Topic |
bgneal@294 | 12 |
bgneal@294 | 13 |
bgneal@294 | 14 class Command(NoArgsCommand): |
bgneal@294 | 15 help = """\ |
bgneal@294 | 16 This command synchronizes the forum application's forums and topic objects |
bgneal@294 | 17 by updating their de-normalized fields. |
bgneal@294 | 18 """ |
bgneal@294 | 19 option_list = NoArgsCommand.option_list + ( |
bgneal@294 | 20 optparse.make_option("-p", "--progress", action="store_true", |
bgneal@294 | 21 help="Output a . after every 50 topics to show progress"), |
bgneal@294 | 22 ) |
bgneal@294 | 23 |
bgneal@294 | 24 def handle_noargs(self, **opts): |
bgneal@294 | 25 |
bgneal@294 | 26 show_progress = opts.get('progress', False) or False |
bgneal@294 | 27 |
bgneal@294 | 28 n = 0 |
bgneal@294 | 29 for topic in Topic.objects.iterator(): |
bgneal@294 | 30 topic.post_count_update() |
bgneal@294 | 31 topic.save() |
bgneal@294 | 32 n += 1 |
bgneal@294 | 33 if n % 50 == 0: |
bgneal@294 | 34 self.stdout.write('.') |
bgneal@294 | 35 self.stdout.flush() |
bgneal@294 | 36 |
bgneal@294 | 37 for forum in Forum.objects.all(): |
bgneal@294 | 38 forum.sync() |
bgneal@294 | 39 forum.save() |
bgneal@294 | 40 |
bgneal@294 | 41 self.stdout.write('\n') |
bgneal@294 | 42 |