def create(): """ Create a new group and add it to the database. The user who created the group is automatically added as a member. :param email: the user's email :param groupName: the intended name for the group :type email: str :type groupName: str :return: group ID, status code :rtype: str, int :raises KeyError: when lack of required fields of inputs :raises sqlalchemy.orm.exc.NoResultFound: when the user does not exist in the database """ data = request.data dataDict = loads(data) try: email = dataDict['email'] groupName = dataDict['groupName'] except KeyError: error = "Lack Input Parameters" return error, 400 try: user = User.getUser(email) except NoResultFound: error = "User Not Found" return error, 404 group = Group.createGroup(groupName) group.addUser(user) return str(group.id)
def validatePassword(): """ Validate email and password when a user log in to the app. :param email: the email of the user :param password: password of a user :type email: str :type groupID: password :return: Json with a result item, true/false :rtype: Json object :raises KeyError: if the input is not provided by the user """ data = request.data dataDict = loads(data) try: userEmail = dataDict['email'] userPassword = dataDict['password'] except KeyError: error = "Invalid input Parameters" return error, 400 try: user = User.getUser(userEmail) except NoResultFound: return jsonify(result=False) if bcrypt.hashpw(userPassword.encode(), user.getPassword()) == user.getPassword(): return jsonify(result=True) else: return jsonify(result=False)
def test_complete_chore_late(self): self.addChoreToDB("sweep") chore = Chore.getChore(1) user = User.getUser("*****@*****.**") user.addChore(chore) deadline = datetime.strptime("11/30/2017", "%m/%d/%Y") chore.setDeadline(deadline) response = requests.put('http://*****:*****@ucla.edu") totalChores = performance["total"] onTimeChores = performance["onTime"] self.assertTrue(completed) self.assertIsNone(userEmail) self.assertIsNone(deadline) self.assertEqual(totalChores, 1) self.assertEqual(onTimeChores, 0)
def test_add_users(self): json = {'groupID': 1, 'listOfEmails': ['*****@*****.**']} response = requests.put('http://*****:*****@ucla.edu') self.assertEqual(user2.getGroups()[0].getName(), 'First Group')
def test_remove_user(self): json = {'groupID': 1, 'email': '*****@*****.**'} response = requests.put('http://*****:*****@ucla.edu') self.assertEqual(len(user3.getGroups()), 0)
def modifyUser(): """ Modify fields of a User object. :param newemail: (optional) the new email of the user :param oldemail: the original email of the user :param username: (optional) the new username of the user :param password: (optional) the new password of the user :param firstName: (optional)the new firstName of the user :param lastName: (optional) the new lastName of the user :type newemail: str :type oldemail: str :type username: str :type password: str :type firstName: str :type lastName: str :return: "Successfully Modified", status code :rtype: str, int :raises KeyError: if the input is not provided by the user """ data = request.data dataDict = loads(data) try: useroldEmail = dataDict['oldemail'] except KeyError: error = "Invalid input Parameters" return error, 400 user = User.query.filter_by(email=useroldEmail).one() if 'newemail' in dataDict: if dataDict['newemail'] != dataDict['oldemail']: try: user = User.getUser(dataDict['newemail']) except NoResultFound: user.setEmail(dataDict['newemail']) else: error = "Cannot change Email" return error, 400 else: error = "Cannot change Email" return error, 400 if 'password' in dataDict: userPassword = dataDict['password'] salt = bcrypt.gensalt() userPassword = bcrypt.hashpw(userPassword.encode(), salt) user.setPassword(userPassword) if 'firstName' in dataDict: user.setFirstName(dataDict['firstName']) if 'lastName' in dataDict: user.setLastName(dataDict['lastName']) if 'username' in dataDict: user.setUserName(dataDict['username']) return "Successfully Modified"
def test_complete_chore_invalid_input(self): self.addChoreToDB("sweep") chore = Chore.getChore(1) user = User.getUser("*****@*****.**") user.addChore(chore) deadline = datetime.strptime("12/12/2020", "%m/%d/%Y") chore.setDeadline(deadline) response = requests.put('http://localhost:8080/api/chore/complete', data='{"id": 500}') self.assertGreaterEqual(response.status_code, 400)
def completeChore(): """ User completes a chore. Precondition: chore must be assigned to a user and have a deadline. :param id: the unique ID corresponding to the target chore :type id: int :return: a message confirming that the chore was successfully completed, status code :rtype: str, int :raises KeyError: chore ID was not specified :raises sqlalchemy.orm.exc.NoResultFound: chore corresponding to the specified ID does not exist """ data = request.data dataDict = loads(data) try: choreID = dataDict['id'] except KeyError: error = "No ID specified" return error, 400 try: chore = Chore.getChore(choreID) except NoResultFound: error = "Chore not found" return error, 404 userEmail = chore.getUserEmail() if userEmail is None: error = "Chore is not assigned to a user" return error, 412 deadline = chore.getDeadline() if deadline is None: error = "Chore does not have a deadline" return error, 412 group = Group.getGroup(chore.getGroupID()) group.incrementPerformanceTotalByEmail(userEmail) if not chore.deadlinePassed(): group.incrementPerformanceOnTimeByEmail(userEmail) chore.setCompleted(True) chore.setDeadline(None) user = User.getUser(userEmail) user.removeChore(chore) return "Chore successfully completed"
def getByEmail(): """ Get a list of groups that a user is in. :param email: the user's email :type email: str :return: a JSON object that contains the descriptions of a list of groups, status code :rtype: json, int :raises sqlalchemy.orm.exc.NoResultFound: when the user does not exist in database """ email = request.args.get('email') try: user = User.getUser(email) except NoResultFound: error = "User Not Found" return error, 404 groups = user.getGroups() return jsonify(groups=[group.serialize for group in groups])
def removeUser(): """ Remove a user from the group. :param groupID: the group's ID :param email: the user's email :type groupID: int :type email: str :return: a message that marks the success of removing a member from the group, status code :rtype: str, int :raises KeyError: when lack of required fields of inputs :raises sqlalchemy.orm.exc.NoResultFound: when the group/user does not exist in database """ data = request.data dataDict = loads(data) try: groupID = dataDict['groupID'] email = dataDict['email'] except KeyError: error = "Lack Input Parameters" return error, 400 try: group = Group.getGroup(groupID) except NoResultFound: error = "Group Not Found" return error, 404 try: user = User.getUser(email) except NoResultFound: error = "User " + email + " Not Found" return error, 404 group.removeUser(user) if len(group.getUsers()) == 0: Group.deleteGroup(group.getID()) return "User Successfully Removed From The Group"
def addUsers(): """ Add users to a group. :param groupID: the group's ID :param listOfEmails: the list of user's emails waiting to be added to the group :type groupID: int :type listOfEmails: list of str :return: a message that marks the success of adding members to the group, status code :rtype: str, int :raises KeyError: when lack of required fields of inputs :raises sqlalchemy.orm.exc.NoResultFound: when the group/user does not exist in database """ data = request.data dataDict = loads(data) try: groupID = dataDict['groupID'] listOfEmails = dataDict['listOfEmails'] except KeyError: error = "Lack Input Parameters" return error, 400 try: group = Group.getGroup(groupID) except NoResultFound: error = "Group Not Found" return error, 404 for email in listOfEmails: try: user = User.getUser(email) except NoResultFound: error = "User " + email + " Not Found" return error, 404 group.addUser(user) return "Users Successfully Added To The Group"
def getUser(): """ Get information about a user. :param email: the email of the user :type email: str :return: a user JSON object, status code :rtype: JSON object, int :raises sqlalchemy.orm.exc.NoResultFound: if the user is not found in the database """ userEmail = request.args.get('email') if userEmail is None: error = "Invalid input Parameters" return error, 400 try: user = User.getUser(userEmail) except NoResultFound: error = "User Not found" return error, 404 return jsonify(user.serialize)
def assignUserOrDeadlineToChore(): """ Assign a user and/or deadline to a chore. Notes: For initial assignment, email and deadline are required parameters. For editing the deadline or assigned user later, email and deadline are optional parameters. Postcondition: both user and deadline must not be null. :param id: the unique ID corresponding to the target chore :param email: the email of the user who will be assigned to the chore :param deadline: the new deadline for the chore (format: "mm/dd/yyyy") :type id: int :type email: str :type deadline: str :return: a message confirming that the user and deadline have been set, status code :rtype: str, int :raises KeyError: chore ID was not specified :raises sqlalchemy.orm.exc.NoResultFound: chore ID does not exist, or user email does not exist """ data = request.data dataDict = loads(data) try: choreID = dataDict['id'] except KeyError: error = "Chore ID not specified" return error, 400 try: chore = Chore.getChore(choreID) except NoResultFound: error = "Chore not found" return error, 404 chore.setCompleted(False) userEmail = None deadline = None if chore.userEmail is None and chore.deadline is None: try: userEmail = dataDict['email'] deadline = dataDict['deadline'] except KeyError: error = "Need to specify user email and deadline" return error, 400 else: if 'email' in dataDict: userEmail = dataDict['email'] if 'deadline' in dataDict: deadline = dataDict['deadline'] if userEmail is not None: try: user = User.getUser(userEmail) except NoResultFound: error = "User not found" return error, 404 user.addChore(chore) if deadline is not None: choreDeadlineStr = dataDict['deadline'] choreDeadline = datetime.strptime(choreDeadlineStr, "%m/%d/%Y") chore.setDeadline(choreDeadline) return "User assignment and deadline set successfully"