Beispiel #1
0
 def test_edit_invalid(self):
     request = self.request_factory.post('/', {})
     form = QuestionForm(request.POST, instance=self.question)
     self.assertFalse(form.is_valid())
     self.assertEqual(form.errors['title'], ['This field is required.'])
     self.assertEqual(form.errors['question'], ['This field is required.'])
     self.assertEqual(Action.objects.count(), 0,
         'Invalid form submission should not result in an action'
     )
Beispiel #2
0
 def test_create_invalid(self):
     request = self.request_factory.post('/', {})
     site = Site.objects.get(id=current_site_id)
     question = Question(user=self.create_user(), site=site)
     form = QuestionForm(request.POST, instance=question)
     self.assertFalse(form.is_valid())
     self.assertEqual(form.errors['title'], ['This field is required.'])
     self.assertEqual(form.errors['question'], ['This field is required.'])
     self.assertEqual(Action.objects.count(), 0,
         'Invalid form submission should not result in an action'
     )
Beispiel #3
0
 def test_edit_valid(self):
     post_data = {
         'title': self.get_random_string(),
         'question': self.get_random_string(),
     }
     request = self.request_factory.post('/', post_data)
     form = QuestionForm(request.POST, instance=self.question)
     if form.is_valid():
         form.save()
         self.assertEqual(Action.objects.count(), 0,
             'editting a question should not result in an action'
         )
     else:
         self.fail('Form should be valid')
Beispiel #4
0
 def test_create_valid(self):
     post_data = {
         'title': self.get_random_string(),
         'question': self.get_random_string(),
     }
     request = self.request_factory.post('/', post_data)
     site = Site.objects.get(id=current_site_id)
     question = Question(user=self.create_user(), site=site)
     form = QuestionForm(request.POST, instance=question)
     if form.is_valid():
         form.save()
         self.assertEqual(Action.objects.count(), 1,
             'Creating a question should result in an action'
         )
     else:
         self.fail('Form should be valid')
Beispiel #5
0
def question_create_edit(request, question_slug=None):
    user = request.user
    site = Site.objects.get(id=current_site_id)
    if question_slug:
        question = get_object_or_404(Question, slug=question_slug)
        if question.user != user and not user.is_superuser:
            return HttpResponseForbidden(
                'You are not the owner of this question')
    else:
        question = Question(user=user, site=site)
    question_form = QuestionForm(request.POST or None, instance=question)
    if question_form.is_valid():
        question_form.save()
        return redirect(question)
    return render(request, 'qanda/question_create_edit.html', {
        'question': question,
        'question_form': question_form,
    })
Beispiel #6
0
def question_create_edit(request, question_slug=None):
    user = request.user
    site = Site.objects.get(id=current_site_id)
    if question_slug:
        question = get_object_or_404(Question, slug=question_slug)
        if question.user != user and not user.is_superuser:
            return HttpResponseForbidden(
                'You are not the owner of this question'
            )
    else:
        question = Question(user=user, site=site)
    question_form = QuestionForm(request.POST or None, instance=question)
    if question_form.is_valid():
        question_form.save()
        return redirect(question)
    return render(request, 'qanda/question_create_edit.html', {
        'question': question,
        'question_form': question_form,
    })