def addContactToChatJson(self, cid, form): print(cid) dao = chatsDAO() dao2 = usersDAO() if not dao.getChatById(cid): return jsonify(Error="Chat with id:" + str(cid) + " not found."), 404 if len(form) != 3: return jsonify(Error="Malformed add contact to chat request"), 400 ufirstname = form['ufirstname'] ulastname = form['ulastname'] uemail = form['uemail'] if ufirstname and ulastname and uemail: row = dao2.getUserByEmail(uemail) if not row: return jsonify(Error="User with email: " + str(uemail) + " not found."), 404 uid = row[0] if dao.isMember(cid, uid): return jsonify(Error="User with email: " + str(uemail) + " is already a member of chat with id: " + str(cid)) if dao.addMemberToChat(cid, uid) == 1: result = self.build_contact_attributes(ufirstname, ulastname, uemail) return jsonify(AddedContactToChat=result), 200 else: return jsonify( Error="Unexpected attributes in add contact to chat request" ), 400
def deleteChat(self, args): if len(args) != 2: return jsonify(Error="Malformed delete chat request"), 400 try: cid = int(args['cid']) uid = int(args['uid']) # cid = form['cid'] # uid = form['uid'] except: return jsonify( Error="Unexpected attributes in delete chat request"), 400 dao = chatsDAO() if not dao.getChatById(cid): return jsonify(Error="Chat not found."), 404 else: if not dao.isAdmin(cid, uid): return jsonify(Error="User " + str(uid) + " is not admin of chat " + str(cid)), 405 else: if dao.deleteMembersOfChat( cid, dao.getChatMembers(cid)) and dao.deleteChat(cid) == 1: return jsonify(DeletedChat="Chat " + str(cid) + " deleted."), 200 else: return jsonify(Error="Delete chat was unsuccessful"), 400
def createPost(self, form, file, path): dao = postsDAO() # Assumes form contains post_msg, user_id, cname if form and file and len(form) >= 3: puser = form['puser'] pphoto = "http://127.0.0.1:5000" + path + "/" + file.filename pmessage = form['pmessage'] pdate = form['pdate'] # user_id = form['puser'] chatName = form['chatName'] if puser and pphoto and pmessage and pdate: temp = pmessage p_message = pmessage.split() pmessage = "" for word in p_message: if not word[0] == "#": pmessage = pmessage + word + " " post = dao.insertPost(puser, pphoto, pmessage, pdate) post_id, pdate = post['pid'], post['pdate'] # post_id, post_date = post['post_id'], post['post_date'] hashtags = dao.getHashtagList(temp) print(hashtags) if len(hashtags) > 0: for hashtag in hashtags: print(hashtag) hid = dao.insertIntoHashtag(hashtag) dao.insertIntoTagged(post_id, hid) # Upload file file_secure_name = secure_filename(file.filename) file_path = os.path.join(path, file_secure_name) file.save(os.path.join(os.getcwd(), file_path[1:])) likes = dao.getPostLikes(post_id) dislikes = dao.getPostDislikes(post_id) cid = chatsDAO().getChatByChatName(chatName) if not cid: return jsonify(Error="Chat with name: " + str(chatName) + " not found") if not dao.insertIntoHas(cid, post_id): return jsonify(Error="Inserting into table HAS failed") row = [post_id, puser, pphoto, pmessage, pdate] # result = self.build_post_attributes(post_id, puser, pphoto, pmessage, pdate) result = self.build_post_dict_UI(row, likes, dislikes) return jsonify(Post=result), 201 else: return jsonify(Error='Malformed POST request'), 400 else: return jsonify(Error='Malformed POST request'), 400
def getAdminOfChat(self, cid): dao = chatsDAO() if not dao.getChatById(cid): return jsonify(Error="Chat not found."), 404 else: row = dao.getAdminOfChat(cid) result = self.build_users_from_chat_dict(row) return jsonify(AdminFromChat=result)
def getChatsById(self, cid): dao = chatsDAO() row = dao.getChatById(cid) if not row: return jsonify(Error="Chat Not Found"), 404 else: chat = self.build_chats_dict(row) return jsonify(Chats=chat)
def postPhotoAndMsgToChat(self, cid, photo, msg): dao = chatsDAO() postToAdd = Post(4, "clopez36", photo, "new post added", msg) for chat in dao.getChatList(): if chat.getId() == cid: chat.addPost(postToAdd)
def getChatsByChatname(self, form): dao = chatsDAO() cname = form['cname'] cid = dao.getChatByChatName(cname) if not cid: return jsonify(Error="Chat Not Found"), 404 else: # chats = self.build_chats_dict(row) return jsonify(Chat={"cid": cid})
def getChatMemberByID(self, chat_id, mem_id): dao = chatsDAO() row = dao.getChatMemberById(chat_id, mem_id) if not row: return jsonify(Error="Member Not Found"), 404 else: member = self.build_chats_dict(row) return jsonify(User=member)
def getAllChats(self): dao = chatsDAO() chats_list = dao.getAllChats() result_list = [] print(chats_list) for row in chats_list: print(row) result = self.build_chats_dict(row) result_list.append(result) return jsonify(Chats=result_list)
def getAllUsersFromChat(self, cid): dao = chatsDAO() if not dao.getChatById(cid): return jsonify(Error="Chat not found."), 404 else: users_list = dao.getAllUsersFromChat(cid) result_list = [] for row in users_list: result = self.build_users_from_chat_dict(row) result_list.append(result) return jsonify(UsersInChat=result_list)
def getAllPostsFromChat(self, args): cid = int(args['cid']) print(cid) dao = chatsDAO() posts_list = dao.getPostsFromChat(cid) result_list = [] if not posts_list: return jsonify(Error="Chat has no posts"), 404 else: for row in posts_list: post = self.build_post_dict(row) result_list.append(post) return jsonify(ChatPosts=result_list)
def searchChats(self, args): cname = args["cname"] dao = chatsDAO() if (len(args) == 1) and cname: chats_list = dao.getChatByChatName(cname) else: return jsonify(Error="Malformed query string"), 400 print(chats_list) result_list = self.build_chats_dict(chats_list) return jsonify(Chats=result_list)
def getAllChatsOfUser(self, uid): dao2 = usersDAO() if not dao2.getUserById(uid): return jsonify(Error="User " + str(uid) + " not found"), 404 dao = chatsDAO() chat_list = dao.getAllChatsOfUser(uid) if not chat_list: return jsonify(NoChats="User " + str(uid) + " is not a member of any chat."), 404 result_list = [] for row in chat_list: print(row) result = self.build_chats_dict(row) result_list.append(result) return jsonify(Chats=result_list)
def addPostToChat(self, args, form): cid = int(args['cid']) dao = chatsDAO() if not dao.getChatById(cid): return jsonify(Error="Chat not found."), 404 else: pid = form['pid'] puser = form['puser'] pphoto = form['pphoto'] pmessage = form['pmessage'] pdate = form['pdate'] plike = form['plike'] pdislikes = form['pdislikes'] if pid and puser and pphoto and pmessage and pdate and plike and pdislikes: return jsonify(AddStatus="OK"), 200 else: return jsonify( Error="Unexpected attributes in update request"), 400
def getUsersForAdding(self, cid, uid): dao = chatsDAO() udao = usersDAO() if not dao.getChatById(cid): return jsonify(Error="Chat not found."), 404 elif not udao.getUserById(uid): return jsonify(Error="User not found."), 404 else: chat_members = dao.getAllUsersFromChat(cid) if len(chat_members) == 1: return jsonify(Error="Only user in chat is admin.") contact_list = udao.getContactListFromUserId(uid) print(chat_members) print(contact_list) contacts = [] for contact in contact_list: if not contact in chat_members: contacts.append(contact) print(contacts) if len(contacts) > 0: result = [] for row in contacts: result.append(self.build_users_from_chat_dict(row)) print(result) return jsonify(Contacts=result), 200 else: return jsonify( Error="All users in contact list already in chat.")
def deleteUserFromChat(self, cid, admin_id, uid): dao = chatsDAO() dao2 = usersDAO() if not dao.getChatById(cid): return jsonify(Error="Chat with id: " + str(cid) + " not found."), 404 if not dao2.getUserById(uid): return jsonify(Error="User with id: " + str(uid) + " not found."), 404 if not dao.isMember(cid, uid): return jsonify(Error="User with id: " + str(uid) + " is not a member of chat with id: " + str(cid)) if not dao.isAdmin(cid, admin_id): return jsonify(Error="You are not admin") if admin_id == uid: return jsonify(Error="You cannot remove yourself from the chat.") if dao.deleteUserFromChat(cid, uid) == 1: return jsonify(DeletedChatMember="User: "******" from Chat " + str(cid) + " was deleted."), 200 else: return jsonify(Error="Delete user: "******" from chat: " + str(cid) + " was unsuccessful"), 400
def insertPost(self, form, file): print("form: ", form) if not form: return jsonify(Error="You cannot pass null object"), 400 if len(form) != 5: return jsonify(Error="Malformed post request"), 400 else: p_user = form['puser'] p_photo = form['pphoto'] p_message = form['pmessage'] p_date = form['pdate'] cname = form['cname'] if p_user and p_photo and p_message and p_date: dao = postsDAO() if not dao.getUserById(p_user): return jsonify(Error="User " + str(p_user) + " not found."), 404 pid = dao.insertPost(p_user, p_photo, p_message, p_date) cid = chatsDAO().getChatByChatName(cname) if not cid: return jsonify(Error="Chat with name: " + str(cname) + " not found") if not dao.insertIntoHas(cid, pid): return jsonify(Error="Inserting into table HAS failed") result = self.build_post_attributes(pid, p_user, p_photo, p_message, p_date) hashtags = dao.getHashtagList(p_message) print(hashtags) if len(hashtags) > 0: for hashtag in hashtags: print(hashtag) hid = dao.insertIntoHashtag(hashtag) dao.insertIntoTagged(pid, hid) return jsonify(Post=result), 201 else: return jsonify(Error="Unexpected attributes in post request"), 400
def createChat(self, form): print("form: ", form) if len(form) != 2: return jsonify(Error="Malformed create chat request"), 400 else: try: cname = form['cname'] cadmin = form['cadmin'] if cname and cadmin: dao = chatsDAO() cid = dao.createChat(cname, cadmin) result = self.build_chats_attributes(cid, cname, cadmin) return jsonify(Chat=result), 201 else: return jsonify( Error="Unexpected attributes in create chat request" ), 400 except: return jsonify( Error="Unexpected attributes in create chat request"), 400
def addContactToChat(self, form): dao = chatsDAO() dao2 = usersDAO() cid = form["cid"] contact_id = form["contact_id"] if not dao.getChatById(cid): return jsonify(Error="Chat with id: " + str(cid) + " not found."), 404 if not dao2.getUserById(contact_id): return jsonify(Error="User with contact id: " + str(contact_id) + " not found."), 404 if dao.isMember(cid, contact_id): return jsonify(Error="User with contact id: " + str(contact_id) + " is already a member of chat with id: " + str(cid)) if dao.addMemberToChat(cid, contact_id) == 1: return jsonify(AddedChatMember="User with contact id: " + str(contact_id) + " was added to Chat: " + str(cid)), 200 else: return jsonify(Error="Adding contact with id: " + str(contact_id) + " to chat: " + str(cid) + " was unsuccessful"), 400