def addGroupToDB(self): self.group1 = Group.createGroup("First Group") self.group2 = Group.createGroup("Second Group Old Name") self.group1.addUser(self.user1) self.group1.addUser(self.user3) self.group1.addUser(self.user4) self.group2.addUser(self.user1) self.group2.addUser(self.user2) self.group2.addUser(self.user4)
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 getUsers(): """ Get all users from the specified group. :param groupID: the group's ID :type groupID: int :return: a JSON object that contains the profiles of a list of users, status code :rtype: json, int :raises sqlalchemy.orm.exc.NoResultFound: when the group/user does not exist in database """ groupID = request.args.get('groupID') try: group = Group.getGroup(groupID) except NoResultFound: error = "Group Not Found" return error, 404 users = group.getUsers() users = [user.serialize for user in users] return jsonify(users=users)
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 getPerformanceByGroupAndEmail(): """ Get the user's performance in the specified group. :param groupID: the group's ID :param email: the user's email :type groupID: int :type email: str :return: a JSON object that contains the user's performance in the specified group. :rtype: json, int :raises sqlalchemy.orm.exc.NoResultFound: when the group does not exist in database """ groupID = request.args.get('groupID') email = request.args.get('email') try: group = Group.getGroup(int(groupID)) except NoResultFound: error = "Group Not Found" return error, 404 performance = group.getPerformanceByEmail(email) if performance is None: error = "User " + email + " never joined the group before." return error, 404 return jsonify(performance)
def edit(): """ Edit a group's name. :param groupID: the group's ID :param groupName: the intended new name for the group. :type groupID: int :type groupName: str :return: a message that marks the success of editing the group name, status code :rtype: str, int :raises KeyError: when lack of required fields of inputs :raises sqlalchemy.orm.exc.NoResultFound: when the group does not exist in database """ data = request.data dataDict = loads(data) try: groupID = dataDict['groupID'] groupName = dataDict['groupName'] except KeyError: error = "Lack Input Parameters" return error, 400 try: group = Group.getGroup(groupID) except NoResultFound: error = "Group Not Found" return error, 404 group.setName(groupName) return "Group Name Successfully Edited"
def test_edit(self): json = {'groupID': 2, 'groupName': 'Second Group New Name'} response = requests.put('http://localhost:8080/api/group/edit', json=json) self.assertEqual(response.status_code, 200) group2 = Group.getGroup(2) self.assertEqual(group2.getName(), 'Second Group New Name')
def test_create(self): json = {'email': '*****@*****.**', 'groupName': 'Third Group'} response = requests.post('http://localhost:8080/api/group/create', json=json) self.assertEqual(response.status_code, 200) group3 = Group.getGroup(3) self.assertEqual(group3.getName(), 'Third Group')
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 getChores(): """ Get all active chores or completed chores for a particular user in a particular group. :param email: the email of the user :param groupID: a particular groupID that the user is in :param completed: a boolean indicating whether a list of active chores or completed chores get return :type email: str :type groupID: int :type completed: boolean :return: a list of JSON chore objects, status code :rtype: list of JSON objects, int :raises ValueError: if the input of the completed parameter is neither true or false :raises sqlalchemy.orm.exc.NoResultFound: if the user is not found in the database """ userEmail = request.args.get('email') groupID = request.args.get('groupID') completed = request.args.get('completed') try: completed = loads(completed.lower()) except ValueError: error = "Input Format Error" return error, 400 if userEmail is None or groupID is None: error = "Invalid input Parameters" return error, 400 try: group = Group.getGroup(groupID) except NoResultFound: error = "Group Not Found" return error, 404 activeChoreList = [] completedChoreList = [] choreList = group.getChores() resultList = [] for chore in choreList: if chore.getUserEmail() == userEmail and chore.getCompleted() == False: activeChoreList.append(chore) elif chore.getUserEmail() == userEmail and chore.getCompleted( ) == True: completedChoreList.append(chore) if completed == False: resultList.append([chore.serialize for chore in completedChoreList]) elif completed == True: resultList.append([chore.serialize for chore in activeChoreList]) return jsonify(Chorelist=resultList)
def setupClass(self): app = createApp(True) app.app_context().push() db.create_all() self.user1 = User.createUser("*****@*****.**", "Michael", "hello", "Michael", "Shea") self.user2 = User.createUser("*****@*****.**", "kaitlyne", "hello", "Kaitlyne", "Chan") self.user3 = User.createUser("*****@*****.**", "hello", "hello", "helloe", "hello") self.group = Group.createGroup("My Apartment")
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 getByID(): """ Get information about a group, using the group's ID. :param groupID: the group's ID :type groupID: int :return: a JSON object that describes the group, status code :rtype: json, int :raises sqlalchemy.orm.exc.NoResultFound: when the group does not exist in database """ groupID = request.args.get('groupID') try: group = Group.getGroup(groupID) except NoResultFound: error = "Group Not Found" return error, 404 return jsonify(group.serialize)
def createChore(): """ Create a new Chore object and add it to the database. :param name: name of the chore :param groupID: the unique ID of the group where the chore will be added :param description: (optional) more information about the chore :type name: str :type groupID: int :type description: str :return: chore ID, status code :rtype: str, int :raises KeyError: name or group ID was not specified :raises sqlalchemy.orm.exc.NoResultFound: user or group does not exist """ data = request.data dataDict = loads(data) try: choreName = dataDict['name'] groupID = dataDict['groupID'] except KeyError: error = "Name or group ID not specified" return error, 400 try: group = Group.getGroup(groupID) except NoResultFound: error = "Group not found" return error, 404 if 'description' in dataDict: choreDescription = dataDict['description'] else: choreDescription = None chore = Chore.createChore(choreName, description=choreDescription) group.addChore(chore) return str(chore.id)
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 getCompletedOrIncompletedChores(): """ Get a list of a group's completed or incomplete chores. :param groupID: the group's ID :param completed: whether to get incompleted or completed chores :type groupID: int :type completed: boolean :return: a JSON object that contains the descriptions of a list of chores, status code :rtype: json, int :raises sqlalchemy.orm.exc.NoResultFound: when the group does not exist in database """ groupID = request.args.get('groupID') completed = request.args.get('completed') try: group = Group.getGroup(groupID) except NoResultFound: error = "Group Not Found" return error, 404 try: completed = loads(completed.lower()) except ValueError: error = "Input Format Error" return error, 400 chores = group.getChores() chores = [ chore.serialize for chore in chores if chore.getCompleted() == completed ] return jsonify(chores=chores)
def setUp(self): db.create_all() self.user = User.createUser("*****@*****.**", "kaitlyne", "hello", "Kaitlyne", "Chan") self.group = Group.createGroup("My Apartment") self.group.addUser(self.user)
# Menu for UrbanBurger user1= User(name="gloomy", email="*****@*****.**"); session.add(user1) session.commit() user2= User(name="happy", email="*****@*****.**"); session.add(user2) session.commit() user3= User(name="funny", email="*****@*****.**"); session.add(user3) session.commit() design_group = Group(name="design"); session.add(design_group) session.commit() art_group = Group(name="art"); session.add(art_group) session.commit() post1 = Post(subject="a sad day", content="i'm depressed", group=design_group, user=user1, date_added = datetime.strptime('Jun 1 2005 1:33PM', '%b %d %Y %I:%M%p')) session.add(post1) session.commit()