Ejemplo n.º 1
0
def teacher_get_assignment_detail(request, submission_id):
    try:
        print('hello')
        assignment = ExperimentAssignment.objects.get(submission_case_id=submission_id)
    except ExperimentAssignment.DoesNotExist:
        error_data = {"detail": "not exist"}
        return Response(utils.generate_response(error_data, False), status=status.HTTP_404_NOT_FOUND)
    if request.method == 'GET':
        assignment_serializer = ExperimentAssignmentSerializer(assignment)
        answer = dict(assignment_serializer.data)
        case_id = answer['submission_case_id']
        case = ExperimentCaseDatabase.objects.get(experiment_case_id=case_id)
        case_serializer = ExperimentCaseDatabaseSerializer(case)
        answer['experiment_name'] = case_serializer.data['experiment_name']
        answer['experiment_case_name'] = case_serializer.data['experiment_case_name']
        answer['experiment_case_description'] = case_serializer.data['experiment_case_description']

        # 生成 minio 下载 url
        response_headers = {}
        file_token = assignment_serializer.data['submission_file_token']
        get_url = local_minio_client.presigned_url("GET",
                                                   DEFAULT_BUCKET,
                                                   file_token,
                                                   expires=DEFAULT_FILE_URL_TIMEOUT)
        response_headers['ASSIGNMENT_DOWNLOAD_URL'] = get_url

        ans = assignment_serializer.data
        ans.pop('submission_file_token', None)

        return Response(utils.generate_response(answer, True), headers=response_headers)
Ejemplo n.º 2
0
def assignment_teacher_detail(request, pk):
    """
    Retrieve, update or delete a assignment submission instance.
    """
    try:
        assignment = ExperimentAssignment.objects.get(pk=pk)
    except ExperimentAssignment.DoesNotExist:
        error_data = {"detail": "not exist"}
        return Response(utils.generate_response(error_data, False), status=status.HTTP_404_NOT_FOUND)

    if request.method == 'GET':
        serializer = ExperimentAssignmentSerializer(assignment)
        return Response(utils.generate_response(serializer.data, True))

    elif request.method == 'PUT':

        course_case_id = ExperimentAssignmentSerializer(assignment).data['course_case_id']
        course_object = CourseCase.objects.get(pk=course_case_id)
        course_id = CourseCaseSerializer(course_object).data['course_id']
        if request.user.character in [2, 3]:
            user_id = request.user.user_id
            # 检查是否绑定授课
            courses_set = set()
            teach_objects = Teach.objects.filter(teacher_id=user_id)
            teach_list = TeachSerializers(teach_objects, many=True)
            for teach in teach_list.data:
                courses_set.add(teach['course_id'])
            if course_id not in courses_set:
                response_data = {
                    "error_msg": 'permission denied, You have not yet bound the corresponding course'
                }
                return Response(utils.generate_response(response_data, False), status=status.HTTP_400_BAD_REQUEST)
        selected_attributions = [
            'submission_case_id',
            'submission_score',
            'submission_is_public',
            'submission_comments'
        ]
        update_data = {item: request.data[item] for item in selected_attributions}
        # 非责任教师不允许public
        if request.user.character in [2, 3]:
            update_data.pop('submission_is_public')
        serializer = ExperimentAssignmentSerializer(
            assignment, data=update_data, partial=True)
        if serializer.is_valid():
            serializer.save()
            return Response(utils.generate_response(serializer.data, True))
        return Response(utils.generate_response(serializer.errors, False), status=status.HTTP_400_BAD_REQUEST)

    elif request.method == 'DELETE':
        assignment.delete()
        response_data = {"detail": "have delete"}
        return Response(utils.generate_response(response_data, True), status=status.HTTP_204_NO_CONTENT)
Ejemplo n.º 3
0
def assignment_teacher_list(request, course_case_id):
    """
    List all the assignments with a specific course_case_id for teacher.
    """
    if request.method == 'GET':
        assignments = ExperimentAssignment.objects.all()
        # TODO: 鉴权

        assignments = assignments.filter(course_case_id=course_case_id)
        serializer = ExperimentAssignmentSerializer(assignments, many=True)
        return Response(utils.generate_response(serializer.data, True))
Ejemplo n.º 4
0
def get_course_case_statistics(request, course_case_id):
    course_case = CourseCase.objects.get(course_case_id=course_case_id)
    course_id = CourseCaseSerializer(course_case).data['course_id']
    print(course_id)
    takes = TakeCourseSerializer(
        TakeCourse.objects.all().filter(course_id=course_id), many=True).data
    # 选课人数
    user_id_list = [take['student_id'] for take in takes]
    print(user_id_list)
    # 提交人数
    assignment_list = ExperimentAssignmentSerializer(
        ExperimentAssignment.objects.all().filter(
            course_case_id=course_case_id),
        many=True).data
    submission_num = len(assignment_list)
    print(submission_num)
    # 已批改的人数
    remarked_list = [
        assignment for assignment in assignment_list
        if assignment['submission_score'] > 0
    ]
    remarked_num = len(remarked_list)
    # 分数段 0-60 61-70 71-80 81-90 91-100
    score_distributed = [0, 0, 0, 0, 0]
    for remarked in remarked_list:
        if remarked['submission_score'] <= 60:
            score_distributed[0] += 1
        elif remarked['submission_score'] <= 70:
            score_distributed[1] += 1
        elif remarked['submission_score'] <= 80:
            score_distributed[2] += 1
        elif remarked['submission_score'] <= 90:
            score_distributed[3] += 1
        elif remarked['submission_score'] <= 100:
            score_distributed[4] += 1
    answer = {
        'submitted_num': submission_num,
        'unsubmitted_num': len(user_id_list) - submission_num,
        'remarked_num': remarked_num,
        'unremarked_num': submission_num - remarked_num,
        'score_distributed': score_distributed,
    }
    return Response(utils.generate_response(answer, True))
Ejemplo n.º 5
0
def teacher_get_assignment_detail(request, submission_id):
    try:
        print('hello')
        assignment = ExperimentAssignment.objects.get(
            submission_case_id=submission_id)
    except ExperimentAssignment.DoesNotExist:
        error_data = {"detail": "not exist"}
        return Response(utils.generate_response(error_data, False),
                        status=status.HTTP_404_NOT_FOUND)
    if request.method == 'GET':
        assignment_serializer = ExperimentAssignmentSerializer(assignment)
        answer = dict(assignment_serializer.data)
        case_id = answer['submission_case_id']
        case = ExperimentCaseDatabase.objects.get(experiment_case_id=case_id)
        case_serializer = ExperimentCaseDatabaseSerializer(case)
        answer['experiment_name'] = case_serializer.data['experiment_name']
        answer['experiment_case_name'] = case_serializer.data[
            'experiment_case_name']
        answer['experiment_case_description'] = case_serializer.data[
            'experiment_case_description']
        return Response(utils.generate_response(answer, True))
Ejemplo n.º 6
0
def student_get_case_detail(request, course_case_id):
    """
    get a course case detail info for student
    """
    try:
        course_case = CourseCase.objects.get(course_case_id=course_case_id)
    except CourseCase.DoesNotExist:
        error_data = {"detail": "not exist"}
        return Response(utils.generate_response(error_data, False),
                        status=status.HTTP_404_NOT_FOUND)
    if request.method == 'GET':
        course_case_serializer = CourseCaseSerializer(course_case)
        case_id = course_case_serializer['case_id']
        # 补充案例信息
        case = ExperimentCaseDatabase.objects.get(
            experiment_case_id=case_id.value)
        case_serializer = ExperimentCaseDatabaseSerializer(case)
        answer = dict(course_case_serializer.data)
        answer['experiment_name'] = case_serializer.data['experiment_name']
        answer['experiment_case_name'] = case_serializer.data[
            'experiment_case_name']
        answer['experiment_case_description'] = case_serializer.data[
            'experiment_case_description']
        # 补充提交信息
        try:
            answer['is_submit'] = True
            assignment = ExperimentAssignment.objects.get(
                course_case_id=course_case_id,
                submission_uploader=request.user.user_id)
            assignment_serializer = ExperimentAssignmentSerializer(assignment)
            answer['is_public_score'] = assignment_serializer.data[
                'submission_is_public']
            answer['comment'] = assignment_serializer.data[
                'submission_comments']
            answer['score'] = assignment_serializer.data['submission_score']
            answer['answer'] = assignment_serializer.data[
                'submission_file_token']
        except ExperimentAssignment.DoesNotExist:
            answer['is_submit'] = False
        return Response(utils.generate_response(answer, True))
Ejemplo n.º 7
0
def teacher_public_all_assignments(request, course_case_id):
    if request.method == 'PUT':
        assignment_model_list = ExperimentAssignment.objects.all().filter(course_case_id=course_case_id)
        assignment_list = ExperimentAssignmentSerializer(assignment_model_list, many=True)
        for index, assignment in enumerate(assignment_list.data):
            change_data = assignment
            change_data['submission_is_public'] = True
            print(change_data)
            assignment_serializer = ExperimentAssignmentSerializer(assignment_model_list[index], data=change_data, partial=True)
            if assignment_serializer.is_valid():
                assignment_serializer.save()
            else:
                print(assignment_serializer.errors)
                error_data = {"detail": "update error"}
                return Response(utils.generate_response(error_data, False))
        success_data = {"detail": "success"}
        return Response(utils.generate_response(success_data, True), status=status.HTTP_201_CREATED)
Ejemplo n.º 8
0
def assignment_student_list(request):
    """
    List all assignments for a particular student.
    Submit a assignment
    """
    if request.method == 'GET':
        assignments = ExperimentAssignment.objects.filter(
            submission_uploader=request.user.user_id)
        # assignments = assignments.filter()
        serializer = ExperimentAssignmentSerializer(assignments, many=True)
        return Response(
            utils.generate_response(
                utils.student_assignments_filter(serializer.data), True))

    elif request.method == 'POST':
        # 输入字段检测
        if not utils.is_submission_valid(request.data):
            return Response(utils.generate_response({"error_msg": '参数错误'},
                                                    False),
                            status=status.HTTP_400_BAD_REQUEST)

        # 获取学生选过的课程集合
        user_id = request.user.user_id
        course_objects = TakeCourse.objects.filter(student_id=user_id)
        course_id_list = []
        tmp_serializer = TakeCourseSerializer(course_objects, many=True)

        for item in tmp_serializer.data:
            course_id_list.append(item['course_id'])
        course_id_set = set(course_id_list)

        # 检测上传的课程是否是在选过的课程中
        course_id = request.data['course_id']
        if course_id not in course_id_set:
            error_res = {"error_msg": '提交的课程尚未加入'}
            return Response(utils.generate_response(error_res, False),
                            status=status.HTTP_400_BAD_REQUEST)
        print('wzj')
        try:
            course_case_object = CourseCase.objects.get(
                pk=request.data['course_case_id'])
        except CourseCase.DoesNotExist:
            error_data = {"detail": "course case not exist"}
            return Response(utils.generate_response(error_data, False),
                            status=status.HTTP_404_NOT_FOUND)

        course_case = CourseCaseSerializer(course_case_object).data

        # DDL检验
        if not utils.check_ddl(course_case['case_end_timestamp']):
            return Response(
                utils.generate_response({"error_msg": 'Assignment is due'},
                                        False))

        request.data['submission_uploader'] = user_id

        # upload url
        response_headers = {}
        file_display_name = request.data["submission_file_name"]
        random_hex_string = ('%030x' % random.randrange(16**30))
        file_token = f"{EXPERIMENT_CASE_PREFIX}/{random_hex_string}/{file_display_name}"
        post_url = local_minio_client.presigned_url(
            "PUT",
            DEFAULT_BUCKET,
            file_token,
            expires=DEFAULT_FILE_URL_TIMEOUT)
        request.data['submission_file_token'] = file_token
        response_headers['SUBMISSION_UPLOAD_URL'] = post_url

        serializer = ExperimentAssignmentSerializer(data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response(utils.generate_response(serializer.data, True),
                            headers=response_headers,
                            status=status.HTTP_201_CREATED)
        return Response(utils.generate_response(serializer.errors, False),
                        status=status.HTTP_400_BAD_REQUEST)
Ejemplo n.º 9
0
def assignment_student_detail(request, pk):
    """
    Retrieve, update or delete a assignment submission instance.
    """
    try:
        assignment = ExperimentAssignment.objects.get(pk=pk)
    except ExperimentAssignment.DoesNotExist:
        error_data = {"detail": "not exist"}
        return Response(utils.generate_response(error_data, False),
                        status=status.HTTP_404_NOT_FOUND)

    if request.method == 'GET':

        serializer = ExperimentAssignmentSerializer(assignment)

        # 生成 minio 下载 url
        response_headers = {}
        file_token = serializer.data['submission_file_token']
        get_url = local_minio_client.presigned_url(
            "GET",
            DEFAULT_BUCKET,
            file_token,
            expires=DEFAULT_FILE_URL_TIMEOUT)
        response_headers['ASSIGNMENT_DOWNLOAD_URL'] = get_url

        ans = serializer.data
        ans.pop('submission_file_token', None)
        return Response(utils.generate_response(
            utils.student_assignments_filter([ans])[0], True),
                        headers=response_headers)

    elif request.method == 'PUT':
        old_assignment = ExperimentAssignmentSerializer(assignment).data
        # Public检验
        if not utils.is_submission_retrieve_valid(old_assignment,
                                                  request.data):
            return Response(utils.generate_response(
                {"error_msg": 'permission denied'}, False),
                            status=status.HTTP_400_BAD_REQUEST)

        # DDL检验
        course_case_object = CourseCase.objects.get(
            course_case_id=old_assignment['course_case_id'])
        course_case = CourseCaseSerializer(course_case_object).data
        if not utils.check_ddl(course_case['case_end_timestamp']):
            return Response(
                utils.generate_response({"error_msg": 'Assignment is due'},
                                        False))

        # upload url
        response_headers = {}
        file_token = old_assignment['submission_file_token']
        post_url = local_minio_client.presigned_url(
            "PUT",
            DEFAULT_BUCKET,
            file_token,
            expires=DEFAULT_FILE_URL_TIMEOUT)
        response_headers['SUBMISSION_UPLOAD_URL'] = post_url

        serializer = ExperimentAssignmentSerializer(assignment,
                                                    data=old_assignment,
                                                    partial=True)
        if serializer.is_valid():
            serializer.save()
            return Response(utils.generate_response(serializer.data, True),
                            headers=response_headers)
        return Response(utils.generate_response(serializer.errors, False),
                        status=status.HTTP_400_BAD_REQUEST)

    elif request.method == 'DELETE':
        if not utils.is_submission_delete_valid(
                ExperimentAssignmentSerializer(assignment).data):
            response_data = {"error_msg": 'permission denied'}
            return Response(utils.generate_response(response_data, False),
                            status=status.HTTP_400_BAD_REQUEST)
        assignment.delete()
        response_data = {"detail": "have delete"}
        return Response(utils.generate_response(response_data, True),
                        status=status.HTTP_204_NO_CONTENT)