def __init__(self, id_card): if not pd_card(id_card): raise TypeError(u'请输入标准的第二代身份证号码') self.id_card = id_card self.birth_year = int(self.id_card[6:10]) self.birth_month = int(self.id_card[10:12]) self.birth_day = int(self.id_card[12:14]) self.sex = -1 if int(self.id_card[-2:-1]) % 2 == 0 else 1
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 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 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 check_student_insert_info(request): school = request.data.get('school') clazz = request.data.get('clazz') 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_success_200(code=STATUS_PARAMETER_ERROR, message="card 不能为空") if not name: return response_success_200(code=STATUS_PARAMETER_ERROR, message="name 不能为空") # 学校关联 if not School.objects.filter(id=school): return response_success_200(code=STATUS_PARAMETER_ERROR, message="学校不存在") # 班级关联 if not Class.objects.filter(id=clazz): return response_success_200(code=STATUS_PARAMETER_ERROR, message="班级不存在") # 用户检查是否存在 if UserDetails.objects.filter(card=card): return response_success_200(code=STATUS_PARAMETER_ERROR, message="身份证已经注册存在") if User.objects.filter(phone_number=phone_number): return response_success_200(code=STATUS_PARAMETER_ERROR, message="手机号码已经注册存在") # 验证格式 if not pd_card(card): return response_success_200(code=STATUS_PARAMETER_ERROR, message="身份证格式错误") if qq and not pd_qq(qq): return response_success_200(code=STATUS_PARAMETER_ERROR, message="qq格式不正确") if email and not pd_email(email): return response_success_200(code=STATUS_PARAMETER_ERROR, message="email格式不正确") if phone_number and not pd_phone_number(phone_number): return response_success_200(code=STATUS_PARAMETER_ERROR, message="手机号格式错误")
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 check_parent_insert_info(request): 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_success_200(message="card 不能为空") if not name: return response_success_200(message="name 不能为空") if not phone_number: return response_success_200(message="手机号 不能为空") # 用户检查是否存在 if UserDetails.objects.filter(card=card): return response_success_200(staus=STATUS_PARAMETER_ERROR, message="身份证已经注册存在") if User.objects.filter(phone_number=phone_number): return response_success_200(staus=STATUS_PARAMETER_ERROR, message="手机号码已经注册存在") # 验证格式 if not pd_card(card): return response_success_200(staus=STATUS_PARAMETER_ERROR, message="身份证格式错误") if qq and not pd_qq(qq): return response_success_200(staus=STATUS_PARAMETER_ERROR, message="qq格式不正确") if email and not pd_email(email): return response_success_200(staus=STATUS_PARAMETER_ERROR, message="email格式不正确") if not pd_phone_number(phone_number): return response_success_200(staus=STATUS_PARAMETER_ERROR, message="手机号格式错误")
def partial_update_adm(self, request, *args, **kwargs): check_token = pd_adm_token(request) if check_token: return check_token user_update = Student.objects.get(pk=kwargs['pk']).user phone_number = request.data.get("phone_number") if phone_number: if phone_number != user_update.phone_number: if not pd_phone_number(phone_number): return response_success_200(code=STATUS_PHONE_NUMBER_ERROR, message="手机号输入有误") if User.objects.exclude(pk=user_update.id).filter( phone_number=phone_number): return response_success_200( code=STATUS_PHONE_NUMBER_DUPLICATE, 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_success_200(code=STATUS_PARAMETER_ERROR, message=check_time) user_detail_update.birthday = birthday if card and card != user_detail_update.card: if not pd_card(card): return response_success_200(code=STATUS_PARAMETER_ERROR, message="身份证输入有误") if UserDetails.objects.exclude(pk=user_detail_update.id).filter( card=card): return response_success_200(code=STATUS_PARAMETER_ERROR, message="身份证已存在") user_detail_update.card = card if qq and qq != user_detail_update.qq: print(qq) print(user_detail_update.qq) if not pd_qq(qq): return response_success_200(code=STATUS_PARAMETER_ERROR, message="qq输入有误") if UserDetails.objects.exclude(pk=user_detail_update.id).filter( qq=qq): return response_success_200(code=STATUS_PARAMETER_ERROR, message="qq已存在") user_detail_update.qq = qq if email and email != user_detail_update.email: if not pd_email(email): return response_success_200(code=STATUS_PARAMETER_ERROR, message="email输入有误") if UserDetails.objects.exclude(pk=user_detail_update.id).filter( email=email): return response_success_200(code=STATUS_PARAMETER_ERROR, message="email已存在") user_detail_update.email = email # 保存修改 user_update.save() user_detail_update.save() resp = super().partial_update(request, *args, **kwargs) return response_success_200(data=resp.data)