def answer(request, question_id = 0): if request.method == 'POST': #create Answer objects given parameters in POST answer_text = request.POST.get('answer_text') answer_text = answer_text.replace("\n", "<br/>") question_id = request.POST.get('q_id') original_question=Question.objects.get(pk=question_id) response_data = {} answer=Answer( question=original_question, answer_text=answer_text, user=request.user ) answer.save() response_data['answer_id'] = answer.pk response_data['votes'] = answer.upvotes response_data['answer_text'] = answer.answer_text response_data['answer_username'] = answer.user.username response_data['answer_user_id'] = answer.user.id response_data['is_teacher'] = "Student" response_data['name_style'] = "" if answer.user.userprofile.isTeacher: response_data['is_teacher'] = "Teacher" response_data['name_style'] = "color: #FF6600" return HttpResponse( dumps(response_data), content_type = "application/json" )
def process_data(self, **data): answer = Answer(author=self.user, parent=data["question"], **self.create_revision_data(True, **data)) answer.save() if settings.FORM_ANSWERED_BY_SUPERUSER_TAG and self.user.is_superuser: answer_tag = settings.FORM_ANSWERED_BY_SUPERUSER_TAG.strip() if answer_tag not in answer.parent.tagname_list(): answer.parent.active_revision.tagnames += " " + answer_tag answer.parent.active_revision.save() answer.parent.tagnames += " " + answer_tag answer.parent.save() self.node = answer
def process_data(self, **data): answer = Answer(author=self.user, parent=data['question'], **self.create_revision_data(True, **data)) answer.save() if settings.FORM_ANSWERED_BY_SUPERUSER_TAG and self.user.is_superuser: answer_tag = settings.FORM_ANSWERED_BY_SUPERUSER_TAG.strip() if answer_tag not in answer.parent.tagname_list(): answer.parent.active_revision.tagnames += ' ' + answer_tag answer.parent.active_revision.save() answer.parent.tagnames += ' ' + answer_tag answer.parent.save() self.node = answer
def post(self, request, **kwargs): id_ = kwargs['id'] if request.is_ajax(): return ajax_question(request, **kwargs) if not request.user.is_authenticated: response = redirect('forum:login') response['Location'] += '?next=' + reverse('forum:question', kwargs={'id': id_}) return response question = get_object_or_404(Question, id=id_) answer_form = AnswerTheQuestionForm(data=request.POST, instance=Answer( author=request.user, question_id=id_)) if answer_form.is_valid(): answer = answer_form.save() answer.author.answer_added(question) kwargs['answer_form'] = answer_form return self.get_(request, id_, question, **kwargs)
def answer_resource(request): """ Secured answer API. Only authenticated users have access. Don't allow suspect characters in fields - prevent injections. Permission rules: You can create answer only for your current user. Your question will be approved by the administrator. You can see only public questions. You can't modify answers. POST: /api/v1/forum/question/ - create question GET: /api/v1/forum/question/ - get list of questions """ if not request.user.is_authenticated: return HttpResponseForbidden(json.dumps( {'message': 'User must be authenticated'}), content_type='application/json') if request.method != 'POST': return HttpResponseNotAllowed('Method not allowed') data = json.loads(request.body.decode('utf-8')) answer = Answer() if 'question_id' not in data: return HttpResponseForbidden(json.dumps( {'message': 'The question_id is required'}), content_type='application/json') if 'body' in data: answer.body = encrypt(data['body'], settings.TEXT_SECRET_CODE) answer.user_id = request.user.id answer.question_id = data['question_id'] answer.save() return HttpResponse(json.dumps({ 'body': decrypt(answer.body, settings.TEXT_SECRET_CODE), 'question_id': answer.question_id, 'user__first_name': answer.user.first_name, 'user__last_name': answer.user.last_name }), content_type='application/json')
def process_data(self, **data): answer = Answer(author=self.user, parent=data['question'], **self.create_revision_data(True, **data)) answer.save() self.node = answer
def new_answer(request, question_id): new_answer = Answer(content=request.POST["new_answer"], question=Question.objects.get(id=question_id), user=request.user) new_answer.save() return HttpResponseRedirect("/question/" + str(question_id) + "/")
class TestAnswerTopic(TestCase): answer = Answer() topic = Topic() user = User() user_topic = User() def setUp(self): self.answer.description = "<p>Description answer.</p>" self.user.email = "*****@*****.**" self.user.first_name = "TestUser" self.user.username = "******" self.user.is_active = True self.user_topic.email = "*****@*****.**" self.user_topic.first_name = "TestUser" self.user_topic.username = "******" self.user_topic.is_active = True self.user_topic.save() self.topic.title = 'Basic Topic' self.topic.subtitle = 'How test in Django' self.topic.description = '<p>Text Basic Exercise.</p>' self.topic.author = self.user_topic self.factory = RequestFactory() self.user.set_password('userpassword') self.user.save() self.topic.save() self.wrong_user = '******' self.answer_creation_form = { 'description': self.answer.description, } def test_answer_topic(self): request = self.factory.post('/forum/topics/1/', self.answer_creation_form) request.user = self.user response = show_topic(request, 1) self.assertEqual(response.status_code, REQUEST_REDIRECT) def test_list_all_answer(self): list_answers = self.topic.answers() self.assertEqual(len(list_answers), 0) def list_all_answer_except_best_answer(self): self.answer.user = self.user self.answer.topic = self.topic self.answer.save() self.topic.best_answer = self.answer self.topic.save() list_answers = self.topic.answers() self.assertEqual(len(list_answers), 0) def test_if_user_can_delete_answer(self): self.answer.user = self.user self.answer.topic = self.topic self.answer.save() request = self.factory.get('/forum/deleteanswer/1/', follow=True) request.user = self.user response = delete_answer(request, self.answer.id) self.assertEqual(response.status_code, REQUEST_REDIRECT) self.assertEqual(response.url, '/en/forum/topics/1/') def test_delete_answer_which_does_not_exit(self): self.answer.user = self.user self.answer.topic = self.topic self.answer.save() request = self.factory.get('/forum/deleteanswer/12/', follow=True) request.user = self.user response = delete_answer(request, 12) self.assertEqual(response.status_code, REQUEST_REDIRECT) def test_if_user_cant_delete_answer(self): self.answer.user.username = self.wrong_user self.answer.topic = self.topic self.answer.save() request = self.factory.get('/forum/deleteanswer/1/', follow=True) request.user = self.user response = delete_answer(request, self.answer.id) self.assertEqual(response.status_code, REQUEST_REDIRECT) self.assertEqual(response.url, '/en/forum/topics/1/') def test_if_user_can_see_delete_answer_button(self): self.answer.user = self.user self.answer.topic = self.topic self.answer.save() answers = Answer.objects.filter(topic=self.answer.topic) not_deletable_answer = [] not_deletable_answer.append(True) deletable_answers = __show_delete_answer_button__( answers, self.topic, self.user.username) self.assertEqual(deletable_answers, not_deletable_answer) def test_if_user_cant_see_delete_answer_button(self): self.answer.user = self.user self.answer.topic = self.topic self.answer.save() answers = Answer.objects.filter(topic=self.topic) not_deletable_answer = [] not_deletable_answer.append(False) deletable_answers = __show_delete_answer_button__( answers, self.topic, self.wrong_user) self.assertEqual(deletable_answers, not_deletable_answer) def test_if_user_can_see_choose_best_answer_button(self): topic_author = self.topic.author current_user = topic_author choose_best_answer = __show_choose_best_answer_button__( topic_author, current_user) self.assertEqual(choose_best_answer, True) def test_if_user_cant_see_choose_best_answer_button(self): topic_author = self.topic.author current_user = self.user choose_best_answer = __show_choose_best_answer_button__( topic_author, current_user) self.assertEqual(choose_best_answer, False) def test_if_topic_author_cant_choose_an_inexistent_answer(self): request = self.factory.get('/forum/best_answer/1/') request.user = self.user response = best_answer(request, self.answer.id) self.assertEqual(response.status_code, REQUEST_REDIRECT) self.assertEqual(response.url, '/en/forum/topics/') def test_if_topic_author_can_choose_best_answer(self): self.answer.user = self.user self.answer.topic = self.topic self.answer.save() request = self.factory.get('/forum/best_answer/1/') request.user = self.topic.author response = best_answer(request, self.answer.id) self.assertEqual(response.status_code, REQUEST_REDIRECT) self.assertEqual(response.url, '/en/forum/topics/1/')
class TestAnswerCreation(TestCase): answer = Answer() topic = Topic() user = User() user_topic = User() def setUp(self): self.answer.description = "<p>Description answer.</p>" self.user.email = "*****@*****.**" self.user.first_name = "TestUser" self.user.username = "******" self.user.is_active = True self.user_topic.email = "*****@*****.**" self.user_topic.first_name = "TestUser" self.user_topic.username = "******" self.user_topic.is_active = True self.user_topic.save() self.topic.title = 'Basic Topic' self.topic.subtitle = 'How test in Django' self.topic.author = self.user_topic self.topic.description = '<p>Text Basic Exercise.</p>' self.user.set_password('userpassword') self.user.save() self.topic.save() def test_if_answer_is_saved_database(self): self.answer.user = self.user self.answer.topic = self.topic self.answer.save() answer_database = Answer.objects.get(id=self.answer.id) self.assertEqual(str(answer_database), str(self.answer)) def test_if_creates_answer(self): self.answer.creates_answer(self.user, self.topic, self.answer.description) answer_database = Answer.objects.get(id=self.answer.id) self.assertEqual(str(answer_database), str(self.answer)) def test_answer_get_description(self): self.answer.creates_answer(self.user, self.topic, self.answer.description) answer_database = Answer.objects.get(id=self.answer.id) self.assertEqual(answer_database.description, self.answer.description) def test_answer_get_date(self): self.answer.creates_answer(self.user, self.topic, self.answer.description) answer_database = Answer.objects.get(id=self.answer.id) self.assertEqual(answer_database.date_answer, self.answer.date_answer) def test_answer_get_user(self): self.answer.creates_answer(self.user, self.topic, self.answer.description) answer_database = Answer.objects.get(id=self.answer.id) self.assertEqual(answer_database.user, self.answer.user) def test_answer_get_topic(self): self.answer.creates_answer(self.user, self.topic, self.answer.description) answer_database = Answer.objects.get(id=self.answer.id) self.assertEqual(answer_database.topic, self.answer.topic)
def process_data(self, **data): answer = Answer(author=self.user, parent=data['question'], **self.create_revision_data(True, **data)) answer.save() if 'invites' in data: answer.invites = data['invites'].strip() self.node = answer