def save(self, **kwargs): answer = Answer(**self.cleaned_data) # question_id = re.findall(r'/question/(\d+)/',kwargs['HTTP_REFERER']) answer.author_id = self._user # answer.question_id = int(question_id[0]) answer.save() return answer
def save(self): self.cleaned_data['author'] = self.user answer = Answer(**self.cleaned_data) # answer.question = Question.objects.get(id=self._question) answer.save() # answer = Answer.objects.create(text=self.cleaned_data['text'], question=self.cleaned_data['question_id']) return answer
def save(self): newanswer = Answer(text=self.cleaned_data['text'], author=self.cleaned_data['author']) newanswer.question = Question.objects.get( pk=self.cleaned_data['question']) newanswer.save() return self.cleaned_data['question']
def save(self, question_id): question_id = self.cleaned_data.pop('question') q = Question.objects.get(id=question_id) answer = Answer(question=q, **self.cleaned_data) answer.author = self._author answer.save() return answer
def save(self): self.cleaned_data['question'] = Question.objects.get(id=self.cleaned_data['question']) self.cleaned_data['author'] = self._user #print self.cleaned_data answer = Answer(**self.cleaned_data) answer.save() return answer
def save(self, author): data = self.cleaned_data data['question'] = Question.objects.get(pk=data['question']) answer = Answer(**self.cleaned_data) answer.author = author answer.save() return answer
def save(self): self.cleaned_data['author'] = self.user self.cleaned_data['question'] = get_object_or_404( Question, pk=self.cleaned_data['question']) answer = Answer(**self.cleaned_data) answer.save() return answer
def save(self): answer = Answer(text = self.cleaned_data['text'], question=Question.objects.get(pk=self.cleaned_data['question']), author=self._user) answer.save() return answer
def save(self): q = get_object_or_404(Question, pk=self.cleaned_data['question']) a = Answer(question=q, text=self.cleaned_data['text'], author=self._user) a.save() return a
def save(self): author = User.objects.get(username=self.cleaned_data['author']) answer = Answer(text=self.cleaned_data['text'], question_id=self.cleaned_data['question'], author=author) answer.save() return answer
def post(self, request): if not request.user.is_authenticated(): return HttpResponse(json.dumps({ "status": "fail", "msg": "用户未登录" }), content_type='application/json') choices = request.POST.get('choices', '') question_id = request.POST.get('question_id', '') if int(question_id) > 0 and choices: exist_records = Answer.objects.filter(user_id=request.user.id, question_id=int(question_id)) if exist_records: return HttpResponse('{"status":"success","msg":"你已经回答过这个问题"}', content_type='application/json') answer = Answer() question = Question.objects.get(id=question_id) question_standard_choices = QuestionStandardAnswers.objects.get( question_id=question_id).choice_position answer.question_id = question.id answer.content = choices answer.user = request.user if choices != question_standard_choices: answer.correct = False answer.save() return HttpResponse('{"status":"success","msg":"答案错误"}', content_type='application/json') answer.save() return HttpResponse('{"status":"success","msg":"答案正确"}', content_type='application/json') else: return HttpResponse('{"status":"fail","msg":"回答失败"}', content_type='application/json')
def question_details(request, id): if request.method == 'GET': question = get_object_or_404(Question, id=id) form = AnswerForm() url_answer = reverse(question_details, args=(id, )) try: answers = Answer.objects.filter(question_id__exact=id) except Answer.DoesNotExist: answers = None return render( request, 'qa/question_details.html', { 'question': question, 'answers': answers, 'form': form, 'url': url_answer, 'user': request.user, }) if request.method == 'POST' and request.user.is_authenticated(): form = AnswerForm(request.POST) if form.is_valid(): ans = Answer() ans.save_data(form, request.user.id, id) return HttpResponseRedirect(reverse(question_details, args=(id, ))) else: return HttpResponse('OK') else: return HttpResponseRedirect(reverse(qa_login, ))
def save(self): q_id = self.cleaned_data['question'] q = Question.objects.get(id=q_id) self.cleaned_data['question'] = q self.cleaned_data['author'] = self._user a = Answer(**self.cleaned_data) a.save() return a
def save(self): question = Question.objects.get(pk = self.cleaned_data['question']) self.cleaned_data['question'] = question self.cleaned_data['author'] = self._user answer = Answer(**self.cleaned_data) answer.save() return answer
def save(self): if self._user.is_anonymous(): self.cleaned_data['author_id'] = 1 else: self.cleaned_data['author'] = self._user answer = Answer(**self.cleaned_data) answer.save() return answer
def save(self): if self._user.is_anonymous(): self.cleaned_data['author_id'] = 1 else: self.cleaned_data['author'] = self._user answer = Answer(**self.cleaned_data) answer.save() return answer
def save(self): question = get_object_or_404(Question, id=self.cleaned_data['question']) answer = Answer(text=self.cleaned_data['text'], question=question, author=self._user) answer.save() return answer
def test_model(request, *args, **kwargs): user = User(username='******', password='******') user.save() question = Question(title='qwe', text='qwe', author=user) question.save() answer = Answer(text='qwe', question=question, author=user) answer.save() return HttpResponse('OK', status=200)
def save(self): self.cleaned_data['question'] = Question.objects.get( id=self.cleaned_data['question']) answer = Answer(**self.cleaned_data) answer.author_id = 1 #answer.question = Question.objects.get(id = self.cleaned_data[question]) answer.save() return answer
def save(self): a = Answer(text=self.cleaned_data['text'], question_id=self.cleaned_data['question']) a.save() return a
def save(self): self.cleaned_data['question'] = get_object_or_404( Question, pk=self.cleaned_data['question']) self.cleaned_data['author_id'] = 1 answer = Answer(**self.cleaned_data) answer.save() return answer
def save(self): self.cleaned_data['question'] = get_object_or_404(Question,pk=self.cleaned_data['question']) if self._user.is_anonymous(): self.cleaned_data['author_id'] = 1 else: self.cleaned_data['author'] = self._user answer = Answer(**self.cleaned_data) answer.save() return answer
def save(self): user = User.objects.get(id=3) self.cleaned_data['author'] = user self.cleaned_data['added_at'] = datetime.now() self.cleaned_data['question'] = self.question answer = Answer(**self.cleaned_data) answer.save() return answer
def save(self): self.cleaned_data['question'] = get_object_or_404( Question, pk=self.cleaned_data['question']) if self._user.is_anonymous(): self.cleaned_data['author_id'] = 1 else: self.cleaned_data['author'] = self._user answer = Answer(**self.cleaned_data) answer.save() return answer
def save(self): data = { 'text': self.cleaned_data['text'], 'added_at': datetime.now().strftime("%Y-%m-%d %H:%M:%S"), 'question_id': int(self.cleaned_data['question']), 'author_id': self._user.id, } answer = Answer(**data) answer.save() return answer
def answer(request, *args, **kwargs): answer = Answer(author=request.user) form = AnswerForm(request.POST, instance=answer) if form.is_valid(): answer.save() url = answer.question.get_url() return HttpResponseRedirect(url) q = get_object_or_404(Question, id=form['question'].data) return HttpResponseRedirect(q.get_url())
def save(self): data = { 'text': self.cleaned_data['text'], 'added_at': datetime.now().strftime("%Y-%m-%d %H:%M:%S"), 'question_id': int(self.cleaned_data['question']), 'author_id': self._user.id, } answer = Answer(**data) answer.save() return answer
def save(self): #answer = Answer(**self.cleaned_data) u = User.objects.all()[0] #answer.question = Question.objects.get(id=self.cleaned_data["question"]) #answer.author = u q = Question.objects.get(id=self.cleaned_data["question"]) t = self.cleaned_data["text"] answer = Answer(text = t, question = q, author = u) answer.save() return answer
def save(self): question_id = self.cleaned_data['question'] data = {'text': self.cleaned_data['text'], #'question': Question.objects.get(pk=question_id), 'question_id': int(question_id), 'author': self._user } #answer = Answer(**self.cleaned_data) answer = Answer(**data) answer.save() return answer
def save(self): # self.cleaned_data['question'] = Question.objects.filter(pk = self.cleaned_data['question']) self.cleaned_data['question'] = get_object_or_404(Question, pk=self.cleaned_data['question']) if self._user.is_anonymous(): self.cleaned_data['author_id'] = 1 else: self.cleaned_data['author'] = self._user post = Answer(**self.cleaned_data) post.save() return post
def question(request, id): if request.method == 'POST': form = AnswerForm(request.POST) if form.is_valid(): answer = Answer(**form.cleaned_data) answer.save() question = get_object_or_404(Question, pk=id) form = AnswerForm() return render(request, 'qa/question.html', { 'question': question, 'form': form })
def save(self): question = Question.objects.get(id = self.cleaned_data['question']) # if not question.id: # return HttpResponse('question id is empty') self.cleaned_data['author'] = self._user answer = Answer(text=self.cleaned_data['text'], author = self.cleaned_data['author'], question=question) #answer = Answer(**self.cleaned_data) answer.save() return answer
def test_question(self): user, _ = User.objects.get_or_create(username='******', password='******') try: question = Question(title='qwe', text='qwe', author=user) question.save() except: assert False, "Failed to create question model, check db connection" try: answer = Answer(text='qwe', question=question, author=user) question.save() answer.save() except: assert False, "Failed to create answer model, check db connection"
def perform_destroy(self, instance): answer = Answer() qs = answer.collection.find( {"comments": { "$all": [str(instance.get("_id"))] }}) if qs.count() == 1: answer_obj = qs.next() comments = answer_obj.get("comments") # delete comment id from answer comment list comments.remove(str(instance.get("_id"))) data = {"comments": comments} answer.update({"_id": answer_obj.get("_id")}, data) self.comment.remove({"_id": instance.get("_id")})
def has_object_permission(self, request, view, obj): answer_id = obj answer = Answer() qs = answer.collection.find({"_id": ObjectId(answer_id)}) if qs.count() == 1: return True return False
def post(self, request): if not request.user.is_authenticated(): return HttpResponse(json.dumps({ "status": "fail", "msg": "用户未登录" }), content_type='application/json') content = request.POST.get('content', '') question_id = request.POST.get('question_id', '') if int(question_id) > 0 and content: answer = Answer() question = Question.objects.get(id=question_id) answer.question_id = question.id answer.content = content answer.user = request.user answer.save() return HttpResponse(json.dumps({ "status": "success", "msg": "回答成功", "id": answer.id }), content_type='application/json') else: return HttpResponse('{"status":"fail","msg":"回答失败,请重新回答"}', content_type='application/json')
def save(self): a = Answer() a.text = self.cleaned_data['text'] #a.author = self._user a.author_id = self._user.pk a.question_id = self.cleaned_data['question'] a.save() return a
def save(self): answer = Answer() answer.text = self.cleaned_data['text'] answer.question_id = self.cleaned_data['question'] answer.added_at = datetime.now() answer.author = self._user answer.save() return answer
def answer_question(request): if not request.user.is_authenticated(): return redirect('/') else: q_id = request.POST['question'] content = request.POST['answer'] question = Question.objects.get(pk=q_id) question.update_timestamp() answer = Answer(author=request.user, question=question, content=content) answer.save() message_followers(question=question, actor=request.user) ## Add the user to the list of followers for this question question.add_follower(request.user) if question.course.has_approved(request.user): answer.approved = True answer.save() return redirect('/question/%s' % q_id) return redirect('/question/%s?msg=amod' % q_id)
def create(self, validated_data): comment = Comment() request = self.context.get("request") token = request.headers.get("Authorization").split()[1] payload = jwt_decode_handler(token) user_id = payload.get("user_id") comment.set_user_id(user_id) comment.set_message(validated_data.get("message")) data = { "user_id": ObjectId(user_id), "message": comment.get_message(), "date_created": comment.get_date_created(), } c_id = comment.save(data) comment.set_id(c_id) # Add comment's id to question comment list if self.context.get("question_id"): question = Question() qs = question.collection.find( {"_id": ObjectId(self.context.get("question_id"))}) if qs.count() == 1: question_obj = qs.next() comments_list = question_obj.get("comments") comments_list.append(str(comment.get_id())) data = {"comments": comments_list} question.update({"_id": question_obj.get("_id")}, data) # Add comment's id to answer comment list elif self.context.get("answer_id"): answer = Answer() qs = answer.collection.find( {"_id": ObjectId(self.context.get("answer_id"))}) if qs.count() == 1: answer_obj = qs.next() comments_list = answer_obj.get("comments") comments_list.append(str(comment.get_id())) data = {"comments": comments_list} answer.update({"_id": answer_obj.get("_id")}, data) return comment
def question(request, id): quest = get_object_or_404(Question, id=id) try: answers = Answer.objects.filter(question=quest).all() except Answer.DoesNotExist: answers = [] a = Answer(question=quest, author=request.user) form = AnswerForm(instance=a) return render(request, 'question.html', { 'quest': quest, 'answers': answers, 'form': form })
def answer(request): """Answer post form.""" if request.method == 'POST': form = AnswerForm(request.POST) if form.is_valid(): question = form.question params = { 'text': form.cleaned_data['text'], 'question': question } if request.user.is_authenticated(): params.update({'author': request.user}) answer = Answer(**params) answer.save() return HttpResponseRedirect( reverse('question', kwargs={'id': question.id}) ) return HttpResponseRedirect(reverse('root', kwargs={}))
def test_answer(self): from qa.models import Answer from qa.models import Question try: text = Answer._meta.get_field('text') except FieldDoesNotExist: assert False, "text field does not exist in Answer model" assert isinstance(text, TextField), "text field is not TextField" try: question = Answer._meta.get_field('question') except FieldDoesNotExist: assert False, "question field does not exist in Answer model" assert isinstance(question, ForeignKey), "question field is not ForeignKey" if hasattr(question, 'related'): assert question.related.parent_model == Question, "question field does not refer Question model" elif hasattr(question, 'rel'): assert question.rel.to == Question, "question field does not refer Question model" try: added_at = Answer._meta.get_field('added_at') except FieldDoesNotExist: assert False, "added_at field does not exist in Answer model" assert isinstance(text, DateField) or isinstance( added_at, DateField), "added_at field is not DateTimeField" try: author = Answer._meta.get_field('author') except FieldDoesNotExist: assert False, "author field does not exist in Answer model" assert isinstance(author, ForeignKey), "author field is not ForeignKey" if hasattr(author, 'related'): assert author.related.parent_model == User, "author field does not refer User model" elif hasattr(author, 'rel'): assert author.rel.to == User, "author field does not refer User model" user, _ = User.objects.get_or_create(username='******', defaults={ 'password': '******', 'last_login': timezone.now() }) question = Question.objects.create(title='qwe', text='qwe', author=user) try: answer = Answer(text='qwe', question=question, author=user) question.save() except: assert False, "Failed to create answer model, check db connection"
def run(self): answer = Answer() answer.question = self.question answer.user = self.user question_title = self.question.title question_description = self.question.description if USE_SERVER: answer_content = self.reuse(question_title, question_description) if not answer_content: answer_content = self.answer(question_title, question_description) if answer_content: answer.answer_text = answer_content answer.save()
def save(self): answer = Answer(**self.cleaned_data) #t7 user,_ = User.objects.get_or_create(username='******',password='******') #t7 answer.author=user answer.author_id = self._user.id if self._user.id is None: user, _ = User.objects.get_or_create(username='******', password='******') answer.author = user else: pass answer.save() return answer
def save(self): answer = Answer(**self.cleaned_data) answer.save() return answer
def save(self, user): q = get_object_or_404(Question, id=self.cleaned_data['question']) a = Answer(author=user, question=q, text=self.cleaned_data['text']) a.save() return a
def save(self): a = Answer(**self.cleaned_data) a.save() return a
def save(self): question = Question.objects.get(id=self.cleaned_data['question']) answer = Answer(text=self.cleaned_data['text'], question=question) answer.save() return answer
["sixth","what is the time now??", datetime.now(),17,56,User.objects.get(username="******")], ["seventh","how much is the fish",datetime.now(),12,8,User.objects.get(username="******")], ["eghth","WTF 0_o ???", datetime.now(),6,14,User.objects.get(username="******")], ["ninegth","what is the time now??", datetime.now(),10,20,User.objects.get(username="******")], ["tenth","how much is the fish",datetime.now(),0,0,User.objects.get(username="******")], ["eleven","WTF 0_o ???", datetime.now(),14,56,User.objects.get(username="******")], ["tvelve","what is the time now??", datetime.now(),22,34,User.objects.get(username="******")], ["forteen","how much is the fish",datetime.now(),24,56,User.objects.get(username="******")], ["sixteenth","WTF 0_o ???", datetime.now(),23,32,User.objects.get(username="******")], ["eigtheenth","what is the time now??", datetime.now(),34,17,User.objects.get(username="******")], ] for question in questions: title,text,added_at,rating,likes,author = question question=Question(title=title,text=text,added_at=added_at,rating=rating,likes=likes,author=author) question.save() answers = [ ["dorogo",datetime.now(),Question.objects.get(title="first"),User.objects.get(username="******")], ["togo!togo",datetime.now(),Question.objects.get(title="second"),User.objects.get(username="******")], ["13:00 o clock!!", datetime.now(),Question.objects.get(title="second"),User.objects.get(username="******")], ] for answer in answers: text,added_at,question,author = answer answer=Answer(text=text,added_at=added_at,question=question,author=author) answer.save()
def save(self): answer = Answer(**self.cleaned_data) answer.author_id = self._user.id answer.save() return answer
def save(self): user, _ = User.objects.get_or_create(username='******', defaults={'password': '******'}) ans = Answer(text=self.cleaned_data['text'], question=self.cleaned_data['question'], author=user) ans.save() return ans
def save(self): self.cleaned_data['question'] = Question(self.cleaned_data['question']) answer = Answer(**self.cleaned_data) answer.author_id = self.user.id answer.save() return answer
def save(self): if self.is_valid(): pass answer = Answer(text=self.cleaned_data['text'], question_id=self.cleaned_data['question']) answer.save() return answer
def save(self): self.cleaned_data['author'] = self._user answer = Answer(**self.cleaned_data) answer.save() return answer
def save(self): newanswer = Answer(text=self.cleaned_data['text'], author=self.cleaned_data['author']) newanswer.question = Question.objects.get(pk=self.cleaned_data['question']) newanswer.save() return self.cleaned_data['question']
def save(self): answer = Answer(**self.cleaned_data) answer.author_id = self._user.id answer.save() return answer
def save(self): a = Answer(text=self.cleaned_data['text'], question_id=self.cleaned_data['question']) a.save() return a