comparison gpp/forums/views/main.py @ 318:c550933ff5b6

Fix a bug where you'd get an error when trying to delete a forum thread (topic does not exist). Apparently when you call topic.delete() the posts would get deleted, but the signal handler for each one would run, and it would try to update the topic's post count or something, but the topic was gone? Reworked the code a bit and explicitly delete the posts first. I also added a sync() call on the parent forum since post counts were not getting adjusted.
author Brian Neal <bgneal@gmail.com>
date Sat, 05 Feb 2011 21:46:52 +0000
parents 4f032a6e21f8
children 3f9b9fd54b01
comparison
equal deleted inserted replaced
317:2934a867c2ef 318:c550933ff5b6
478 Deletes the topic and all posts contained within. 478 Deletes the topic and all posts contained within.
479 Adjusts the parent forum's last_post as needed. 479 Adjusts the parent forum's last_post as needed.
480 Note that we don't bother adjusting all the users' 480 Note that we don't bother adjusting all the users'
481 post counts as that doesn't seem to be worth the effort. 481 post counts as that doesn't seem to be worth the effort.
482 """ 482 """
483 if topic.forum.last_post and topic.forum.last_post.topic == topic: 483 parent_forum = topic.forum
484 topic.forum.last_post_pre_delete(deleting_topic=True) 484 if parent_forum.last_post and parent_forum.last_post.topic == topic:
485 topic.forum.save() 485 parent_forum.last_post_pre_delete(deleting_topic=True)
486 parent_forum.save()
486 487
487 # delete subscriptions to this topic 488 # delete subscriptions to this topic
488 topic.subscribers.clear() 489 topic.subscribers.clear()
489 topic.bookmarkers.clear() 490 topic.bookmarkers.clear()
490 491
491 # delete all attachments 492 # delete all attachments
492 posts = Post.objects.filter(topic=topic) 493 posts = Post.objects.filter(topic=topic)
493 for post in posts: 494 for post in posts:
494 post.attachments.clear() 495 post.attachments.clear()
495 496
496 # It should be safe to just delete the topic now. This will 497 # Null out the topic's last post so we don't have a foreign key pointing
497 # automatically delete all posts in the topic. 498 # to a post when we delete posts.
499 topic.last_post = None
500 topic.save()
501
502 # delete all posts in bulk
503 posts.delete()
504
505 # It should be safe to just delete the topic now.
498 topic.delete() 506 topic.delete()
507
508 # Resync parent forum's post and topic counts
509 parent_forum.sync()
510 parent_forum.save()
499 511
500 512
501 @login_required 513 @login_required
502 def new_post(request, topic_id): 514 def new_post(request, topic_id):
503 """ 515 """