def test_scope(self): before = db.query(Test).all() with transaction_scope(db) as session: obj = Test(value='Test') session.add(obj) after = db.query(Test).all() assert len(before) < len(after)
def delete(self, model): """Deletes a model from the database. Args: model (mixed): The model to delete """ with transaction_scope(self.session) as session: session.delete(model)
def delete_token(self, token): """Delete a token. Args: token: The token identifier to be deleted """ with transaction_scope(self.provider.session) as session: session.delete(token)
def save_all(self, *models): """Save a list of models. Args: models (list, tuple): The models to save """ with transaction_scope(self.session) as session: for model in models: session.add(model) return True
def update_user_password(self, token, password): """Update the users password. Once the user has been updated, make sure that the token has been deleted to prevent further access of that token. """ token.user.password = password with transaction_scope(self.provider.session) as session: session.add(token.user) session.delete(token)
def save(self, model): """Add the model to the session and save it to the database. Args: model (mixed): The object to save Returns: mixed: The saved model """ with transaction_scope(self.session) as session: session.add(model) return model
def populate(session, file_name): total = 0 with open(file_name) as file: json_data = json.loads(file.read()) with transaction_scope(session) as session: for item in json_data: class_ = imports.load_definition_from_string(item['class']) basic_fields = {k: v for k, v in item['fields'].items() if not isinstance(v, (list, tuple))} obj = class_(**basic_fields) total += 1 session.add(obj) return total
def delete_user(self, username, auth_provider, database): """Delete a user. Args: username: The username of the user database: The name of the database session. """ session = ensure_session_in_container(self.container, database) provider = self.container.get(auth_provider) user = provider.get_user(username) with transaction_scope(session) as session: session.delete(user) self.write('Deleted user {}'.format(username))
def delete_all(self, *models): """Delete a list of models. If deleting more than a single model of the same type, then a Repository.find(**kwargs).delete() should be called (and wrapped in a transaction_scope) instead. Args: models (list): The models to delete """ with transaction_scope(self.session) as session: for model in models: session.delete(model)
def add_permission(self, key, name, database): """Adds a new permission to the database. Args: key: The identifier for the permission name: The human readable name for the permission database: The name of the database session. """ session = ensure_session_in_container(self.container, database) from watson.auth.models import Permission permission = Permission(key=key, name=name) with transaction_scope(session) as session: session.add(permission) self.write('Permission: {} (key: {}) added!'.format(name, key))
def add_role(self, key, name, database): """Adds a new role to the database. Args: key: The identifier for the role name: The human readable name for the role database: The name of the database session. """ session = ensure_session_in_container(self.container, database) from watson.auth.models import Role role = Role(key=key, name=name) with transaction_scope(session) as session: session.add(role) self.write('Role: {} (key: {}) added!'.format(name, key))
def create_token(self, user, request): """Create a new forgotten password token. Email the user their reset password link. The template for this email can be overridden via `auth/emails/forgotten-password.html`. Args: user (watson.auth.models.User): The user who forgot their password request (watson.http.messages.Request): The HTTP request """ token = models.ForgottenPasswordToken(token=generate_forgotten_token()) user.forgotten_password_tokens.append(token) with transaction_scope(self.provider.session) as session: session.add(user) return token
def create_user(self, username, password, auth_provider, database): """Create a new user. Args: username: The username of the user password: The password of the user database: The name of the database session. """ session = ensure_session_in_container(self.container, database) provider = self.container.get(auth_provider) user_model = provider.config['model']['class'] model_class = imports.load_definition_from_string(user_model) with transaction_scope(session) as session: user = model_class() setattr(user, provider.config['model']['identifier'], username) setattr(user, 'password', password) session.add(user) self.write('Created user {}'.format(username))
def test_close_connection(self): with transaction_scope(db, should_close=True) as session: pass assert not session._new
def test_scope_exception(self): with raises(Exception): with transaction_scope(db) as session: session.add(Test())