Beispiel #1
0
 def test_create_poll_invalid(self):
     """
     TopicPollForm
     """
     form_data = {"choice_limit": 0}
     form = TopicPollForm(topic=self.topic, data=form_data)
     self.assertFalse(form.is_valid())
Beispiel #2
0
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, 'spirit/topic/topic_publish.html', {'form': form, 'cform': cform,
                                                               'pform': pform, 'pformset': pformset})
Beispiel #3
0
 def test_create_poll_invalid(self):
     """
     TopicPollForm
     """
     form_data = {'choice_limit': 0, }
     form = TopicPollForm(topic=self.topic, data=form_data)
     self.assertFalse(form.is_valid())
Beispiel #4
0
 def test_create_poll(self):
     """
     TopicPollForm
     """
     form_data = {"choice_limit": 1}
     form = TopicPollForm(topic=self.topic, data=form_data)
     self.assertTrue(form.is_valid())
     form.save()
     self.assertEqual(len(TopicPoll.objects.filter(topic=self.topic)), 1)
Beispiel #5
0
 def test_create_poll(self):
     """
     TopicPollForm
     """
     form_data = {'choice_limit': 1, }
     form = TopicPollForm(topic=self.topic, data=form_data)
     self.assertTrue(form.is_valid())
     form.save()
     self.assertEqual(len(TopicPoll.objects.filter(topic=self.topic)), 1)
Beispiel #6
0
def poll_update(request, pk):
    poll = get_object_or_404(TopicPoll, pk=pk, topic__user=request.user)

    if request.method == 'POST':
        form = TopicPollForm(data=request.POST, instance=poll)
        formset = TopicPollChoiceFormSet(data=request.POST, instance=poll)

        if form.is_valid() and formset.is_valid():
            poll = form.save()
            choices = formset.save()
            return redirect(request.POST.get('next', poll.get_absolute_url()))
    else:
        form = TopicPollForm(instance=poll)
        formset = TopicPollChoiceFormSet(instance=poll)

    return render(request, 'spirit/topic_poll/poll_update.html', {'form': form, 'formset': formset})
Beispiel #7
0
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, 'spirit/topic/topic_publish.html', {
        'form': form,
        'cform': cform,
        'pform': pform,
        'pformset': pformset
    })
Beispiel #8
0
def poll_update(request, pk):
    poll = get_object_or_404(TopicPoll, pk=pk, topic__user=request.user)

    if request.method == 'POST':
        form = TopicPollForm(data=request.POST, instance=poll)
        formset = TopicPollChoiceFormSet(data=request.POST, instance=poll)

        if form.is_valid() and formset.is_valid():
            poll = form.save()
            choices = formset.save()
            return redirect(request.POST.get('next', poll.get_absolute_url()))
    else:
        form = TopicPollForm(instance=poll)
        formset = TopicPollChoiceFormSet(instance=poll)

    return render(request, 'spirit/topic_poll/poll_update.html', {'form': form, 'formset': formset})