def test_question_form(user): from forum.forms import QuestionForm from forum.models import Question instance = Question() instance.course = mommy.make('Course', slug='dbsql') instance.user = user data = { 'title': 'Test Title', 'text': 'asljf asdfhuas dfasdflashfdlusafdlsafdlsa filasdflisalfdiayslfdnsalfdyaslifd', } form = QuestionForm(instance=instance, data=data) assert form.is_valid() is True, form.errors form.save() assert Question.objects.filter(slug='test-title').exists() data = { 'title': '', 'text': 'asljf asdfhuas dfasdflashfdlusafdlsafdlsa filasdflisalfdiayslfdnsalfdyaslifd', } form = QuestionForm(data=data) assert form.is_valid() is False data = { 'title': 'Test Title', 'text': '', } form = QuestionForm(data=data) assert form.is_valid() is False
def post(self, request, *args, **kwargs): new_question = Question() course = get_object_or_404(Course, slug=self.kwargs['course_slug']) new_question.course = course new_question.user = request.user form = QuestionForm(instance=new_question, **self.get_form_kwargs()) if form.is_valid(): form.save() self.success_url = reverse_lazy('forum_question', kwargs={'slug': new_question.slug}) return self.form_valid(form) else: return self.form_invalid(form)