class RoleCheckMap(): pg = None logger = None def __init__(self, logger=None): cfg.configure(self) self.logger = logger self.pg = PG() self.pg.connect(self.pg_db, self.pg_user, self.pg_password, self.pg_host) def get_all(self): ret = {} q = """ SELECT * FROM role_check_map """ x = self.pg.read(q) for row in x: ret[row['id']] = row return ret def get_by_role_id(self, id): ret = {} q = """ SELECT * FROM role_check_map WHERE role_id = %s """ x = self.pg.read(q, [id]) for row in x: ret[row['id']] = row return ret # WRITE approach is to do the whole batch def update_by_role(self, role_id, check_ids): statements = [ "DELETE FROM role_check_map WHERE role_id = %s", ] args = [] for c in check_ids: statements.append( "INSERT INTO role_check_map (role_id, check_id) VALUES (%s, %s) RETURNING role_id" ) args.append([role_id, c]) x = self.pg.multi_write(statements, args) return x # role_id on success, False on failure
def __init__(self, logger=None): cfg.configure(self) self.logger = logger self.pg = PG() self.pg.connect(self.pg_db, self.pg_user, self.pg_password, self.pg_host)
class Roles(): pg = None logger = None def __init__(self, logger=None): cfg.configure(self) self.logger = logger self.pg = PG() self.pg.connect(self.pg_db, self.pg_user, self.pg_password, self.pg_host) def get_all_roles(self): ret = {} q = """ SELECT * FROM roles """ x = self.pg.read(q) for row in x: row['created'] = row['created'].strftime('%Y-%m-%d %H:%M:%S') row['modified'] = row['modified'].strftime('%Y-%m-%d %H:%M:%S') ret[row['id']] = row return ret def get_role(self, id): ret = {} q = """ SELECT * FROM roles WHERE id = %s """ x = self.pg.read(q, [id]) ret = x[0] ret['created'] = ret['created'].strftime('%Y-%m-%d %H:%M:%S') ret['modified'] = ret['modified'].strftime('%Y-%m-%d %H:%M:%S') return ret # @data - flask request.form # RETURNING id trick http://www.neilconway.org/docs/sequences/ def create_role(self, data): stm = """ INSERT INTO roles (name, created, modified, modified_by) VALUES (%s, now(), now(), %s) RETURNING id """ res = self.pg.write(stm, [data.get('role_name'), data.get('modified_by')]) if res is not False: return res[0][0] return res def update_role(self, data): stm = """ UPDATE roles SET name = %s modified = now() modified_by = %s WHERE id = %s RETURNING id """ args = [ data.get('role_name'), data.get('modified_by'), data.get('id') ] res = self.pg.write(stm, args) if res is not False: return res[0][0] return res def delete_role(self, data): stm = """ DELETE FROM roles WHERE id = %s """ return self.pg.write(stm, [data.get('id')])
class Users(): pg = None logger = None def __init__(self, logger=None): cfg.configure(self) self.logger = logger self.pg = PG() self.pg.connect(self.pg_db, self.pg_user, self.pg_password, self.pg_host) def get_all_users(self): ret = {} q = """ SELECT * FROM users """ x = self.pg.read(q) for row in x: row['created'] = row['created'].strftime('%Y-%m-%d %H:%M:%S') row['modified'] = row['modified'].strftime('%Y-%m-%d %H:%M:%S') ret[row['id']] = row return ret def get_user(self, id): ret = {} q = """ SELECT * FROM users WHERE id = %s """ print "ID: %d" % id x = self.pg.read(q, [id]) print "GET USER read user" print repr(x) ret = x[0] ret['created'] = ret['created'].strftime('%Y-%m-%d %H:%M:%S') ret['modified'] = ret['modified'].strftime('%Y-%m-%d %H:%M:%S') return ret def get_by_username(self, username): ret = {} q = """ SELECT * FROM users WHERE username = %s """ x = self.pg.read(q, [username]) ret = x[0] ret['created'] = ret['created'].strftime('%Y-%m-%d %H:%M:%S') ret['modified'] = ret['modified'].strftime('%Y-%m-%d %H:%M:%S') return ret # @data - flask request.form # RETURNING id trick http://www.neilconway.org/docs/sequences/ def create_user(self, data): stm = """ INSERT INTO users ( username, password, first_name, last_name, created, modified, modified_by ) VALUES ( %s, %s, %s, %s, now(), now(), %s) RETURNING id """ args = [ data.get('username'), data.get('password'), data.get('first_name'), data.get('last_name'), data.get('modified_by') ] res = self.pg.write(stm, args) if res is not False: return res[0][0] return res def update_user(self, data): stm = """ UPDATE users SET username = %s, password = %s, first_name = %s, last_name = %s, modified = now(), modified_by = %s WHERE id = %s RETURNING id """ args = [ data.get('username'), data.get('password'), data.get('first_name'), data.get('last_name'), data.get('modified_by'), data.get('id') ] res = self.pg.write(stm, args) if res is not False: return res[0][0] return res def delete_user(self, data): stm = """ DELETE FROM users WHERE id = %s """ return self.pg.write(stm, [data.get('id')])
class Checks(): pg = None logger = None def __init__(self, logger=None): cfg.configure(self) self.logger = logger self.pg = PG() self.pg.connect(self.pg_db, self.pg_user, self.pg_password, self.pg_host) def get_all_checks(self): ret = {} q = """ SELECT * FROM checks """ x = self.pg.read(q) for row in x: row['created'] = row['created'].strftime('%Y-%m-%d %H:%M:%S') row['modified'] = row['modified'].strftime('%Y-%m-%d %H:%M:%S') ret[row['id']] = row return ret def get_check(self, id): ret = {} q = """ SELECT * FROM checks WHERE id = %s """ x = self.pg.read(q, [id]) ret = x[0] ret['created'] = ret['created'].strftime('%Y-%m-%d %H:%M:%S') ret['modified'] = ret['modified'].strftime('%Y-%m-%d %H:%M:%S') return ret # @data - flask request.form # RETURNING id trick http://www.neilconway.org/docs/sequences/ def create_check(self, data): stm = """ INSERT INTO checks (name, script, created, modified, modified_by) VALUES (%s, %s, now(), now(), %s) RETURNING id """ args = [ data.get('check_name'), data.get('script'), data.get('modified_by') ] res = self.pg.write(stm, args) if res is not False: return res[0][0] return res def update_check(self, data): stm = """ UPDATE checks SET name = %s script = %s modified = now() modified_by = %s WHERE id = %s RETURNING id """ args = [ data.get('check_name'), data.get('script'), data.get('modified_by'), data.get('id') ] res = self.pg.write(stm, args) if res is not False: return res[0][0] return res def delete_check(self, data): stm = """ DELETE FROM checks WHERE id = %s """ return self.pg.write(stm, [data.get('id')])