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>' )
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())
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()))
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()))
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()))
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>')
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()))
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()))
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()))
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()))
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()))
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()))
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()))