Exemplo n.º 1
0
def create():
    student = User.get_or_none(User.identity_card == get_jwt_identity())
    params = request.json
    new_submission = Answer(exam_id=params.get("exam_id"),
                            student_id=student.id,
                            submission=params.get("submission"))

    if student.roles == "student":
        # check if there's existing data in Answer's table
        check_table = Answer.get_or_none(
            (Answer.exam_id == new_submission.exam.id)
            & (Answer.student_id == new_submission.student.id))
        if not check_table:
            # putting the txt from submission to a tempfile
            temp = tempfile.TemporaryFile()
            temp.write(new_submission.submission.encode("utf-8"))

            # get path from S3
            file_path = upload_file_to_s3(temp, get_jwt_identity(),
                                          new_submission.exam)

            # save path into the table
            answer = Answer(exam=new_submission.exam,
                            submission=file_path,
                            student=student.id)

            if answer.save():
                response = {
                    "Message": "Successfully saved",
                    "Status": "Success",
                    "exam.id": answer.exam_id,
                    "material.id": answer.exam.material_id,
                    "material.name": answer.exam.material.name,
                    "subject.id": answer.exam.material.classroom.subject.id,
                    "subject.name":
                    answer.exam.material.classroom.subject.name,
                    "staff.id": answer.exam.staff_id,
                    "staff.name": answer.exam.staff.full_name,
                    "student.id": answer.student_id,
                    "student.name": answer.student.full_name,
                    "submission": answer.full_file_url
                }
            else:
                response = {
                    "Message": "Action unsuccessful",
                    "Status": "Failed",
                }
        else:
            response = {"Message": "There is an existing submission!"}
    else:
        response = {"Message": "You are not allow to perform this action!"}
    return jsonify(response)