Mercurial > public > sg101
view gpp/forums/tools.py @ 186:be3fff614b93
Implement #66; use jQuery UI autocomplete widget to replace obsolete jquery-autocomplete plugin. I implemented a very simple caching system.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Tue, 30 Mar 2010 01:30:32 +0000 |
parents | 152d77265da6 |
children | d77e0dc772ad |
line wrap: on
line source
""" This module contains misc. utility functions for forum management. """ from forums.models import Post, Topic, Forum, ForumLastVisit, TopicLastVisit def delete_user_posts(user): """ This function deletes all the posts for a given user. It also cleans up any last visit database records for the user. This function adjusts the last post foreign keys before deleting the posts to avoid the cascading delete behavior. """ posts = Post.objects.filter(user=user).select_related() # build a set of topics and forums affected by the post deletions topics = set(post.topic for post in posts) forums = set(topic.forum for topic in topics) post_ids = [post.pk for post in posts] pending_delete = [] for topic in topics: if topic.last_post.pk in post_ids: topic_posts = Post.objects.filter(topic=topic).exclude( pk__in=post_ids) topic.post_count = topic_posts.count() if topic.post_count > 0: topic.last_post = topic_posts.latest() topic.update_date = topic.last_post.creation_date topic.save() else: # Topic should be deleted, it has no posts; # We can't delete it now as it could cascade and take out a # forum. Remember it for later deletion. pending_delete.append(topic) for forum in forums: if forum.last_post.pk in post_ids: forum_posts = Post.objects.filter(topic__forum=forum).exclude( pk__in=post_ids) forum.post_count = forum_posts.count() if forum.post_count > 0: forum.last_post = forum_posts.latest() else: forum.last_post = None forum.save() # Delete pending topics now because forums have just adjusted their # foreign keys into Post if pending_delete: topic_ids = [topic.pk for topic in pending_delete] Topic.objects.filter(pk__in=topic_ids).delete() # Topics have been deleted, re-compute topic counts for forums for forum in forums: forum.topic_count = Topic.objects.filter(forum=forum).count() forum.save() # All foreign keys are accounted for, we can now delete the posts in bulk. # Since some posts in our original queryset may have been deleted already, # run a new query (although it may be ok) Post.objects.filter(pk__in=post_ids).delete() # delete all the last visit records for this user TopicLastVisit.objects.filter(user=user).delete() ForumLastVisit.objects.filter(user=user).delete()