def teach_list(request): """ List all teach relations, or create a new tech relation. """ if request.method == 'GET': teach_list = Teach.objects.all() serializer = TeachSerializers(teach_list, many=True) answer = serializer.data for index, teach in enumerate(answer): course_id = teach['course_id'] course_info = CourseSerializers(Course.objects.get(course_id=course_id)).data answer[index]['course_name'] = course_info['course_name'] answer[index]['course_description'] = course_info['course_description'] answer[index]['course_credit'] = course_info['course_credit'] return Response(generate_response(serializer.data, True)) elif request.method == 'POST': teacher_id = request.data['teacher_id'] try: teacher = User.objects.get(user_id=teacher_id) except ObjectDoesNotExist: error_msg = {"detail": "被邀请人ID不存在"} return Response(generate_response(error_msg, False), status=status.HTTP_404_NOT_FOUND) teacher = User.objects.get(pk=teacher_id) if teacher.character not in [2, 3]: error_msg = {"detail": "被邀请人身份不符合要求"} return Response(generate_response(error_msg, False), status=status.HTTP_400_BAD_REQUEST) serializer = TeachSerializers(data=request.data) if serializer.is_valid(): serializer.save() return Response(generate_response(serializer.data, True), status=status.HTTP_201_CREATED) return Response(generate_response(serializer.errors, False), status=status.HTTP_400_BAD_REQUEST)
def teach_detail(request, teach_id): """ delete a teach relation. """ try: teach = Teach.objects.get(pk=teach_id) except Teach.DoesNotExist: error_msg = {"detail": "object not exists"} return Response(generate_response(error_msg, False), status=status.HTTP_404_NOT_FOUND) teach.delete() msg = {"detail": "has been deleted successfully"} return Response(generate_response(msg, True), status=status.HTTP_204_NO_CONTENT)
def courses_list(request): """ List all courses, or create a new course. """ if request.method == 'GET': user_id = request.user.user_id if request.user.character in [2, 3]: teaches_objects = Teach.objects.filter(teacher_id=user_id) teaches_list = TeachSerializers(teaches_objects, many=True).data course_list = [] for teach in teaches_list: course = CourseSerializers(Course.objects.get(course_id=teach['course_id'])).data course_list.append(course) elif request.user.character == 1: course_list = CourseSerializers(Course.objects.all(), many=True).data else: # for student takes_list = TakeCourseSerializer(TakeCourse.objects.filter(student_id=user_id), many=True).data course_list = [] for take in takes_list: course = CourseSerializers(Course.objects.get(course_id=take['course_id'])).data course_list.append(course) return Response(generate_response(course_list, True)) elif request.method == 'POST': serializer = CourseSerializers(data=request.data) if request.user.character in [2, 3, 4]: response_data = {"error_msg": 'permission denied'} return Response(utils.generate_response(response_data, False), status=status.HTTP_400_BAD_REQUEST) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def course_detail(request, course_id): """ Retrieve or update a course instance. """ try: course = Course.objects.get(pk=course_id) except Course.DoesNotExist: error_msg = {"detail": "object not exists"} return Response(generate_response(error_msg, False), status=status.HTTP_404_NOT_FOUND) user_id = request.user.user_id if request.user.character in [2, 3]: if not is_teacher_teach_course(teacher_id=user_id, course_id=course_id): response_data = {"error_msg": 'permission denied'} return Response(utils.generate_response(response_data, False), status=status.HTTP_400_BAD_REQUEST) elif request.user.character == 4: if not is_student_within_course(student_id=user_id, course_id=course_id): response_data = {"error_msg": 'permission denied'} return Response(utils.generate_response(response_data, False), status=status.HTTP_400_BAD_REQUEST) if request.method == 'GET': serializer = CourseSerializers(course) return Response(generate_response(serializer.data, True)) elif request.method == 'PUT': teacher = User.objects.get(user_id=request.user.user_id) if teacher.character not in [1]: error_msg = {"detail": "没有权限"} return Response(generate_response(error_msg, False), status=status.HTTP_400_BAD_REQUEST) serializer = CourseSerializers(course, data=request.data, partial=True) if serializer.is_valid(): serializer.save() return Response(generate_response(serializer.data, True)) return Response(generate_response(serializer.errors, False), status=status.HTTP_400_BAD_REQUEST) elif request.method == 'DELETE': if request.user.character in [1]: course.delete() response_data = {"detail": "have delete"} return Response(generate_response(response_data, True), status=status.HTTP_204_NO_CONTENT) else: error_msg = {"detail": "没有权限"} return Response(generate_response(error_msg, False), status=status.HTTP_400_BAD_REQUEST)
def take_list(request): if request.method == 'GET': take_list = TakeCourse.objects.all() serializer = TakeCourseSerializer(take_list, many=True) answer = serializer.data for index, take in enumerate(answer): course_id = take['course_id'] course_info = CourseSerializers( Course.objects.get(course_id=course_id)).data answer[index]['course_name'] = course_info['course_name'] answer[index]['course_description'] = course_info[ 'course_description'] answer[index]['course_credit'] = course_info['course_credit'] user_id = take['student_id'] student = User.objects.get(pk=user_id) answer[index]['email'] = student.email answer[index]['realname'] = student.realname answer[index]['student_user_id'] = answer[index]['student_id'] answer[index]['student_id'] = student.personal_id return Response(generate_response(serializer.data, True))