def validate_id(self, value): try: id = ObjectId(value.data) except: raise FormValidateError(msg='班级id无效') cls = ClassModel.objects(id=id).first() if not cls: raise FormValidateError(msg='班级id无效')
def validate_classId(self, value): try: classId = ObjectId(value.data) except: raise FormValidateError(msg='无效的班级') cls = ClassModel.objects(id=classId).first() if not cls: raise FormValidateError(msg='无效的班级')
def validate_id(self, value): try: id = ObjectId(value.data) except: raise FormValidateError(msg='无效的用户id') user = UserModel.objects(id=id).first() if not user: raise FormValidateError(msg='无效的用户id')
def validate_password(self, value): account = self.account.data password = value.data admin = AdminModel.objects(account=account).first() if not admin: raise FormValidateError(msg='用户名不存在') if check_password_hash(admin['password'], password) == False: raise FormValidateError(msg='密码不正确') self.userInfo = { "id": str(admin['id']), 'name': admin['name'], "is_admin": True }
def get_user_list(): data = request.args or {} form = UserGetListForm(data=data) form.validate_for_api() page = form.page.data size = form.size.data userName = data['userName'] or '' schoolId = data['schoolId'] or '' classroom = data['classroom'] or '' if classroom != '': try: classroom = ObjectId(classroom) except: return FormValidateError(msg='班级id有误') cls = ClassModel.objects(id=classroom).first() if cls is None: return FormValidateError(msg='班级id有误') count = UserModel.objects(name__contains=userName, schoolId__contains=schoolId, classId=cls).count() users = UserModel.objects( name__contains=userName, schoolId__contains=schoolId, classId=cls).order_by('classId', 'schoolId').skip( (int(page) - 1) * int(size)).limit(int(size)) else: count = UserModel.objects(name__contains=userName, schoolId__contains=schoolId).count() users = UserModel.objects( name__contains=userName, schoolId__contains=schoolId).order_by('classId', 'schoolId').skip( (int(page) - 1) * int(size)).limit(int(size)) list = [] for user in users: list.append({ 'id': str(user['id']), 'name': user['name'], 'classId': str(user['classId']['id']), 'classname': str(user['classId']['name']), 'schoolId': user['schoolId'], 'avatar': user['avatar'] }) data = { 'pagination': { 'count': count, 'size': int(size), 'page': int(page) }, 'list': list } return Success(msg='获取成功!', data=data)
def validate_wxId(self, value): wxId = value.data + current_app.config['SECRET_KEY'] m = hashlib.md5() m.update(wxId.encode("utf8")) wxId = m.hexdigest() user = UserModel.objects(wxId=wxId).first() if user: raise FormValidateError(msg='此微信id已经被注册了!')
def get_user(id): user = UserModel.objects(id=ObjectId(id)).first() if not user: raise FormValidateError(msg='用户不存在') dic = { 'id': str(user['id']), 'name': user['name'], 'avatar': user['avatar'], 'schoolId': user['schoolId'], 'classroom': str(user['classId']['name']) } return dic
def validate_wxId(self, value): wxId = value.data + current_app.config['SECRET_KEY'] m = hashlib.md5() m.update(wxId.encode("utf8")) wxId = m.hexdigest() user = UserModel.objects(wxId = wxId).first() if not user: raise FormValidateError(msg='还没有注册,先去注册吧!') self.userInfo = { "id": str(user['id']), 'name': user['name'], 'is_admin': False }
def validate_name(self, value): name = value.data cls = ClassModel.objects(name=name).first() if cls: raise FormValidateError(msg='班级名重复')
def validate_page(self, value): num = str(value.data) if num.isdigit() == False or int(num) <= 0: raise FormValidateError(msg='page应该是一个正整数') self.page.data = int(value.data)
def validate_schoolId(self, value): super(UserPutForm, self).validate_schoolId(value) user = UserModel.objects(schoolId=value.data).first() if user and str(user['id']) != self.id.data: raise FormValidateError(msg='学号已经被注册,请联系管理员!')
def validate_schoolId(self, value): super(UserRegisterForm, self).validate_schoolId(value) user = UserModel.objects(schoolId=value.data).first() if user: raise FormValidateError(msg='学号已经被注册了,请联系管理员!')
def validate_schoolId(self, value): schoolId = value.data if len(schoolId) == 12 and re.match(r'\d{12}', schoolId): pass else: raise FormValidateError(msg='学号必须是12位的纯数字')
def validate_expiration(self, value): expiration = str(value.data) if expiration.isdigit() == False or int(expiration) <= 0: raise FormValidateError(msg='签到时间应该是一个正整数')