changeset 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 (2009-12-28)
parents 4f07047e0a40
children 952e05cb3d80
files gpp/forums/management/__init__.py gpp/forums/management/commands/__init__.py gpp/forums/management/commands/forum_stats.py gpp/forums/models.py gpp/forums/templatetags/forum_tags.py gpp/templates/forums/forum_stats_tag.html gpp/templates/forums/index.html gpp/templates/forums/whos_online_tag.html
diffstat 6 files changed, 100 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /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()
--- 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)
--- 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,
+    }
--- /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 @@
+<div id="forum-stats">
+Our <strong>{{ user_count }}</strong> users have posted a total of <strong>{{ post_count }}</strong> posts.<br />
+The newest registered user is <a href="{% url bio-view_profile username=latest_user %}">{{ latest_user }}</a>.
+{% if stats %}
+<br />
+The most users ever online was <strong>{{ stats.max_users }}</strong> on {{ stats.max_users_date|date:"P l, N n, Y" }}.
+{% endif %}
+</div>
--- 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 @@
       </tbody>
    </table>
 {% endfor %}
+{% cache 900 forum-stats-block %}
+{% forum_stats %}
+{% endcache %}
 {% whos_online %}
 <p>{% current_forum_time user %}</p>
 </div>
--- /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 @@
+<div id="whos-online">
+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:
+<ul class="inline-list">
+{% for user in users %}
+<li><a href="{% url bio-view_profile username=user %}">{{ user }}</a></li>
+{% endfor %}
+</ul>
+{% endif %}
+</div>