class ClassDeleteAllView(ModelViewSet): queryset = Class.objects.all() @swagger_auto_schema( operation_summary="根据id列表批量删除班级信息及用户信息", manual_parameters=[ openapi.Parameter('TOKEN', openapi.IN_HEADER, type=openapi.TYPE_STRING, description='管理员TOKEN'), ], request_body=request_body( properties={ 'id_list': array_schema('班级ID列表', it=integer_schema()) }), ) def destroy_all(self, request, *args, **kwargs): check_token = pd_adm_token(request) if check_token: return check_token # print(request.data) list = request.data.get("id_list") message = "" print(list) for i in list: if not Class.objects.filter(pk=i): message += "班级i+未找到" else: Class.objects.filter(pk=i).delete() return response_success_200(message="批量删除删除结束," + message)
class TeacherDeleteAllView(ModelViewSet): queryset = Teacher.objects.all() @swagger_auto_schema( operation_summary="根据id列表批量删除老师信息及用户信息", manual_parameters=[ openapi.Parameter('TOKEN', openapi.IN_HEADER, type=openapi.TYPE_STRING, description='管理员TOKEN'), ], request_body=request_body( properties={ 'id_list': array_schema('老师ID列表', it=integer_schema()) }), ) def destroy_all2(self, request, *args, **kwargs): check_token = pd_adm_token(request) if check_token: return check_token # print(request.data) list = request.data.get("id_list") print(list) # # 先删除用户 for i in list: check_del = del_user_and_user_details(0, int(i)) if check_del: return check_del # 删除老师 # super().destroy(request, *args, **kwargs) return response_success_200(message="成功")
class StudentDeleteAllView(ModelViewSet): queryset = Student.objects.all() @swagger_auto_schema( operation_summary="根据id列表批量删除学生信息及用户信息", manual_parameters=[ openapi.Parameter('TOKEN', openapi.IN_HEADER, type=openapi.TYPE_STRING, description='管理员TOKEN'), ], request_body=request_body( properties={ 'id_list': array_schema('学生ID列表', it=integer_schema()) }), ) def destroy_all2(self, request, *args, **kwargs): check_token = pd_token(request) if check_token: return check_token role = request.auth if role not in [-2, -1, 3]: return response_success_200(code=STATUS_TOKEN_NO_AUTHORITY, message="没有权限") # print(request.data) list = request.data.get("id_list") print(list) # # 先删除用户 for i in list: check_del = del_user_and_user_details(1, int(i)) if check_del: return check_del return response_success_200(message="成功")
class WeekInsertView(mixins.CreateModelMixin, GenericViewSet): queryset = Week.objects.all() serializer_class = WeekInfoSerializersAll @swagger_auto_schema( operation_summary="添加数据 ", request_body=request_body(properties={ 'index': integer_schema('周的下标'), 'title': string_schema('描述'), }), manual_parameters=[ openapi.Parameter('TOKEN', openapi.IN_HEADER, type=openapi.TYPE_STRING, description='超级管理员的token'), ]) def create(self, request, *args, **kwargs): check_token = pd_super_adm_token(request) if check_token: return check_token # 添加用户信息 index = request.data.get('index') # 用户index否存在 if self.queryset.filter(index=index): return response_error_400(message="index 已存在") resp = super().create(request) return response_success_200(data=resp.data)
class TimetableInsertView(mixins.CreateModelMixin, GenericViewSet): queryset = Timetable.objects.all() serializer_class = TimetableAllSerializersInsert @swagger_auto_schema(operation_description="添加课程到课表中", operation_summary="timetable添加课程", request_body=request_body( properties={ 'timetable_id': integer_schema('课表id'), 'course_id': integer_schema('课程id'), })) def add_course(self, request): timetable_id = request.data.get('timetable_id') course_id = request.data.get('course_id') result = views.add_course(timetable_id, course_id) return result if result else response_success_200(message="添加成功") @swagger_auto_schema( request_body=request_body(properties={ 'clazz': integer_schema('班级id'), 'week': integer_schema('星期'), })) def create(self, request, *args, **kwargs): clazz = request.data.get('clazz') week = request.data.get('week') if not Class.objects.filter(id=clazz): message = "班级ID信息不存在" return response_success_200(status=STATUS_PARAMETER_ERROR, message=message) if Timetable.objects.filter(clazz_id=clazz).filter(week=week): message = "课程已经添加" return response_success_200(status=STATUS_PARAMETER_ERROR, message=message) # 把课程添加到课程表中 resp = super().create(request) return response_success_200(data=resp.data)
class CourseInsertView(mixins.CreateModelMixin, GenericViewSet): queryset = Course.objects.all() serializer_class = CourseALlSerializers @swagger_auto_schema(request_body=request_body( properties={ 'timetable': integer_schema('课表id'), 'teacher': integer_schema('老师ID'), 'course_name': string_schema('课程名'), 'index': integer_schema('第几节课'), }), manual_parameters=[ openapi.Parameter('TOKEN', openapi.IN_HEADER, type=openapi.TYPE_STRING, description='管理员TOKEN'), ]) def create(self, request, *args, **kwargs): teacher_info = request.data.get('teacher') timetable_id = request.data.get('timetable') # print(Class.objects.filter(grade_name=grade_name)) # if Class.objects.filter(grade_name=grade_name): # message = "班级已经存在" # return response_error_400(status=STATUS_CODE_ERROR, message=message) if not Teacher.objects.filter(id=teacher_info): message = "老师ID信息不存在" return response_success_200(status=STATUS_PARAMETER_ERROR, message=message) if not Timetable.objects.filter(id=timetable_id): message = "课程表不存在" return response_success_200(status=STATUS_PARAMETER_ERROR, message=message) # 把课程添加到课程表中 resp = super().create(request) # Timetable.objects.get(id=timetable_id).course_info.add(resp.data['id']) print(resp.data) return response_success_200(data=resp.data)
class TimetableSelectView(mixins.ListModelMixin, mixins.RetrieveModelMixin, GenericViewSet): queryset = Timetable.objects.all() serializer_class = TimetableDepth2SerializersInsert @swagger_auto_schema( operation_summary="查询班级的课表", operation_description="传入班级id", request_body=request_body(properties={ 'class_id': integer_schema('班级id'), })) def select_class(self, request): class_id = request.data.get('class_id') return select_class(self, class_id) @swagger_auto_schema(operation_summary="token查询课表", operation_description="传入token", request_body=no_body, manual_parameters=[ openapi.Parameter('TOKEN', openapi.IN_HEADER, type=openapi.TYPE_STRING, description='TOKEN') ]) def select_student_timetable_by_token(self, request): # 判断token check_token = pd_token(request) if check_token: return check_token if request.auth != 1: return response_success_200(code=STATUS_PARAMETER_ERROR, message="需要传入学生token") try: clazz = Student.objects.get(user_id=request.user).clazz if not clazz: return response_success_200(code=STATUS_PARAMETER_ERROR, message="请先加入班级") return select_class(self, clazz.id) except Student.DoesNotExist: return response_success_200(code=STATUS_PARAMETER_ERROR, message="未找到该学生")
class RegularAddRecordInsertView(mixins.CreateModelMixin, GenericViewSet): queryset = RegularAddRecord.objects.all() serializer_class = RegularAddRecordInfoSerializersInsert @swagger_auto_schema( operation_summary="添加数据 ", operation_description=f"当前的时间戳是 {int(time.time())}", request_body=request_body( properties={ 'describe': string_schema(description="我是描述", default="默认值", title="标题"), 'regular': integer_schema('regular的id', default=1), 'reminder_time': integer_schema('提醒时间'), 'start_time': integer_schema('每天开始的时间'), 'end_time': integer_schema('每天结束的时间'), 'start_date': integer_schema('开始日期时间'), 'end_date': integer_schema('结束日期时间'), 'week': array_schema('周'), }), manual_parameters=[ openapi.Parameter('TOKEN', openapi.IN_HEADER, type=openapi.TYPE_STRING, description='用户的token'), ]) def create(self, request, *args, **kwargs): # 先检测用户输入的时间的规范性 check_time = check_insert_time(request) if check_time: return check_time check_token = pd_token(request) if check_token: return check_token resp = super().create(request) # 保存用户的id regular = RegularAddRecord.objects.get(id=resp.data['id']) regular.user_id = request.user regular.save() return response_success_200(data=resp.data, message="添加成功")
class TeacherInsertView(mixins.CreateModelMixin, GenericViewSet): queryset = Teacher.objects.all() serializer_class = TeacherInfoSerializersInsert @swagger_auto_schema( operation_summary="教师信息单独导入", operation_description="说明:身份证和电话号码必须未注册过,学校必须存在", request_body=request_body(properties={ 'name': string_schema('老师姓名'), 'card': string_schema('身份证'), 'title': string_schema('职称'), 'phone_number': string_schema('电话号码'), 'school': integer_schema('学校id'), 'qq': string_schema('QQ'), 'email': string_schema('邮件'), }), manual_parameters=[ openapi.Parameter('TOKEN', openapi.IN_HEADER, type=openapi.TYPE_STRING, description='管理员TOKEN'), ] ) def create(self, request, *args, **kwargs): check_token = pd_adm_token(request) if check_token: return check_token # 检测teacher插入的合法性 check_info = check_teacher_insert_info(request) if check_info: return check_info # 创建用户详情和用户 create_user_details_and_user(request, 0) resp = super().create(request) return response_success_200(data=resp.data)
class RegularAddRecordOtherView(ModelViewSet): queryset = RegularAddRecord.objects.all() serializer_class = RegularAddRecordInfoSerializersInsert @swagger_auto_schema( operation_summary="根据id删除RegularAddRecord的信息", required=[], manual_parameters=[ openapi.Parameter('TOKEN', openapi.IN_HEADER, type=openapi.TYPE_STRING, description='用户TOKEN') ], ) def destroy(self, request, *args, **kwargs): # 需要修改的检测 pk = kwargs['pk'] check_token = pd_token(request) if check_token: return check_token # 检查权限 check = check_authority(self, request, pk) if check: return check # 删除 super().destroy(request, *args, **kwargs) return response_success_200(message="成功") @swagger_auto_schema( operation_summary="修改!!", operation_description=f"当前的时间戳是 {int(time.time())}", request_body=request_body( properties={ 'describe': string_schema(description="我是描述", default="默认值", title="标题"), 'regular': integer_schema('描述', default=1), 'reminder_time': integer_schema('提醒时间'), 'start_time': integer_schema('每天开始的时间'), 'end_time': integer_schema('每天结束的时间'), 'start_date': integer_schema('开始日期时间'), 'end_date': integer_schema('结束日期时间'), 'week': array_schema('周'), }), manual_parameters=[ openapi.Parameter('TOKEN', openapi.IN_HEADER, type=openapi.TYPE_STRING, description='用户的TOKEN') ]) def partial_update(self, request, *args, **kwargs): # 需要修改的检测 pk = kwargs['pk'] check_token = pd_token(request) if check_token: return check_token # 检查权限 check = check_authority(self, request, pk) if check: return check # 判断时间 check_time = check_update_time(request, pk) if check_time: return check_time resp = super().partial_update(request, *args, **kwargs) return response_success_200(data=resp.data)
class ClassInsertView(mixins.CreateModelMixin, GenericViewSet): """ create: 添加一条数据 无描述 """ queryset = Class.objects.all() serializer_class = ClassInfoSerializers @swagger_auto_schema(request_body=request_body( properties={ 'teacher': integer_schema('辅导员ID'), 'school': integer_schema('学校ID'), 'class_name': string_schema('班级名') }), manual_parameters=[ openapi.Parameter('TOKEN', openapi.IN_HEADER, type=openapi.TYPE_STRING, description='管理员TOKEN'), ]) def create(self, request, *args, **kwargs): check_token = pd_adm_token(request) if check_token: return check_token teacher_id = request.data.get('teacher') class_name = request.data.get('class_name') school_id = request.data.get('school') print(class_name) if Class.objects.filter(school_id=school_id).filter( class_name=class_name): message = "班级已经存在" return response_success_200(code=STATUS_PARAMETER_ERROR, message=message) if not Teacher.objects.filter(id=teacher_id): message = "老师ID信息不存在" return response_success_200(code=STATUS_PARAMETER_ERROR, message=message) if not School.objects.filter(id=school_id): message = "学校ID信息不存在" return response_success_200(code=STATUS_CODE_ERROR, message=message) teacher = Teacher.objects.get(id=teacher_id) headmaster_id = teacher.user_id try: clazz = self.queryset.create(headmaster_id=headmaster_id, school_id=school_id, class_name=class_name) # 一对一重复报错 except IntegrityError: return response_success_200(message="该老师已有班级") # 修改老师的信息为管理员 user = User.objects.get(id=teacher.user_id) teacher.identity = "辅导员" user.role = 3 user.save() teacher.save() return response_success_200(data=clazz.to_json())