def validate_update(data, user_id): # got_id = data['id'] found_user = session.query(User).filter(User.id == user_id).one_or_none() if found_user is None: raise ValidationError(message='invalid id') username = data['username'] username_user = session.query(User).filter( User.username == username).one_or_none() if found_user.username != username and not username_user is None: raise ValidationError(message='username is busy') user_data = {'username': data['username'], 'email': data['email']} schema = UserSchema() schema.load(user_data)
def create_user(): app.logger.info('entered action') data = request.json user_schema = UserSchema() app.logger.info('created schema') parsed_data = { 'username': data['username'], 'email': data['email'], 'password_hash': bcrypt.generate_password_hash(data['password']).decode('utf-8') } if "role" in data: parsed_data['role'] = data['role'] if not session.query(User).filter( User.username == parsed_data['username']).one_or_none() is None: return 'username busy', 400 try: user = user_schema.load(parsed_data) except ValidationError as err: return err.messages, 400 app.logger.info('created user') session.add(user) session.commit() return user_schema.dump(user), 201
class UserRegistration(Resource): def __init__(self): self.user_schema = UserSchema() self.validator = Validator({ 'username': { 'type': 'string', 'empty': False, 'minlength': 4, 'maxlength': 30 }, 'password': { 'type': 'string', 'empty': False, 'minlength': 4, }, 'email': { 'type': 'string', 'regex': '^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$', 'empty': False, 'minlength': 5 } }) super().__init__ def post(self): args = request.get_json() self.validator.validate(args) if len(self.validator.errors) > 0: return {'error': self.validator.errors}, 400 existing_user = UserModel.query.filter_by( username=args.get('username')).first() if existing_user: return {'error': 'Username already taken'}, 400 try: # Overriding value in dictionary to encrypted password args['password'] = _flask_bcrypt.generate_password_hash( password=args.get('password'), rounds=15).decode('utf-8') new_user = self.user_schema.load(args) new_user.save() except ValidationError as e: return {'error': f'{e}'}, 400 except Exception as e: return {'error': f'{e}'}, 500 return { 'message': 'New user created!', 'data': self.user_schema.dump(new_user) }, 201
def login(): """ User login """ user_data = request.json schema = UserSchema() try: user = schema.load(user_data).data except ValidationError as err: response.status = 400 return schema.get_validation_errors(err) try: db_user = User.select().where(User.username == user.username).get() except User.DoesNotExist as err: return { "code": "user_does_not_exist", "description": "User does not exist" } if fn.check_password(user.password, db_user.password) == True: return get_token(user.username)
def signup(): """ Create User """ user_data = request.json schema = UserSchema() try: user = schema.load(user_data).data except ValidationError as err: response.status = 400 return schema.get_validation_errors(err) try: query = User.insert(username=user.username, password=fn.make_password( user.password)).execute() except IntegrityError as err: return { "code": "username_already_exist", "description": "User does not exist" } result = schema.dump(user).data return HTTPResponse(result, status=201)