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')
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')
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, })
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, })