annotate gpp/core/management/commands/max_users.py @ 265:1ba2c6bf6eb7

Closing #98. Animated GIFs were losing their transparency and animated properties when saved as avatars. Reworked the avatar save process to only run the avatar through PIL if it is too big. This preserves the original uploaded file if it is within the desired size settings. This may still mangle big animated gifs. If this becomes a problem, then maybe look into calling the PIL Image.resize() method directly. Moved the PIL image specific functions from bio.forms to a new module: core.image for better reusability in the future.
author Brian Neal <bgneal@gmail.com>
date Fri, 24 Sep 2010 02:12:09 +0000
parents dcc929973bba
children 3fe60148f75c
rev   line source
bgneal@239 1 """
bgneal@239 2 max_users is a custom manage.py command.
bgneal@239 3 It is intended to be called from a cron job to calculate the maximum
bgneal@239 4 number of users online statistic.
bgneal@239 5 """
bgneal@239 6 import datetime
bgneal@239 7
bgneal@239 8 from django.core.management.base import NoArgsCommand
bgneal@239 9
bgneal@239 10 from core.models import UserLastVisit, AnonLastVisit, Statistic
bgneal@239 11
bgneal@239 12
bgneal@239 13 class Command(NoArgsCommand):
bgneal@239 14 help = "Run periodically to compute the max users online statistic."
bgneal@239 15
bgneal@239 16 def handle_noargs(self, **options):
bgneal@239 17
bgneal@239 18 now = datetime.datetime.now()
bgneal@239 19 cut_off = now - datetime.timedelta(minutes=15)
bgneal@239 20
bgneal@239 21 users = UserLastVisit.objects.filter(last_visit__gte=cut_off).count()
bgneal@239 22 guests = AnonLastVisit.objects.filter(last_visit__gte=cut_off).count()
bgneal@239 23
bgneal@239 24 updated = False
bgneal@239 25 try:
bgneal@239 26 stat = Statistic.objects.get(pk=1)
bgneal@239 27 except Statistic.DoesNotExist:
bgneal@239 28 stat = Statistic(max_users=users,
bgneal@239 29 max_users_date=now,
bgneal@239 30 max_anon_users=guests,
bgneal@239 31 max_anon_users_date=now)
bgneal@239 32 updated=True
bgneal@239 33 else:
bgneal@239 34 if users > stat.max_users:
bgneal@239 35 stat.max_users = users
bgneal@239 36 stat.max_users_date = now
bgneal@239 37 updated=True
bgneal@239 38 if guests > stat.max_anon_users:
bgneal@239 39 stat.max_anon_users = guests
bgneal@239 40 stat.max_anon_users_date = now
bgneal@239 41 updated=True
bgneal@239 42
bgneal@239 43 if updated:
bgneal@239 44 stat.save()