def adm_update_user_details(user_update: User, request): phone_number = request.data.get("phone_number") if phone_number and phone_number != user_update.phone_number: if not pd_phone_number(phone_number): return response_error_400(message="手机号输入有误") if User.objects.exclude(pk=user_update.id).filter( phone_number=phone_number): return response_error_400(message="手机号已存在") user_update.phone_number = phone_number # 获得传过来的参数 user_details = request.data.get('user_details') print(user_details) # 获得需要修改的userDetails user_detail_update = user_update.user_details sex = user_details.get('sex') name = user_details.get('name') birthday = user_details.get('birthday') card = user_details.get('card') qq = user_details.get('qq') email = user_details.get('email') if sex and sex is not user_detail_update.sex: user_detail_update.sex = sex if name and name is not user_detail_update.name: user_detail_update.name = name if birthday and birthday != user_detail_update.birthday: check_time = check_time_stamp(int(birthday)) print(check_time) if check_time: return response_error_400(message=check_time) user_detail_update.birthday = birthday if card and card != user_detail_update.card: if not pd_card(card): return response_error_400(message="身份证输入有误") if UserDetails.objects.exclude(pk=user_detail_update.id).filter( card=card): return response_error_400(message="身份证已存在") user_detail_update.card = card if qq and qq != user_detail_update.qq: if not pd_qq(qq): return response_error_400(message="qq输入有误") if UserDetails.objects.exclude(pk=user_detail_update.id).filter(qq=qq): return response_error_400(message="qq已存在") user_detail_update.qq = qq if email and email != user_detail_update.email: if not pd_email(email): return response_error_400(message="email输入有误") if UserDetails.objects.exclude(pk=user_detail_update.id).filter( email=email): return response_error_400(message="email已存在") user_detail_update.email = email # 保存修改 user_update.save() user_detail_update.save()
def create(self, request, *args, **kwargs): check_token = pd_token(request) if check_token: return check_token if lookup_token(request) is not 1: return response_error_400(status=STATUS_TOKEN_NO_AUTHORITY, message="不是学生提交什么作业?") print(request.data) resp = super().create(request) return response_success_200(data=resp.data)
def batch_import_test(file): excel_data = pd.read_excel(file, header=0, dtype='str') test = [] i = 1 card_list = [] phone_list = [] for dt in excel_data.iterrows(): i = i + 1 message = "" card = dt[1]['身份证'] phone_number = dt[1]['手机号码'] qq = dt[1]['QQ(选填)'] email = dt[1]['邮箱(选填)'] if not dt[1]['家长姓名'] or not card or not phone_number: message += "有空字段" # 判断身份证的格式是否存在 if not pd_card(card): message += ",身份证格式错误" elif card in card_list: message += f",身份证和{card_list.index(card) + 1}重复" elif User.objects.filter(user_name=card): message += ",身份证已经注册存在" if not pd_phone_number(phone_number): message += ",手机号格式错误" elif phone_number in phone_list: message += f",手机号和{phone_list.index(phone_number) + 1}重复" elif User.objects.filter(phone_number=phone_number): message += ",手机号码已经注册存在" # 验证qq号 if not qq and not pd_qq(qq): message += ",qq号格式错误" # 验证邮箱 if not email and not pd_email(email): message += ",邮箱格式错误" if message: test.append({"index": i, "message": message}) card_list.append(card) if phone_number: phone_list.append(phone_number) if len(test) > 0: return response_error_400(code=STATUS_PARAMETER_ERROR, message="有错误信息", err_data=test, length=len(test)) return None
def destroy(self, request, *args, **kwargs): check_token = pd_token(request) if check_token: return check_token if lookup_token(request) not in [-2, -1, 0, 3]: return response_error_400(status=STATUS_TOKEN_NO_AUTHORITY, message="权限不够") instance = self.get_object() self.perform_destroy(instance) return Response(status=status.HTTP_204_NO_CONTENT)
def examine_update(self, request, *args, **kwargs): check_token = pd_token(request) if check_token: return check_token if lookup_token(request) not in [0, 3]: return response_error_400(status=STATUS_TOKEN_NO_AUTHORITY, message="权限不够") print(request.data) resp = super().partial_update(request, *args, **kwargs) return response_success_200(data=resp.data)
def batch_import_test(file): excel_data = pd.read_excel(file, header=0, dtype='str') test = [] i = 1 card_list = [] phone_list = [] for dt in excel_data.iterrows(): i = i + 1 message = "" # 添加用户信息 card = dt[1]['身份证'] phone_number = dt[1]['手机号码(选填)'] school = dt[1]['学校名称'] class_name = dt[1]['班级'] if not dt[1]['学生姓名'] or not card or not class_name or not school: message += "有空字段" # 判断身份证的格式是否存在 if not pd_card(card): message += ",身份证格式错误" elif card in card_list: message += f",身份证和{card_list.index(card) + 1}重复" elif UserDetails.objects.filter(card=card): message += ",身份证已经注册存在" if phone_number: if not pd_phone_number(phone_number): message += ",手机号格式错误" elif phone_number in phone_list: message += f",手机号和{phone_list.index(phone_number) + 1}重复" elif User.objects.filter(phone_number=phone_number): message += ",手机号码已经注册存在" if not School.objects.filter(school_name=school): message += ",学校不存在" elif not Class.objects.filter( class_name=class_name, school_id=School.objects.get(school_name=school).id): message += ",学校中班级名不存在" if message: test.append({"index": i, "message": message}) card_list.append(card) if phone_number: phone_list.append(phone_number) if len(test) > 0: return response_error_400(code=STATUS_PARAMETER_ERROR, message="有错误信息", err_data=test, length=len(test)) return None
def del_user_and_user_details(role, pk): # 老师或辅导员 if role == 0 or role == 3: try: teacher = Teacher.objects.get(pk=pk) except Teacher.DoesNotExist: return response_error_400(status=STATUS_NOT_FOUND_ERROR, message=f"id({pk})未找到") user_id = teacher.user_id # 学生 elif role == 1: try: student = Student.objects.get(pk=pk) except Student.DoesNotExist: return response_error_400(status=STATUS_NOT_FOUND_ERROR, message=f"id({pk})未找到") user_id = student.user_id # 家长 elif role == 2: try: parent = Parent.objects.get(pk=pk) except Parent.DoesNotExist: return response_error_400(status=STATUS_NOT_FOUND_ERROR, message=f"id({pk})未找到") user_id = parent.user_id else: return response_error_400(status=STATUS_NOT_FOUND_ERROR, message=f"role({role})范围应该在(0`3)") if not User.objects.filter(pk=user_id): return response_error_400(status=STATUS_NOT_FOUND_ERROR, message=f"role={role}中对应的用户id{user_id}未找到") user = User.objects.get(pk=user_id) # 删除用户详情 user.user_details.delete() # 删除用户 user.delete() return None
def check_teacher_insert_info(request): school = request.data.get('school') title = request.data.get('title') card = request.data.get('card') phone_number = request.data.get('phone_number') name = request.data.get('name') qq = request.data.get('qq') email = request.data.get('email') # 不能为空的数据 if not card: return response_error_400(message="card 不能为空") if not name: return response_error_400(message="name 不能为空") if not phone_number: return response_error_400(message="手机号 不能为空") # 学校关联 if not School.objects.filter(id=school): return response_error_400(staus=STATUS_PARAMETER_ERROR, message="学校不存在") # 用户检查是否存在 if UserDetails.objects.filter(card=card): return response_error_400(staus=STATUS_PARAMETER_ERROR, message="身份证已经注册存在") if User.objects.filter(phone_number=phone_number): return response_error_400(staus=STATUS_PARAMETER_ERROR, message="手机号码已经注册存在") # 验证格式 if not pd_card(card): return response_error_400(message="身份证格式错误") if qq and not pd_qq(qq): return response_error_400(message="qq格式不正确") if email and not pd_email(email): return response_error_400(message="email格式不正确") if not pd_phone_number(phone_number): return response_error_400(message="手机号格式错误")
def create(self, request, *args, **kwargs): check_token = pd_token(request) if check_token: return check_token if lookup_token(request) not in [0, 3]: return response_error_400(status=STATUS_TOKEN_NO_AUTHORITY, message="权限不够") # print(request.data) resp = super().create(request) clazz = resp.data['clazz'] # print(clazz) students = Student.objects.filter(clazz=clazz) # print(students) for i in students: UserNoticeSend.send_notice(i.user, 0, resp.data['id']) return response_success_200(data=resp.data)
def batch_add_parent_test(file): excel_data = pd.read_excel(file, header=0, dtype='str') test = [] i = 1 for dt in excel_data.iterrows(): i = i + 1 message = "" # 添加用户信息 student_id = dt[1]['学生学号'] student_name = dt[1]['学生姓名'] parent_id = dt[1]['家长ID'] parent_name = dt[1]['家长姓名'] if not student_id or not student_name or not parent_id or not parent_name: message += "有空字段" # 判断学生ID是否存在 if not Student.objects.get(id=int(student_id)): message += ",没有该学生ID错误" else: student = Student.objects.get(id=int(student_id)) if student.user.user_details.name != student_name: message += ",该学生ID与姓名不匹配" # 判断家长ID是否存在 if not Parent.objects.filter(id=parent_id): message += ",没有该家长ID错误" parent = None else: parent = Parent.objects.get(id=int(parent_id)) if parent.user.user_details.name != parent_name: message += ",该家长ID与姓名不匹配" if message: test.append({"index": i, "message": message}) if len(test) > 0: return response_error_400(code=STATUS_PARAMETER_ERROR, message="有错误信息", err_data=test, length=len(test)) return None