Пример #1
0
    def test_render_form_errors(self):
        """
        return form errors string
        """
        class MockForm:
            non_field_errors = [
                "error1",
            ]
            hidden_fields = [
                {
                    'errors': "error2",
                },
            ]
            visible_fields = [
                {
                    'errors': "error3",
                },
            ]

        res = spirit_utils.render_form_errors(MockForm())
        lines = [line.strip() for line in res.splitlines()]
        self.assertEqual(
            "".join(lines),
            '<ul class="errorlist"><li>error1</li><li>error2</li><li>error3</li></ul>'
        )
Пример #2
0
    def test_render_form_errors(self):
        """
        return form errors string
        """
        class MockForm:
            non_field_errors = "error1"
            errors = "error2"

        res = spirit_utils.render_form_errors(MockForm())
        self.assertEqual(res.splitlines(), "error1\r\nerror2".splitlines())
Пример #3
0
def notification_update(request, pk):
    notification = get_object_or_404(TopicNotification, pk=pk, user=request.user)
    form = NotificationForm(data=request.POST, instance=notification)

    if form.is_valid():
        form.save()
        return redirect(request.POST.get('next', notification.topic.get_absolute_url()))
    else:
        messages.error(request, utils.render_form_errors(form))
        return redirect(request.POST.get('next', notification.topic.get_absolute_url()))
Пример #4
0
def notification_create(request, topic_id):
    topic = get_object_or_404(Topic.objects.for_access(request.user), pk=topic_id)
    form = NotificationCreationForm(user=request.user, topic=topic, data=request.POST)

    if form.is_valid():
        form.save()
        return redirect(request.POST.get("next", topic.get_absolute_url()))
    else:
        messages.error(request, utils.render_form_errors(form))
        return redirect(request.POST.get("next", topic.get_absolute_url()))
Пример #5
0
def favorite_create(request, topic_id):
    topic = get_object_or_404(Topic, pk=topic_id)
    form = FavoriteForm(user=request.user, topic=topic, data=request.POST)

    if form.is_valid():
        form.save()
    else:
        messages.error(request, utils.render_form_errors(form))

    return redirect(request.POST.get('next', topic.get_absolute_url()))
Пример #6
0
def favorite_create(request, topic_id):
    topic = get_object_or_404(Topic, pk=topic_id)
    form = FavoriteForm(user=request.user, topic=topic, data=request.POST)

    if form.is_valid():
        form.save()
    else:
        messages.error(request, utils.render_form_errors(form))

    return redirect(request.POST.get('next', topic.get_absolute_url()))
Пример #7
0
    def test_render_form_errors(self):
        """
        return form errors string
        """
        class MockForm:
            non_field_errors = "error1"
            errors = "error2"

        res = spirit_utils.render_form_errors(MockForm())
        self.assertEqual(res.splitlines(), "error1\r\nerror2".splitlines())
Пример #8
0
    def test_render_form_errors(self):
        """
        return form errors string
        """
        class MockForm:
            non_field_errors = ["error1", ]
            hidden_fields = [{'errors': "error2", }, ]
            visible_fields = [{'errors': "error3", }, ]

        res = spirit_utils.render_form_errors(MockForm())
        lines = [line.strip() for line in res.splitlines()]
        self.assertEqual("".join(lines), '<ul class="errorlist"><li>error1</li><li>error2</li><li>error3</li></ul>')
Пример #9
0
def notification_create(request, topic_id):
    topic = get_object_or_404(Topic.objects.for_access(request.user),
                              pk=topic_id)
    form = NotificationCreationForm(user=request.user,
                                    topic=topic,
                                    data=request.POST)

    if form.is_valid():
        form.save()
        return redirect(request.POST.get('next', topic.get_absolute_url()))
    else:
        messages.error(request, utils.render_form_errors(form))
        return redirect(request.POST.get('next', topic.get_absolute_url()))
Пример #10
0
def private_access_create(request, topic_id):
    topic_private = TopicPrivate.objects.for_create_or_404(topic_id, request.user)
    form = TopicPrivateInviteForm(topic=topic_private.topic, data=request.POST)

    if form.is_valid():
        topic_private_access_pre_create.send(sender=topic_private.__class__,
                                             topic=topic_private.topic,
                                             user=form.cleaned_data['user'])
        form.save()
    else:
        messages.error(request, utils.render_form_errors(form))

    return redirect(request.POST.get('next', topic_private.get_absolute_url()))
Пример #11
0
def notification_update(request, pk):
    notification = get_object_or_404(TopicNotification,
                                     pk=pk,
                                     user=request.user)
    form = NotificationForm(data=request.POST, instance=notification)

    if form.is_valid():
        form.save()
        return redirect(
            request.POST.get('next', notification.topic.get_absolute_url()))
    else:
        messages.error(request, utils.render_form_errors(form))
        return redirect(
            request.POST.get('next', notification.topic.get_absolute_url()))
Пример #12
0
def comment_move(request, topic_id):
    # TODO: comment_move signal (update topic comment_count)
    topic = get_object_or_404(Topic, pk=topic_id)
    form = CommentMoveForm(topic=topic, data=request.POST)

    if form.is_valid():
        comments = form.save()

        for comment in comments:
            comment_posted.send(sender=comment.__class__, comment=comment, mentions=None)
    else:
        messages.error(request, render_form_errors(form))

    return redirect(request.POST.get('next', topic.get_absolute_url()))
Пример #13
0
def poll_vote(request, pk):
    poll = get_object_or_404(TopicPoll, pk=pk)

    if not request.user.is_authenticated():
        return redirect_to_login(next=poll.get_absolute_url(),
                                 login_url=settings.LOGIN_URL)

    form = TopicPollVoteManyForm(user=request.user, poll=poll, data=request.POST)

    if form.is_valid():
        form.save_m2m()
        return redirect(request.POST.get('next', poll.get_absolute_url()))
    else:
        messages.error(request, utils.render_form_errors(form))
        return redirect(request.POST.get('next', poll.get_absolute_url()))
Пример #14
0
def comment_move(request, topic_id):
    # TODO: comment_move signal (update topic comment_count)
    topic = get_object_or_404(Topic, pk=topic_id)
    form = CommentMoveForm(topic=topic, data=request.POST)

    if form.is_valid():
        comments = form.save()

        for comment in comments:
            comment_posted.send(sender=comment.__class__,
                                comment=comment,
                                mentions=None)
    else:
        messages.error(request, render_form_errors(form))

    return redirect(request.POST.get('next', topic.get_absolute_url()))
Пример #15
0
def poll_vote(request, pk):
    poll = get_object_or_404(TopicPoll, pk=pk)

    if not request.user.is_authenticated():
        return redirect_to_login(next=poll.get_absolute_url(),
                                 login_url=settings.LOGIN_URL)

    form = TopicPollVoteManyForm(user=request.user,
                                 poll=poll,
                                 data=request.POST)

    if form.is_valid():
        form.save_m2m()
        return redirect(request.POST.get('next', poll.get_absolute_url()))
    else:
        messages.error(request, utils.render_form_errors(form))
        return redirect(request.POST.get('next', poll.get_absolute_url()))
Пример #16
0
def poll_vote(request, pk):
    # TODO: check if user has access to this topic/poll
    poll = get_object_or_404(TopicPoll, pk=pk)

    if not request.user.is_authenticated():
        return redirect_to_login(next=poll.get_absolute_url(),
                                 login_url=settings.LOGIN_URL)

    form = TopicPollVoteManyForm(user=request.user, poll=poll, data=request.POST)

    if form.is_valid():
        topic_poll_pre_vote.send(sender=poll.__class__, poll=poll, user=request.user)
        form.save_m2m()
        topic_poll_post_vote.send(sender=poll.__class__, poll=poll, user=request.user)
        return redirect(request.POST.get('next', poll.get_absolute_url()))
    else:
        messages.error(request, utils.render_form_errors(form))
        return redirect(request.POST.get('next', poll.get_absolute_url()))
Пример #17
0
def poll_vote(request, pk):
    # TODO: check if user has access to this topic/poll
    poll = get_object_or_404(TopicPoll, pk=pk)

    if not request.user.is_authenticated():
        return redirect_to_login(next=poll.get_absolute_url(),
                                 login_url=settings.LOGIN_URL)

    form = TopicPollVoteManyForm(user=request.user, poll=poll, data=request.POST)

    if form.is_valid():
        topic_poll_pre_vote.send(sender=poll.__class__, poll=poll, user=request.user)
        form.save_m2m()
        topic_poll_post_vote.send(sender=poll.__class__, poll=poll, user=request.user)
        return redirect(request.POST.get('next', poll.get_absolute_url()))

    messages.error(request, utils.render_form_errors(form))
    return redirect(request.POST.get('next', poll.get_absolute_url()))