def getAllChats(self): dao = Group_ChatDAO() result = dao.getAllChats() mapped_result = [] for r in result: mapped_result.append(self.mapToDict(r)) return jsonify(Chats=mapped_result)
def findChat(self, name): dao = Group_ChatDAO() result = dao.searchByChatName(name) if result == None: return jsonify(Error="CHAT NOT FOUND") else: mapped = self.mapToDict(result) return jsonify(Chat=mapped)
def getChatByID(self, id): dao = Group_ChatDAO() result = dao.getChatById(id) if result == None: return jsonify(Error="CHAT NOT FOUND") else: mapped = self.mapToDict(result) return jsonify(Chat=mapped)
def getAllMemberships(self): dao = MemberDAO() dao1 = UsersDAO() dao2 = Group_ChatDAO() result = dao.getAllMemberships() mapped_result = [] for r in result: membership = [] user = dao1.getUserById(r[0]) group = dao2.getChatById(r[1]) membership.append(user[1] + " " + user[2]) membership.append(group[1]) mapped_result.append(self.mapToDict(membership)) return jsonify(Members=mapped_result)
def getAdminOfChatID(self, group_id): dao = AdministratesDAO() dao1 = UsersDAO() dao2 = Group_ChatDAO() result = dao.getAdminOfGroupID(group_id) if result == None: return jsonify(Error="GROUP NOT FOUND") else: mapped_result = [] info = [] user = dao1.getUserById(result[0]) group = dao2.getChatById(result[1]) info.append(user[1] + " " + user[2]) # user name info.append(group[1]) # group name mapped_result.append(self.mapToDict(info)) return jsonify(Admins=mapped_result)
def getAllAdmins(self): dao = AdministratesDAO() dao1 = UsersDAO() dao2 = Group_ChatDAO() result = dao.getAllAdmins() mapped_result = [] for r in result: result2 = dao1.getUserById(r[0]) result3 = dao2.getChatById(r[1]) name_chat = ['', ''] name_chat[0] = result2[1] + " " + result2[2] name_chat[1] = result3[1] mapped_result.append(self.mapToDict(name_chat)) return jsonify(Admins=mapped_result)
def getChatsAdministratedByUser(self, users_id): dao = AdministratesDAO() dao1 = UsersDAO() dao2 = Group_ChatDAO() result = dao.getChatsAdministratedByUser(users_id) if result == None: return jsonify(Error="CHAT NOT FOUND") else: mapped_result = [] for r in result: info = [] user = dao1.getUserById(r[0]) group = dao2.getChatById(r[1]) info.append(user[1] + " " + user[2]) # user name info.append(group[1]) # group name mapped_result.append(self.mapToDict(info)) return jsonify(Admins=mapped_result)
def removeMember(self, form, users_id): if len(form) != 3: return jsonify(Error="Malformed post request"), 400 else: first_name = form['first_name'] last_name = form['last_name'] group_name = form['group_name'] if first_name and last_name and group_name: Mdao = MemberDAO() Udao = UsersDAO() Gdao = Group_ChatDAO() Adao = AdministratesDAO() group_info = Gdao.searchByChatName( group_name) # get group info print(group_info) member_info = Udao.getUserByName( first_name, last_name) # get member info to wish is desired to remove print(member_info) check = Mdao.getMembershipsByUserAndGroup( member_info[0], group_info[0]) # check if user already in group print(check) admin_of_group = Adao.getAdminOfGroupID( group_info[0]) # get admin id admin_info = Udao.getUserById( admin_of_group[0]) # get admin info print(users_id) print(admin_of_group) # check if user is in group and the user trying to add is admin if check and (users_id == admin_of_group[0]): Mdao.remove(member_info[0], group_info[0]) # add user to group result = self.member_attributes(first_name, last_name, group_name) return jsonify(users=result), 201 else: return jsonify( Error="Invalid admin or user already in group"), 400 else: return jsonify( Error="Unexpected attributes in post request"), 400
def getMembershipByChatID(self, group_id): dao = MemberDAO() dao1 = UsersDAO() dao2 = Group_ChatDAO() result = dao.getMembershipsOfGroupID(group_id) # print(result) if result == None: return jsonify(Error="MEMBERSHIP NOT FOUND") else: mapped_result = [] for r in result: membership = [] user = dao1.getUserById(r[0]) group = dao2.getChatById(r[1]) membership.append(user[1] + " " + user[2]) membership.append(group[1]) mapped_result.append(self.mapToDict(membership)) return jsonify(Members=mapped_result)
def checkMember(self, users_id, group_id): dao = MemberDAO() dao1 = UsersDAO() dao2 = Group_ChatDAO() result = dao.getMembershipsByUserAndGroup(users_id, group_id) if result == None: return None else: mapped_result = [] for r in result: membership = [] user = dao1.getUserById(r[0]) group = dao2.getChatById(r[1]) membership.append(user[1] + " " + user[2]) membership.append(group[1]) mapped_result.append(self.mapToDict(membership)) return jsonify(Members=mapped_result)
def checkGroup(self, form): if len(form) != 3: return jsonify(Error="Malformed post request"), 400 else: group_name = form['group_name'] first_name = form['first_name'] last_name = form['last_name'] if group_name and first_name and last_name: dao = Group_ChatDAO() check = dao.check( group_name) # only checking if group exists or not if (check is None): return None else: result = self.group_attributes(group_name, first_name, last_name) return jsonify(users=result), 201 else: return jsonify( Error="Unexpected attributes in post request"), 400
def removeGroup(self, form): if len(form) != 3: return jsonify(Error="Malformed post request"), 400 else: group_name = form['group_name'] first_name = form['first_name'] last_name = form['last_name'] # Check if user is the admin by name if group_name and first_name and last_name: Gdao = Group_ChatDAO() Adao = AdministratesDAO() Udao = UsersDAO() # it is presume user already exists group_info = Gdao.searchByChatName( group_name) #returns chat info print(group_info) admin_info = Adao.getAdminOfGroupID( group_info[0]) # get admin of group print(admin_info) users_info = Udao.getUserById(admin_info[0]) # get admin info print(users_info) # Check if admin is the same as user if (users_info[1] == first_name and users_info[2] == last_name): administrates_info = Adao.remove( users_info[0], group_info[0]) # remove by users_id and group_id group_removed = Gdao.remove(group_name) #remove group result = self.group_attributes(group_name, first_name, last_name) return jsonify(users=result), 201 else: return jsonify(Error="Invalid admin"), 400 else: return jsonify( Error="Unexpected attributes in post request"), 400
def registerGroup(self, form): if len(form) != 3: return jsonify(Error="Malformed post request"), 400 else: group_name = form['group_name'] first_name = form['first_name'] last_name = form['last_name'] if group_name and first_name and last_name: Gdao = Group_ChatDAO() Adao = AdministratesDAO() Udao = UsersDAO() # it is presume user already exists users_info = Udao.getUserByName(first_name, last_name) group_info = Gdao.insert(group_name) administrates_info = Adao.insert( users_info[0], group_info) #insert users_id and group_id result = self.group_attributes(group_name, first_name, last_name) return jsonify(users=result), 201 else: return jsonify( Error="Unexpected attributes in post request"), 400
def getMembershipByUName(self, first_name, last_name): dao = MemberDAO() dao1 = UsersDAO() dao2 = Group_ChatDAO() result = dao.getMembershipsByUserName(first_name, last_name) # print(result) if result == None: return jsonify(Error="MEMBERSHIP NOT FOUND") else: mapped_result = [] for r in result: membership = [] user = dao1.getUserById(r[1]) group = dao2.getChatById(r[0]) # print(user) # print(group) membership.append(user[1] + " " + user[2]) membership.append(group[1]) membership.append(group[0]) membership.append(user[0]) print(membership[2]) mapped_result.append(self.mapToDict(membership)) return jsonify(Chats=mapped_result)