annotate gpp/forums/tools.py @ 176:b7ac381996e8

Implement ticket #59; update RSS feeds for Django 1.2.
author Brian Neal <bgneal@gmail.com>
date Thu, 11 Mar 2010 02:34:07 +0000
parents 152d77265da6
children d77e0dc772ad
rev   line source
bgneal@147 1 """
bgneal@147 2 This module contains misc. utility functions for forum management.
bgneal@147 3 """
bgneal@147 4 from forums.models import Post, Topic, Forum, ForumLastVisit, TopicLastVisit
bgneal@147 5
bgneal@147 6
bgneal@147 7 def delete_user_posts(user):
bgneal@147 8 """
bgneal@147 9 This function deletes all the posts for a given user.
bgneal@147 10 It also cleans up any last visit database records for the user.
bgneal@147 11 This function adjusts the last post foreign keys before deleting
bgneal@147 12 the posts to avoid the cascading delete behavior.
bgneal@147 13 """
bgneal@147 14 posts = Post.objects.filter(user=user).select_related()
bgneal@147 15
bgneal@147 16 # build a set of topics and forums affected by the post deletions
bgneal@147 17
bgneal@147 18 topics = set(post.topic for post in posts)
bgneal@147 19 forums = set(topic.forum for topic in topics)
bgneal@147 20
bgneal@147 21 post_ids = [post.pk for post in posts]
bgneal@147 22 pending_delete = []
bgneal@147 23
bgneal@147 24 for topic in topics:
bgneal@147 25 if topic.last_post.pk in post_ids:
bgneal@147 26 topic_posts = Post.objects.filter(topic=topic).exclude(
bgneal@147 27 pk__in=post_ids)
bgneal@147 28 topic.post_count = topic_posts.count()
bgneal@147 29 if topic.post_count > 0:
bgneal@147 30 topic.last_post = topic_posts.latest()
bgneal@147 31 topic.update_date = topic.last_post.creation_date
bgneal@147 32 topic.save()
bgneal@147 33 else:
bgneal@147 34 # Topic should be deleted, it has no posts;
bgneal@147 35 # We can't delete it now as it could cascade and take out a
bgneal@147 36 # forum. Remember it for later deletion.
bgneal@147 37 pending_delete.append(topic)
bgneal@147 38
bgneal@147 39 for forum in forums:
bgneal@147 40 if forum.last_post.pk in post_ids:
bgneal@147 41 forum_posts = Post.objects.filter(topic__forum=forum).exclude(
bgneal@147 42 pk__in=post_ids)
bgneal@147 43 forum.post_count = forum_posts.count()
bgneal@147 44 if forum.post_count > 0:
bgneal@147 45 forum.last_post = forum_posts.latest()
bgneal@147 46 else:
bgneal@147 47 forum.last_post = None
bgneal@147 48 forum.save()
bgneal@147 49
bgneal@147 50 # Delete pending topics now because forums have just adjusted their
bgneal@147 51 # foreign keys into Post
bgneal@147 52 if pending_delete:
bgneal@147 53 topic_ids = [topic.pk for topic in pending_delete]
bgneal@147 54 Topic.objects.filter(pk__in=topic_ids).delete()
bgneal@147 55
bgneal@147 56 # Topics have been deleted, re-compute topic counts for forums
bgneal@147 57 for forum in forums:
bgneal@147 58 forum.topic_count = Topic.objects.filter(forum=forum).count()
bgneal@147 59 forum.save()
bgneal@147 60
bgneal@147 61 # All foreign keys are accounted for, we can now delete the posts in bulk.
bgneal@147 62 # Since some posts in our original queryset may have been deleted already,
bgneal@147 63 # run a new query (although it may be ok)
bgneal@147 64 Post.objects.filter(pk__in=post_ids).delete()
bgneal@147 65
bgneal@147 66 # delete all the last visit records for this user
bgneal@147 67 TopicLastVisit.objects.filter(user=user).delete()
bgneal@147 68 ForumLastVisit.objects.filter(user=user).delete()
bgneal@147 69