Example #1
0
def PostAnswer(request):
    """!@brief Function for posting an answer which is part of the request body
    @details this function extracts the answer details from the request body, then creates a new entry in the answer table with the corresponding details. It also creates a notification for a user whose question has been provided an answer and increase the field numAnswers for that question by 1
    @post New entries have been created in the answer table as well as the notification table along with an update in th question table to accomodate for the new changes
    @return returns the updated question object
    """
    serializer = AnswerSerializer(data=request.data)
    if serializer.is_valid():
        ans = serializer.validated_data
        sender = ans['user']
        question = ans['question']
        receiver = question.user
        answer = Answer(user=sender,
                        username=sender.name,
                        userdepartment=sender.department,
                        userbio=sender.bio,
                        userdegree=sender.degree,
                        userspecialization=sender.specialization,
                        content=ans['content'],
                        question=question)
        answer.save()
        notification = Notification(receiver=receiver,
                                    sender=sender,
                                    sendername=sender.name,
                                    question=question,
                                    code=3)
        notification.save()
        #print(question.numAnswers)
        question.numAnswers = question.numAnswers + 1
        question.save()
        return QuestionResponse(sender.id, question.id)
    return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Example #2
0
 def apply_challenge(self, request):
     try:
         user_id = request.query_params.get("user_id", None)
         completed = request.query_params.get("completed", None)
         id = request.query_params.get("id", None)
         risk = request.query_params.get("risk", None)
         diff_risk = request.query_params.get("diff_risk", None)
         if user_id and completed and id and risk and diff_risk:
             customer = Customer.objects.filter(vk_id=user_id)
             if customer.count() == 1:
                 customer = customer[0]
                 customer.risk = float(risk)
                 challenge = Challenge.objects.get(id=id)
                 answers = challenge.question.answers \
                  .filter(customer__vk_id=user_id).order_by("date")
                 if answers:
                     ans = answers[0]
                     ans.value = True if completed.lower(
                     ) == "true" else False
                     ans.save()
                 else:
                     ans = Answer(customer=customer,
                                  value=True
                                  if completed.lower() == "true" else False,
                                  question=challenge.question)
                     ans.save()
                 customer.diff_risk = float(diff_risk)
                 customer.save()
                 return Response(data={"is": True})
         return Response(data={"is": False})
     except:
         print(traceback.format_exc())
         return Response(data={"is": False})
Example #3
0
    def post(self, request, room_number):
        request.session.save()

        if 'room_admin_uuid' not in request.session or request.session['room_admin_uuid'] != room_number:
            return Response({"error": "you can't post polls for this room"}, status=401)
        else:
            room = get_or_none(Room, number=room_number)
            if room:
                json = JSONParser().parse(request)
                poll = Poll(room=room, title=json['title'], isExclusive=json['isExclusive'])
                poll.save()

                for item in json['answers']:
                    answer = Answer(poll=poll, title=item['title'], votes=0)
                    answer.save()

                Group('room-%s' % poll.room.number).send({
                    'text': dumps({
                        'type': 'poll',
                        'action': 'create',
                        'data': PollSerializer(poll).data
                    })
                })

                return Response({}, status=201)

            else:
                return Response({"error": "no room " + room.number + " found"}, status=404)
Example #4
0
def add_answer(request, user_id, question_id):

    mentor = Mentor.objects.get(id=user_id)
    question = Question.objects.get(id=question_id)
    answer = Answer()
    answer.author = mentor
    answer.question = question
    answer.title = request.data['title']
    answer.description = request.data['description']
    answer.language = request.data['language']
    answer.save()
    return Response(AnswerSerializer(answer).data,
                    status=status.HTTP_201_CREATED)
Example #5
0
def save_question(question, answer, detractors):
    operator = get_math_operation(question)
    has_negative_values = check_for_negative_values(answer, *detractors)

    q = Question(text=question, operation_type=operator, has_negative_values=has_negative_values)
    q.save()

    all_answers = []
    a = Answer(text=answer, is_correct_answer=True)
    a.save()
    all_answers.append(a)

    for d in detractors:
        a = Answer(text=d, is_correct_answer=False)
        a.save()
        all_answers.append(a)

    q.answers.add(*all_answers)
Example #6
0
    def post(self, question_id):
        request_data = request.get_json()

        user = User.query.get("16")
        question = Question.get_or_404(question_id)

        answer_schema = AnswerSchema(
            only=["id", "title", "created_at", "updated_at", "parent_id"])
        answer_data = answer_schema.load_object_into_schema(request_data)
        answer_data["answerer"] = user
        answer_data["answer"] = question
        answer_data["parent_id"] = 12
        answer = Answer(**answer_data)
        answer.save()

        return jsonify({
            "data": answer_schema.dump_object_into_schema(answer),
            "status": "success",
            "message": SUCCESS_MESSAGES["created"].format("Answer")
        })
Example #7
0
 def create(self, validated_data):
     answer = Answer(**validated_data)
     answer.author = self.context['request'].user
     answer.save()
     return answer