def topic_publish(request, category_id=None): if category_id: Category.objects.get_public_or_404(pk=category_id) if request.method == 'POST': form = TopicForm(user=request.user, data=request.POST) cform = CommentForm(user=request.user, data=request.POST) pform = TopicPollForm(data=request.POST) pformset = TopicPollChoiceFormSet(can_delete=False, data=request.POST) if not request.is_limited and form.is_valid() and cform.is_valid() \ and pform.is_valid() and pformset.is_valid(): # wrap in transaction.atomic? topic = form.save() cform.topic = topic comment = cform.save() comment_posted.send(sender=comment.__class__, comment=comment, mentions=cform.mentions) # Create a poll only if we have choices if pformset.is_filled(): pform.topic = topic poll = pform.save() pformset.instance = poll pformset.save() return redirect(topic.get_absolute_url()) else: form = TopicForm(user=request.user, initial={'category': category_id, }) cform = CommentForm() pform = TopicPollForm() pformset = TopicPollChoiceFormSet(can_delete=False) return render(request, 'foro/topic/topic_publish.html', {'form': form, 'cform': cform, 'pform': pform, 'pformset': pformset})
def test_create_poll_choices_unfilled(self): """ TopicPollChoiceFormSet """ form_data = {'choices-TOTAL_FORMS': 2, 'choices-INITIAL_FORMS': 0, 'choices-0-description': '', 'choices-1-description': ''} form = TopicPollChoiceFormSet(data=form_data) self.assertTrue(form.is_valid()) self.assertFalse(form.is_filled())
def test_create_poll_choices_filled_but_deleted(self): """ TopicPollChoiceFormSet, create """ form_data = {'choices-TOTAL_FORMS': 2, 'choices-INITIAL_FORMS': 0, 'choices-0-description': 'op1', 'choices-0-DELETE': "on", 'choices-1-description': 'op2', 'choices-1-DELETE': "on"} form = TopicPollChoiceFormSet(can_delete=True, data=form_data) self.assertTrue(form.is_valid()) self.assertFalse(form.is_filled())
def test_create_poll_choices(self): """ TopicPollChoiceFormSet Check it's valid and is filled """ form_data = {'choices-TOTAL_FORMS': 2, 'choices-INITIAL_FORMS': 0, 'choices-0-description': 'op1', 'choices-1-description': 'op2'} form = TopicPollChoiceFormSet(data=form_data) self.assertTrue(form.is_valid()) self.assertTrue(form.is_filled())
def test_update_poll_choices_filled_but_deleted(self): """ TopicPollChoiceFormSet, update is_filled should not be called when updating (coz form is always filled), but whatever """ poll = TopicPoll.objects.create(topic=self.topic) form_data = {'choices-TOTAL_FORMS': 2, 'choices-INITIAL_FORMS': 0, 'choices-0-description': 'op1', 'choices-0-DELETE': "on", 'choices-1-description': 'op2', 'choices-1-DELETE': "on"} form = TopicPollChoiceFormSet(can_delete=True, data=form_data, instance=poll) self.assertTrue(form.is_valid()) self.assertTrue(form.is_filled())
def test_create_poll_choices_unfilled(self): """ TopicPollChoiceFormSet """ form_data = { 'choices-TOTAL_FORMS': 2, 'choices-INITIAL_FORMS': 0, 'choices-0-description': '', 'choices-1-description': '' } form = TopicPollChoiceFormSet(data=form_data) self.assertTrue(form.is_valid()) self.assertFalse(form.is_filled())
def test_create_poll_choices(self): """ TopicPollChoiceFormSet Check it's valid and is filled """ form_data = { 'choices-TOTAL_FORMS': 2, 'choices-INITIAL_FORMS': 0, 'choices-0-description': 'op1', 'choices-1-description': 'op2' } form = TopicPollChoiceFormSet(data=form_data) self.assertTrue(form.is_valid()) self.assertTrue(form.is_filled())
def test_create_poll_choices_filled_but_deleted(self): """ TopicPollChoiceFormSet, create """ form_data = { 'choices-TOTAL_FORMS': 2, 'choices-INITIAL_FORMS': 0, 'choices-0-description': 'op1', 'choices-0-DELETE': "on", 'choices-1-description': 'op2', 'choices-1-DELETE': "on" } form = TopicPollChoiceFormSet(can_delete=True, data=form_data) self.assertTrue(form.is_valid()) self.assertFalse(form.is_filled())
def topic_publish(request, category_id=None): if category_id: Category.objects.get_public_or_404(pk=category_id) if request.method == 'POST': form = TopicForm(user=request.user, data=request.POST) cform = CommentForm(user=request.user, data=request.POST) pform = TopicPollForm(data=request.POST) pformset = TopicPollChoiceFormSet(can_delete=False, data=request.POST) if not request.is_limited and form.is_valid() and cform.is_valid() \ and pform.is_valid() and pformset.is_valid(): # wrap in transaction.atomic? topic = form.save() cform.topic = topic comment = cform.save() comment_posted.send(sender=comment.__class__, comment=comment, mentions=cform.mentions) # Create a poll only if we have choices if pformset.is_filled(): pform.topic = topic poll = pform.save() pformset.instance = poll pformset.save() return redirect(topic.get_absolute_url()) else: form = TopicForm(user=request.user, initial={ 'category': category_id, }) cform = CommentForm() pform = TopicPollForm() pformset = TopicPollChoiceFormSet(can_delete=False) return render(request, 'foro/topic/topic_publish.html', { 'form': form, 'cform': cform, 'pform': pform, 'pformset': pformset })
def test_update_poll_choices_filled_but_deleted(self): """ TopicPollChoiceFormSet, update is_filled should not be called when updating (coz form is always filled), but whatever """ poll = TopicPoll.objects.create(topic=self.topic) form_data = { 'choices-TOTAL_FORMS': 2, 'choices-INITIAL_FORMS': 0, 'choices-0-description': 'op1', 'choices-0-DELETE': "on", 'choices-1-description': 'op2', 'choices-1-DELETE': "on" } form = TopicPollChoiceFormSet(can_delete=True, data=form_data, instance=poll) self.assertTrue(form.is_valid()) self.assertTrue(form.is_filled())