def get_user_com_chats(user_id): try: chat_members = SESSION.query(ChatMembers).filter( ChatMembers.user == int(user_id)).all() return [i.chat for i in chat_members] finally: SESSION.close()
def rem_chat(chat_id): with INSERTION_LOCK: chat = SESSION.query(Chats).get(str(chat_id)) if chat: SESSION.delete(chat) SESSION.commit() else: SESSION.close()
def del_user(user_id): with INSERTION_LOCK: curr = SESSION.query(Users).get(user_id) if curr: SESSION.delete(curr) SESSION.commit() return True ChatMembers.query.filter(ChatMembers.user == user_id).delete() SESSION.commit() SESSION.close() return False
def num_users(): try: return SESSION.query(Users).count() finally: SESSION.close()
def num_chats(): try: return SESSION.query(Chats).count() finally: SESSION.close()
def get_user_num_chats(user_id): try: return SESSION.query(ChatMembers).filter( ChatMembers.user == int(user_id)).count() finally: SESSION.close()
def get_all_users(): try: return SESSION.query(Users).all() finally: SESSION.close()
def get_all_chats(): try: return SESSION.query(Chats).all() finally: SESSION.close()
def get_chat_members(chat_id): try: return SESSION.query(ChatMembers).filter( ChatMembers.chat == str(chat_id)).all() finally: SESSION.close()
def get_name_by_userid(user_id): try: return SESSION.query(Users).get(Users.user_id == int(user_id)).first() finally: SESSION.close()
def get_userid_by_name(username): try: return SESSION.query(Users).filter( func.lower(Users.username) == username.lower()).all() finally: SESSION.close()