Permalink
Browse files

Add dry-run and last-login options to rebuild_avatars command, to bet…

…ter control what's rebuilt.

Also indicate progression when printing what's going on.
  • Loading branch information...
diox committed Feb 21, 2012
1 parent 7a6e0c6 commit ec9ad81937cdc43531f7e0d7945f4a8423abd50e
Showing with 37 additions and 10 deletions.
  1. +37 −10 avatar/management/commands/rebuild_avatars.py
@@ -1,14 +1,41 @@
-from django.core.management.base import NoArgsCommand
+from datetime import datetime, timedelta
+from optparse import make_option
+
+from django.core.management.base import BaseCommand
from avatar.models import Avatar
from avatar.settings import AUTO_GENERATE_AVATAR_SIZES
-class Command(NoArgsCommand):
- help = "Regenerates avatar thumbnails for the sizes specified in " + \
- "settings.AUTO_GENERATE_AVATAR_SIZES."
-
- def handle_noargs(self, **options):
- for avatar in Avatar.objects.all():
- for size in AUTO_GENERATE_AVATAR_SIZES:
- print "Rebuilding Avatar id=%s at size %s." % (avatar.id, size)
- avatar.create_thumbnail(size)
+class Command(BaseCommand):
+ """ Regenerates avatar thumbnails for the sizes specified in
+ settings.AUTO_GENERATE_AVATAR_SIZES."""
+ help = __doc__
+
+ option_list = BaseCommand.option_list + (
+ make_option('--dry-run',
+ action='store_true',
+ dest='dryrun',
+ default=False,
+ help='Print what would be rebuilt without actually doing it'),
+
+ make_option('--last-login',
+ action='store',
+ dest='lastlogin',
+ default=None,
+ help='Only rebuild avatars for users that logged in in the last <interval> days'),
+ )
+
+
+ def handle(self, **options):
+ filters = {}
+ if options['lastlogin']:
+ filters['user__last_login__gt'] = datetime.now() - timedelta(days=int(options['lastlogin']))
+ qs = Avatar.objects.filter(**filters)
+ if options['dryrun']:
+ print "Would rebuild %d avatars, each in %d different sizes %s" % (qs.count(), len(AUTO_GENERATE_AVATAR_SIZES), str(AUTO_GENERATE_AVATAR_SIZES))
+ else:
+ count = qs.count()
+ for i, avatar in enumerate(qs.iterator()):
+ for size in AUTO_GENERATE_AVATAR_SIZES:
+ print "Rebuilding Avatar %d / %d id=%s at size %s." % (i + 1, count, avatar.id, size)
+ avatar.create_thumbnail(size)

0 comments on commit ec9ad81

Please sign in to comment.