def selectone(db, cmd): try: if db.execute(cmd) > 0: return OK(db.fetchone()) else: return OK({}) except Exception as e: return FAIL(e)
def select(db, cmd): try: if db.execute(cmd) > 0: return OK(db.fetchall()) else: return OK([]) except Exception as e: return FAIL(e)
def set_nas(self, vp=None): vp = vp if vp else self.p ok, res = self.required(["nasname", "shortname", "secret"]) if not ok: return self.fail_required(res) nasname = vp.get("nasname") shortname = vp.get("shortname") secret = vp.get("secret") ok, res = self.get_nas({"nasname": nasname}) if not ok: return FAIL(res) if len(res) > 0: return fail_duplicated("nasname")
def del_group(self, vp=None): vp = vp if vp else self.p ok, res = self.required(["group_id"]) if not ok: return self.fail_required(res) group_id = vp.get("group_id") ok, res = self.get_groups() if not ok: return FAIL(res) if not len(res): return self.fail_unknown("group_id", group_id) sql = "DELETE FROM groups WHERE group_id = '{group_id}'".format( group_id=group_id) return DB.delete(sql)
def del_user(self, vp=None): vp = vp if vp else self.p ok, res = self.required(["user_id"]) if not ok: return self.fail_required(res) user_id = vp.get("user_id") ok, res = self.get_users() if not ok: return FAIL(res) if not len(res): return self.fail_unknown("user_id", user_id) sql = "DELETE FROM users WHERE user_id = '{user_id}'".format( user_id=user_id) return DB.delete(sql)
def mod_user(self, vp=None): vp = vp if vp else self.p ok, res = self.required(["user_id", "username", "passwd"]) if not ok: return self.fail_required(res) user_id = vp.get("user_id") ok, res = self.get_users() if not ok: return FAIL(res) if not len(res): return self.fail_unknown("user_id", user_id) user_id = vp.get("user_id") username = vp.get("username") passwd = vp.get("passwd") sql = """ UPDATE users SET username='******', passwd='{passwd}' WHERE user_id='{user_id}' """.format(user_id=user_id, username=username, passwd=passwd) return DB.update(sql)
def mod_group(self, vp=None): vp = vp if vp else self.p ok, res = self.required(["group_id", "group_name", "parent_group_id"]) if not ok: return self.fail_required(res) group_id = vp.get("group_id") ok, res = self.get_groups() if not ok: return FAIL(res) if not len(res): return self.fail_unknown("group_id", group_id) group_id = vp.get("group_id") group_name = vp.get("group_name") parent_group_id = vp.get("parent_group_id") sql = """ UPDATE groups SET group_name='{group_name}', parent_group_id='{parent_group_id}' WHERE group_id='{group_id}' """.format(group_id=group_id, group_name=group_name, parent_group_id=parent_group_id) return DB.update(sql)
def update(db, cmd): try: db.execute(cmd) return OK() except Exception as e: return FAIL(e)
def callproc(db, procname, args=()): try: return OK(db.callproc(procname, args)) except Exception as e: return FAIL(e)
def fail_message(self, msg): return FAIL("FAIL: {}".format(msg))
def fail_unknown(self, name, value): return FAIL("unknown {}: {}".format(name, value))
def fail_required(self, cause): return FAIL("required parameter: {}".format(cause))
def fail_duplicated(self, name): return FAIL("duplicate: {}".format(name))