def add_user(self, u_name, u_role, u_password, u_phone, c_id): """ add_user :return: 'Success', '' or 'Fail', 'error_msg' """ log("add_user: %s %s %s %s %s" % (u_name, u_role, u_password, u_phone, c_id)) conn = sqlite3.connect("demo.db") param = ( u_name, u_role, u_password, u_phone, c_id, ) try: conn.execute( 'INSERT INTO user(u_name, u_role, u_password, u_phone, c_id) ' 'VALUES (?, ?, ?, ?, ?);', param) except Exception: conn.close() return "Fail", traceback.print_exc() conn.commit() conn.close() return "Success", ""
def add_community_admin(self, c_id, u_id): """ TODO 给这个函数找个地方用 给当前未分配管理员的社区分配管理员,通常这一步伴随add community一起做 :return: 'Success', '' or 'Fail', 'error_msg' """ log("add_community_admin: %s %s" % (c_id, u_id)) conn = sqlite3.connect("demo.db") param = (u_id, ) response = conn.execute('SELECT u_role FROM user WHERE user.u_id = ?', param) response = response.fetchall() if (len(response) == 0) or response[0][0] != 'admin': conn.close() return "Fail", "%s is not root or admin" % u_id try: param = ( u_id, c_id, ) conn.execute('UPDATE community SET u_id = ? WHERE c_id = ?;', param) except Exception: conn.close() return "Fail", traceback.print_exc() conn.commit() conn.close() return "Success", ""
def log_out(): """ 登出,立刻跳转到首页(此时会被重定向到登录页面) :return: """ log("log_out") session['logged_in'] = False session['u_id'] = -1 session['u_name'] = 'guest' session['u_role'] = 'guest' session['c_id'] = -1 return redirect('index')
def update_user(self, u_id, u_name=None, u_role=None, u_password=None, u_phone=None, c_id=None): """ update_user :return: 'Success', '' or 'Fail', 'error_msg' """ log("update_user: %s %s %s %s %s %s" % (u_id, u_name, u_role, u_password, u_phone, c_id)) conn = sqlite3.connect("demo.db") param = (u_id, ) try: if c_id is not None and u_role == 'admin': param = (c_id, ) response = conn.execute( 'SELECT u_id FROM community WHERE c_id = ?', param) response = response.fetchall()[0][0] if response is not None: return "Fail", \ "Community %s already has an administrator." % c_id response = conn.execute('SELECT * FROM user WHERE u_id = ?;', param) origin = response.fetchall()[0] origin = list(origin) if u_name is not None: origin[1] = u_name if u_role is not None: origin[2] = u_role if u_password is not None: origin[3] = u_password if u_phone is not None: origin[4] = u_phone if c_id is not None: origin[5] = c_id param = tuple(origin) + (u_id, ) conn.execute( 'UPDATE user ' 'SET u_id = ?, u_name = ?, u_role = ?, u_password = ?, ' 'u_phone = ?, c_id = ? ' 'WHERE u_id = ?;', param) except Exception: conn.close() return "Fail", traceback.print_exc() conn.commit() conn.close() return "Success", ""
def delete_user(self, u_id): """ delete_user :return: 'Success', '' or 'Fail', 'error_msg' """ log("delete_user: %s" % (u_id)) conn = sqlite3.connect("demo.db") param = (u_id, ) try: conn.execute('DELETE FROM user WHERE u_id = ?;', param) except Exception: conn.close() return "Fail", traceback.print_exc() conn.commit() conn.close() return "Success", ""
def update_community(self, c_id, new_c_name, u_id): """ update_community :return: 'Success', '' or 'Fail', 'error_msg' """ log("update community: %s %s %s" % (c_id, new_c_name, u_id)) conn = sqlite3.connect("demo.db") param = ( new_c_name, u_id, c_id, ) conn.execute( 'UPDATE community ' 'SET c_name = ?, u_id = ? WHERE c_id = ?;', param) conn.commit() conn.close() return "Success", ""
def validate_user(u_name, u_password): """ 验证(u_name, u_password) :param u_name: :param u_password: :return: True, [user_info] or False, "" """ log("validate_user %s" % u_name) if not u_name or not u_password: return False, "" status, info = user.get_all_user_info() if not status: return False, "guest" for i in info: if (u_name, u_password) == (i[1], i[3]): return True, i return False, "guest"
def add_community(self, c_name, u_id=0): """ :param c_name: :return: 'Success', '' or 'Fail', 'error_msg' """ log("add community: %s %s" % (c_name, u_id)) conn = sqlite3.connect("demo.db") try: param = ( c_name, u_id, ) conn.execute('INSERT INTO community(c_name, u_id) VALUES (?, ?);', param) except Exception: conn.close() return "Fail", traceback.print_exc() conn.commit() conn.close() return "Success", ""
def delete_community(self, c_id): """ :param c_id: :return: 'Success', '' or 'Fail', 'error_msg' """ log("delete community: %s" % (c_id)) conn = sqlite3.connect("demo.db") # check if there is any users belonging to this community param = (c_id, ) response = conn.execute('SELECT * FROM user WHERE c_id = ?;', param) response = response.fetchall() if not response == []: return "Fail", \ "There are still users belonging to community %s" % c_id try: param = (c_id, ) conn.execute('DELETE FROM community WHERE c_id = ?;', param) except Exception: conn.close() return "Fail", traceback.print_exc() conn.commit() conn.close() return "Success", ""