annotate gpp/forums/management/commands/forum_stats.py @ 164:f7a6b8fe4556

Implement #46, add a forums stat feature like phpBB.
author Brian Neal <bgneal@gmail.com>
date Mon, 28 Dec 2009 16:52:42 +0000
parents
children
rev   line source
bgneal@164 1 """
bgneal@164 2 forum_stats is a custom manage.py command for the forums application.
bgneal@164 3 It is intended to be called from a cron job to calculate various forum
bgneal@164 4 statistics.
bgneal@164 5 """
bgneal@164 6 from django.core.management.base import NoArgsCommand
bgneal@164 7 from django.core.cache import cache
bgneal@164 8
bgneal@164 9 from forums.models import Statistic
bgneal@164 10 import forums.middleware
bgneal@164 11
bgneal@164 12
bgneal@164 13 class Command(NoArgsCommand):
bgneal@164 14 help = "Run periodically to gather forum statistics."
bgneal@164 15
bgneal@164 16 def handle_noargs(self, **options):
bgneal@164 17 # update maximum users online statistic
bgneal@164 18 users_online = cache.get(forums.middleware.USERS_ONLINE_KEY)
bgneal@164 19 if users_online:
bgneal@164 20 try:
bgneal@164 21 stats = Statistic.objects.get(pk=1)
bgneal@164 22 except Statistic.DoesNotExist:
bgneal@164 23 stats = Statistic(max_users=0)
bgneal@164 24
bgneal@164 25 curr_users = len(users_online)
bgneal@164 26
bgneal@164 27 if curr_users > stats.max_users:
bgneal@164 28 stats.max_users = curr_users
bgneal@164 29 stats.save()