# HG changeset patch # User Brian Neal # Date 1295209106 0 # Node ID 7e19180b128df2f209900b79d1e7b5d022b08973 # Parent 6ca2c474d92f6d707cf6df565ff88c9c547e6a21 Fixing #97; adding a management command to remove old forum and topic last visit records. diff -r 6ca2c474d92f -r 7e19180b128d gpp/forums/admin.py --- a/gpp/forums/admin.py Sat Jan 15 21:55:37 2011 +0000 +++ b/gpp/forums/admin.py Sun Jan 16 20:18:26 2011 +0000 @@ -8,6 +8,7 @@ from forums.models import Topic from forums.models import Post from forums.models import FlaggedPost +from forums.models import ForumLastVisit from forums.models import TopicLastVisit import bio.badges @@ -64,6 +65,11 @@ accept_flags.short_description = "Accept selected flagged posts" +class ForumLastVisitAdmin(admin.ModelAdmin): + raw_id_fields = ('user', 'forum') + list_display = ('user', 'forum', 'begin_date', 'end_date') + + class TopicLastVisitAdmin(admin.ModelAdmin): raw_id_fields = ('user', 'topic') list_display = ('user', 'topic', 'last_visit') @@ -74,4 +80,5 @@ admin.site.register(Topic, TopicAdmin) admin.site.register(Post, PostAdmin) admin.site.register(FlaggedPost, FlaggedPostAdmin) +admin.site.register(ForumLastVisit, ForumLastVisitAdmin) admin.site.register(TopicLastVisit, TopicLastVisitAdmin) diff -r 6ca2c474d92f -r 7e19180b128d gpp/forums/management/commands/forum_cleanup.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/gpp/forums/management/commands/forum_cleanup.py Sun Jan 16 20:18:26 2011 +0000 @@ -0,0 +1,26 @@ +""" +forum_cleanup.py - A management command to cleanup forum model objects. Right +now this entails deleting old forum and topic last visit records. + +""" +import datetime + +from django.core.management.base import NoArgsCommand, CommandError + +from forums.models import ForumLastVisit, TopicLastVisit +import forums.unread + + +class Command(NoArgsCommand): + help = "This command deletes old forum and topic last visit records." + + def handle_noargs(self, **opts): + + now = datetime.datetime.now() + threshold = now - forums.unread.THRESHOLD * 2 + + # delete old topic last visit records + TopicLastVisit.objects.filter(last_visit__lt=threshold).delete() + + # delete old forum visit records + ForumLastVisit.objects.filter(end_date__lt=threshold).delete() diff -r 6ca2c474d92f -r 7e19180b128d gpp/forums/unread.py --- a/gpp/forums/unread.py Sat Jan 15 21:55:37 2011 +0000 +++ b/gpp/forums/unread.py Sun Jan 16 20:18:26 2011 +0000 @@ -1,5 +1,5 @@ """ -This file contains routines for implementing the "has unread" feature. +This file contains routines for implementing the "has unread" feature. Forums, topics, and posts are displayed with a visual indication if they have been read or not. """ @@ -10,7 +10,7 @@ from forums.models import ForumLastVisit, TopicLastVisit, Topic, Forum -THRESHOLD = datetime.timedelta(days=7) +THRESHOLD = datetime.timedelta(days=14) ####################################################################### @@ -25,7 +25,7 @@ # retrieve ForumLastVisit records in one SQL query forum_ids = [forum.id for forum in qs] - flvs = ForumLastVisit.objects.filter(user=user, + flvs = ForumLastVisit.objects.filter(user=user, forum__in=forum_ids).select_related() flvs = dict([(flv.forum.id, flv) for flv in flvs]) @@ -74,7 +74,7 @@ TopicLastVisit.objects.filter(user=user, topic__forum=forum, last_visit__lt=min_date).delete() - topics = Topic.objects.filter(forum=forum, + topics = Topic.objects.filter(forum=forum, creation_date__gt=flv.begin_date) tracked_topics = TopicLastVisit.objects.filter(user=user, topic__forum=forum, last_visit__gt=flv.begin_date)