# HG changeset patch # User Brian Neal # Date 1262019162 0 # Node ID f7a6b8fe45569afc1d8b6c149de496156852901d # Parent 4f07047e0a40b7d487c8bd0d8e004aaacf666fa1 Implement #46, add a forums stat feature like phpBB. diff -r 4f07047e0a40 -r f7a6b8fe4556 gpp/forums/management/commands/forum_stats.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/gpp/forums/management/commands/forum_stats.py Mon Dec 28 16:52:42 2009 +0000 @@ -0,0 +1,29 @@ +""" +forum_stats is a custom manage.py command for the forums application. +It is intended to be called from a cron job to calculate various forum +statistics. +""" +from django.core.management.base import NoArgsCommand +from django.core.cache import cache + +from forums.models import Statistic +import forums.middleware + + +class Command(NoArgsCommand): + help = "Run periodically to gather forum statistics." + + def handle_noargs(self, **options): + # update maximum users online statistic + users_online = cache.get(forums.middleware.USERS_ONLINE_KEY) + if users_online: + try: + stats = Statistic.objects.get(pk=1) + except Statistic.DoesNotExist: + stats = Statistic(max_users=0) + + curr_users = len(users_online) + + if curr_users > stats.max_users: + stats.max_users = curr_users + stats.save() diff -r 4f07047e0a40 -r f7a6b8fe4556 gpp/forums/models.py --- a/gpp/forums/models.py Tue Dec 22 03:56:26 2009 +0000 +++ b/gpp/forums/models.py Mon Dec 28 16:52:42 2009 +0000 @@ -337,3 +337,22 @@ def touch(self): self.last_visit = datetime.datetime.now() + + +class Statistic(models.Model): + """ + This model keeps track of forum statistics. Currently, the only statistic + is the maximum number of users online. This stat is computed by a mgmt. + command that is run on a cron job to peek at the "users_online" dict + that is maintained in cache by the forums middleware. + """ + max_users = models.IntegerField() + max_users_date = models.DateTimeField() + + def __unicode__(self): + return u'%d users on %s' % (self.max_users, + self.max_users_date.strftime('%Y-%m-%d %H:%M:%S')) + + def save(self, *args, **kwargs): + self.max_users_date = datetime.datetime.now() + super(Statistic, self).save(*args, **kwargs) diff -r 4f07047e0a40 -r f7a6b8fe4556 gpp/forums/templatetags/forum_tags.py --- a/gpp/forums/templatetags/forum_tags.py Tue Dec 22 03:56:26 2009 +0000 +++ b/gpp/forums/templatetags/forum_tags.py Mon Dec 28 16:52:42 2009 +0000 @@ -7,8 +7,11 @@ from django import template from django.conf import settings from django.core.cache import cache +from django.contrib.auth.models import User from forums.models import Topic +from forums.models import Post +from forums.models import Statistic register = template.Library() @@ -143,3 +146,29 @@ 'total': total, 'users': users, } + + +@register.inclusion_tag('forums/forum_stats_tag.html') +def forum_stats(): + """ + Displays forum statistics. + """ + try: + stats = Statistic.objects.get(pk=1) + except Statistic.DoesNotExist: + stats = None + + post_count = Post.objects.all().count() + user_count = User.objects.all().count() + + if user_count > 0: + latest_user = User.objects.values_list('username', flat=True).order_by('-date_joined')[0] + else: + latest_user = None + + return { + 'stats': stats, + 'post_count': post_count, + 'user_count': user_count, + 'latest_user': latest_user, + } diff -r 4f07047e0a40 -r f7a6b8fe4556 gpp/templates/forums/forum_stats_tag.html --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/gpp/templates/forums/forum_stats_tag.html Mon Dec 28 16:52:42 2009 +0000 @@ -0,0 +1,8 @@ +
+Our {{ user_count }} users have posted a total of {{ post_count }} posts.
+The newest registered user is {{ latest_user }}. +{% if stats %} +
+The most users ever online was {{ stats.max_users }} on {{ stats.max_users_date|date:"P l, N n, Y" }}. +{% endif %} +
diff -r 4f07047e0a40 -r f7a6b8fe4556 gpp/templates/forums/index.html --- a/gpp/templates/forums/index.html Tue Dec 22 03:56:26 2009 +0000 +++ b/gpp/templates/forums/index.html Mon Dec 28 16:52:42 2009 +0000 @@ -1,4 +1,5 @@ {% extends 'base.html' %} +{% load cache %} {% load forum_tags %} {% block title %}Forums{% endblock %} {% block content %} @@ -33,6 +34,9 @@ {% endfor %} +{% cache 900 forum-stats-block %} +{% forum_stats %} +{% endcache %} {% whos_online %}

{% current_forum_time user %}

diff -r 4f07047e0a40 -r f7a6b8fe4556 gpp/templates/forums/whos_online_tag.html --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/gpp/templates/forums/whos_online_tag.html Mon Dec 28 16:52:42 2009 +0000 @@ -0,0 +1,11 @@ +
+There {{ total|pluralize:"is,are"}} {{ total }} user{{ total|pluralize }} online: {{ num_users }} registered user{{ num_users|pluralize }} and {{ num_guests }} guest{{ num_guests|pluralize }}. +{% if num_users %} +Registered users: + +{% endif %} +