Skip to content

Commit

Permalink
#237 - Fix possible AttributeError when submitting poll options witho…
Browse files Browse the repository at this point in the history
…ut a question
  • Loading branch information
ellmetha committed Aug 22, 2021
1 parent 5710562 commit 1d0bfbd
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
12 changes: 9 additions & 3 deletions machina/apps/forum_conversation/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -492,11 +492,17 @@ def get_context_data(self, **kwargs):

def form_valid(self, post_form, attachment_formset, poll_option_formset, **kwargs):
""" Processes valid forms. """
save_poll_option_formset = poll_option_formset is not None \
and not self.preview
save_poll_option_formset = (
poll_option_formset is not None and
not self.preview and
kwargs['poll_options_validated']
)

valid = super().form_valid(
post_form, attachment_formset, poll_option_formset=poll_option_formset, **kwargs)
post_form,
attachment_formset,
poll_option_formset=poll_option_formset, **kwargs
)

if save_poll_option_formset:
poll_option_formset.topic = self.forum_post.topic
Expand Down
26 changes: 26 additions & 0 deletions tests/functional/apps/forum_conversation/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,32 @@ def test_cannot_create_polls_with_invalid_options(self):
assert len(messages_2) >= 1
assert all(m.level == MSG.ERROR for m in messages_2)

def test_does_not_create_polls_when_questions_are_not_specified(self):
assign_perm('can_create_polls', self.user, self.top_level_forum)
correct_url = reverse('forum_conversation:topic_create', kwargs={
'forum_slug': self.top_level_forum.slug, 'forum_pk': self.top_level_forum.pk})
post_data = {
'subject': faker.text(max_nb_chars=200),
'content': '[b]{}[/b]'.format(faker.text()),
'topic_type': Topic.TOPIC_POST,
'poll_question': '',
'poll_max_options': 1,
'poll_duration': 0,
'poll-0-id': '',
'poll-0-text': faker.text(max_nb_chars=100),
'poll-1-id': '',
'poll-1-text': faker.text(max_nb_chars=100),
'poll-INITIAL_FORMS': 0,
'poll-TOTAL_FORMS': 2,
'poll-MAX_NUM_FORMS': 1000,
}

response = self.client.post(correct_url, post_data, follow=True)

topic = Topic.objects.get(pk=response.context_data['topic'].pk)
with pytest.raises(Topic.poll.RelatedObjectDoesNotExist):
topic.poll

def test_embed_an_attachment_formset_in_the_context_if_the_user_can_create_attachments(self):
# Setup
assign_perm('can_attach_file', self.user, self.top_level_forum)
Expand Down

0 comments on commit 1d0bfbd

Please sign in to comment.