def delete_script(self, name=None, uuid=None): session = db.get_session() q = utils.model_query(models.PyScriptsScript, session) if name: q = q.filter(models.PyScriptsScript.name == name) elif uuid: q = q.filter(models.PyScriptsScript.script_id == uuid) else: raise ValueError('You must specify either name or uuid.') r = q.delete() if not r: raise api.NoSuchScript(uuid=uuid)
def get_script(self, name=None, uuid=None): session = db.get_session() try: q = session.query(models.PyScriptsScript) if name: q = q.filter(models.PyScriptsScript.name == name) elif uuid: q = q.filter(models.PyScriptsScript.script_id == uuid) else: raise ValueError('You must specify either name or uuid.') res = q.one() return res except sqlalchemy.orm.exc.NoResultFound: raise api.NoSuchScript(name=name, uuid=uuid)
def update_script(self, uuid, **kwargs): session = db.get_session() try: with session.begin(): q = session.query(models.PyScriptsScript) q = q.filter(models.PyScriptsScript.script_id == uuid) script_db = q.with_lockmode('update').one() if kwargs: excluded_cols = ['script_id'] for col in excluded_cols: if col in kwargs: kwargs.pop(col) for attribute, value in six.iteritems(kwargs): if hasattr(script_db, attribute): setattr(script_db, attribute, value) else: raise ValueError( 'No such attribute: {}'.format(attribute)) else: raise ValueError('No attribute to update.') return script_db except sqlalchemy.orm.exc.NoResultFound: raise api.NoSuchScript(uuid=uuid)