Skip to content

Commit

Permalink
Topic model and permission handler class updated so that users cannot…
Browse files Browse the repository at this point in the history
… post replies to locked topics
  • Loading branch information
ellmetha committed Apr 9, 2015
1 parent 1afe83a commit 60dc759
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 1 deletion.
4 changes: 4 additions & 0 deletions machina/apps/forum_conversation/abstract_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ def is_sticky(self):
def is_announce(self):
return self.type == self.TYPE_CHOICES.topic_announce

@property
def is_locked(self):
return self.status == self.STATUS_CHOICES.topic_locked

@property
def first_post(self):
"""
Expand Down
4 changes: 3 additions & 1 deletion machina/apps/forum_permission/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,9 @@ def can_add_post(self, topic, user):
"""
Given a topic, checks whether the user can append posts to it.
"""
return self._perform_basic_permission_check(topic.forum, user, 'can_reply_to_topics')
can_add_post = self._perform_basic_permission_check(topic.forum, user, 'can_reply_to_topics') \
and (not topic.is_locked or user.is_superuser)
return can_add_post

def can_edit_post(self, post, user):
"""
Expand Down
7 changes: 7 additions & 0 deletions tests/unit/conversation/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,13 @@ def test_knows_if_it_is_an_announce(self):
# Run & check
self.assertTrue(announce.is_announce)

def test_knows_if_it_is_locked(self):
# Run & check
self.assertFalse(self.topic.is_locked)
self.topic.status = self.topic.STATUS_CHOICES.topic_locked
self.topic.save()
self.assertTrue(self.topic.is_locked)

def test_has_a_first_post(self):
# Run & check
self.assertEqual(self.topic.first_post, self.post)
Expand Down
22 changes: 22 additions & 0 deletions tests/unit/permission/test_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,28 @@ def test_knows_that_a_superuser_can_post_without_approval(self):
# Run & check
self.assertTrue(self.perm_handler.can_post_without_approval(self.forum_1, u2))

def test_knows_if_a_user_can_add_posts_to_a_topic(self):
# Setup
u2 = UserFactory.create()
assign_perm('can_reply_to_topics', self.u1, self.forum_1)
# Run & check
self.assertTrue(self.perm_handler.can_add_post(self.forum_1_topic, self.u1))
self.assertFalse(self.perm_handler.can_add_post(self.forum_1_topic, u2))

def test_knows_that_a_superuser_can_add_posts_to_a_topic(self):
# Setup
u2 = UserFactory.create(is_superuser=True)
# Run & check
self.assertTrue(self.perm_handler.can_add_post(self.forum_1_topic, u2))

def test_knows_that_a_user_cannot_add_posts_to_a_locked_topic(self):
# Setup
assign_perm('can_reply_to_topics', self.u1, self.forum_1)
self.forum_1_topic.status = self.forum_1_topic.STATUS_CHOICES.topic_locked
self.forum_1_topic.save()
# Run & check
self.assertFalse(self.perm_handler.can_add_post(self.forum_1_topic, self.u1))

def test_knows_if_a_user_can_create_polls(self):
# Setup
u2 = UserFactory.create()
Expand Down

0 comments on commit 60dc759

Please sign in to comment.