def getUser(self, form): if form == None: return jsonify(Error="Malformed post request"), 400 if "user_id" in form: user_id = form['user_id'] if user_id == '': user_id = ' ' else: user_id = ' ' if "username" in form: username = form['username'] if username == '': username = '******' else: username = '******' if user_id == ' ' and username == ' ': return jsonify(Error="Missing email or phone"), 400 if username != ' ': dao = UserDAO() user_list = dao.getAllUsersByUsername(username) mapped_result = [] print(user_list) for r in user_list: user = dao.getUserByID(r) mapped_result.append(self.mapToUserDict(user)) return jsonify(User=mapped_result) if user_id != ' ': return self.getUserByUserID(user_id) else: return jsonify(Error="Malformed post request"), 400
def getAllUsers(self): dao = UserDAO() user_list = dao.getAllUsers() mapped_result = [] for r in user_list: mapped_result.append(self.mapToUserDict(r)) return jsonify(User=mapped_result)
def getUserByUserID(self, uid): dao = UserDAO() user = dao.getUserByID(uid) if user == None: return jsonify(Error="USER NOT FOUND"), 400 else: mapped = self.mapToUserDict(user) return jsonify(User=mapped)
def insertContact(self, form): # print("form: ", form) if len(form) == 3: firstname = form['firstname'] lastname = form['lastname'] email = None phonenumber = None if "email" in form: email = form['email'] if email == '': return jsonify(Error="Malformed post request"), 400 elif "phonenumber" in form: phonenumber = form['phonenumber'] if phonenumber == '': return jsonify(Error="Malformed post request"), 400 else: return jsonify(Error="Malformed post request"), 400 if firstname and lastname: if email: user = UserDAO().getUserIDOnlyByCredentialsEmail( firstname, lastname, email) if phonenumber: user = UserDAO().getUserIDOnlyByCredentialsPhone( firstname, lastname, phonenumber) if user == None: return jsonify(Error="User doesn't exist"), 400 cid = user[0] dao = ContactDAO() contact = dao.getContactByID(cid) if contact == None: cid = dao.insert(cid) return self.getContactByID(cid) else: return jsonify(Error="Contact already exists"), 400 if len(form) == 1: cid = form['id'] user = UserDAO().getUserIDOnly(cid) if user == None: return jsonify(Error="User doesn't exist"), 400 dao = ContactDAO() contact = dao.getContactByID(cid) if contact == None: cid = dao.insert(cid) return self.getContactByID(cid) else: return jsonify(Error="Contact already exists"), 400 else: return jsonify(Error="Malformed post request"), 400
def getAllContactsOfUser(self, uid): user = UserDAO().getUserByID(uid) if user == None: return jsonify(Error="USER NOT FOUND"), 400 dao = ContactDAO() contact_list = dao.getAllContactsOfUser(uid) mapped_result = [] for r in contact_list: mapped_result.append(self.mapToContactDict(r)) return jsonify(Contact=mapped_result)
def loginUser(self, form): print(form) if form == None: return jsonify(Error="Malformed post request"), 400 # if password is not in form or is empty if "password" in form: password = form['password'] if password == '': return jsonify(Error="Missing password"), 400 else: return jsonify( Error="Malformed post request (Did not include password)"), 400 # verify if email is not in form or left blank if "email" in form: email = form['email'] if email == '': email = ' ' else: email = ' ' if "phonenumber" in form: phonenumber = form['phonenumber'] if phonenumber == '': phonenumber = ' ' else: phonenumber = ' ' if email == ' ' and phonenumber == ' ': return jsonify(Error="Missing email or phone"), 400 dao = UserDAO() if dao.loginByEmail(password, email) != None: return [dao.loginByEmail(password, email)[0]] if dao.loginByPhone(password, phonenumber) != None: return [dao.loginByPhone(password, phonenumber)[0]] return -1
def getChatGroupSubscribers(self, chid): dao = ChatDAO() result = dao.getAllChatSubscribers(chid) print(result) members = [] if result == None: return jsonify(Error="CHAT NOT FOUND"), 404 else: dao = UserDAO() for x in result: user = dao.getUserByID(x[0]) members.append(UserHandler().mapToUserDict(user)) return jsonify(members=members)
def insertUser(self, form): # print("form: ", form) if form == None: return jsonify(Error="Malformed post request"), 400 # if password is not in form or is empty if "password" in form: password = form['password'] if password == '': return jsonify(Error="Missing password"), 400 else: return jsonify(Error="Malformed post request"), 400 # if username is not in form or is empty if "username" in form: username = form['username'] if username == '': return jsonify(Error="Missing username"), 400 else: return jsonify(Error="Malformed post request"), 400 # if firstname is not in form or is empty if "firstname" in form: firstname = form['firstname'] if firstname == '': return jsonify(Error="Missing firstname"), 400 else: return jsonify(Error="Malformed post request"), 400 # if lastname is not in form or is empty if "lastname" in form: lastname = form['lastname'] if lastname == '': return jsonify(Error="Missing lastname"), 400 else: return jsonify(Error="Malformed post request"), 400 # verify if email is not in form or left blank if "email" in form: email = form['email'] if email == '': email = ' ' else: email = ' ' if "phonenumber" in form: phonenumber = form['phonenumber'] if phonenumber == '': phonenumber = ' ' else: phonenumber = ' ' if email == ' ' and phonenumber == ' ': return jsonify(Error="Missing email or phone"), 400 # Validate email or phonenumber entered at registration if email != ' ': if UserDAO().validateEmail(email) != None: return jsonify(Error="email taken"), 400 if phonenumber != ' ': if UserDAO().validatePhone(phonenumber) != None: return jsonify(Error="phonenumber taken"), 400 # OPTIONAL # verify username is not taken # if UserDAO().validateUsername(username) != None: # return jsonify(Error="Username taken"), 400 if password and username and firstname and lastname: dao = UserDAO() uid = dao.insert(username, firstname, lastname, email, phonenumber, password) return self.getUserByUserID(uid)