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