예제 #1
0
    def createGroup(self, group):
        """ Creates a new Group.

        Args:
            group(:obj:`splitwise.group.Group`): Splitwise Group Object.

        Returns:
            tuple: tuple containing:
              group(:obj:`splitwise.group.Group`): Object with Group detail

              errors(:obj:`splitwise.error.SplitwiseError`): Object representing errors
        """
        # create group
        group_info = group.__dict__

        if "members" in group_info:
            group_members = group.getMembers()
            del group_info["members"]
            Splitwise.setUserArray(group_members, group_info)

        content = self.__makeRequest(
            Splitwise.CREATE_GROUP_URL, "POST", group_info)
        content = json.loads(content)
        group_detail = None
        errors = None
        if "group" in content:
            group_detail = Group(content["group"])
            if "errors" in content["group"]:
                if len(content["group"]['errors']) != 0:
                    errors = SplitwiseError(content["group"]["errors"])

        return group_detail, errors
예제 #2
0
    def deleteGroup(self, id):
        """ Deletes the group with given id.

        Args:
            id(long): ID of the group to be deleted.

        Returns:
            tuple: tuple containing:
              success(bool): True if group deleted, False otherwise

              errors(:obj:`splitwise.error.SplitwiseError`): Object representing errors
        """
        errors = None
        success = False
        try:
            content = self.__makeRequest(
                Splitwise.DELETE_GROUP_URL+"/"+str(id), "POST")
        except SplitwiseNotAllowedException as e:
            e.setMessage("You are not allowed to access group with id %d" % id)
            raise
        except SplitwiseNotFoundException as e:
            e.setMessage("Group with id %d does not exist" % id)
            raise

        content = json.loads(content)
        if 'success' in content:
            success = content["success"]

        if 'errors' in content:
            if len(content['errors']) != 0:
                errors = SplitwiseError(content['errors'])

        return success, errors
예제 #3
0
    def updateExpense(self, expense):
        """ Updates an existing expense.

        Args:
            expense(:obj:`splitwise.expense.Expense`): Splitwise Expense Object.
            expense id must be set. Include only the fields that should be updated. null fields are ignored

        Returns:
            tuple: tuple containing:
              expense(:obj:`splitwise.expense.Expense`): Object with Expense detail

              errors(:obj:`splitwise.error.SplitwiseError`): Object representing errors
        """
        expense_id = expense.id
        if expense_id is None:
            raise SplitwiseBadRequestException("Incorrect query parameters sent. Expense Id cannot be null")

        # Get the expense Dict
        expense_data = expense.__dict__

        del expense_data['id']

        # Get users and store in a separate var
        expense_users = expense.getUsers()

        if expense_users:
            # Delete users from original dict as we
            # need to put like users_1_
            del expense_data['users']
            # Add user values to expense_data
            Splitwise.setUserArray(expense_users, expense_data)

        category = expense.getCategory()
        if category:
            expense_data["category_id"] = category.getId()

        receipt = expense.getReceiptPath()
        files = None
        if receipt:
            files = {"receipt":  io.open(receipt, "rb")}

        content = self.__makeRequest(
            Splitwise.UPDATE_EXPENSE_URL+"/"+str(expense_id), "POST", expense_data, files=files)
        content = json.loads(content)
        expense = None
        errors = None

        if files:
            files["receipt"].close()

        if 'expenses' in content:
            if len(content['expenses']) > 0:
                expense = Expense(content["expenses"][0])

        if 'errors' in content:
            if len(content['errors']) != 0:
                errors = SplitwiseError(content['errors'])

        return expense, errors
예제 #4
0
    def createExpense(self, expense):
        """ Creates a new expense.

        Args:
            expense(:obj:`splitwise.expense.Expense`): Splitwise Expense Object.

        Returns:
            tuple: tuple containing:
              expense(:obj:`splitwise.expense.Expense`): Object with Expense detail

              errors(:obj:`splitwise.error.SplitwiseError`): Object representing errors
        """
        # Get the expense Dict
        expense_data = expense.__dict__

        # Get users and store in a separate var
        expense_users = expense.getUsers()

        if expense_users:
            # Delete users from original dict as we
            # need to put like users_1_
            del expense_data['users']
            # Add user values to expense_data
            Splitwise.setUserArray(expense_users, expense_data)

        category = expense.getCategory()
        if category:
            expense_data["category_id"] = category.getId()

        receipt = expense.getReceiptPath()
        files = None
        if receipt:
            files = {"receipt": io.open(receipt, "rb")}

        content = self.__makeRequest(Splitwise.CREATE_EXPENSE_URL,
                                     "POST",
                                     expense_data,
                                     files=files)
        content = json.loads(content)
        expense = None
        errors = None

        if files:
            files["receipt"].close()

        if 'expenses' in content:
            if len(content['expenses']) > 0:
                expense = Expense(content["expenses"][0])

        if 'errors' in content:
            if len(content['errors']) != 0:
                errors = SplitwiseError(content['errors'])

        return expense, errors
예제 #5
0
    def addUserToGroup(self, user, group_id):
        """ Adds a user to the group.

        Args:
            user(:obj:`splitwise.user.User`): User to be added
            group_id(long): ID of the group

        Returns:
            tuple: tuple containing:
              success(bool): True if group deleted, False otherwise

              user(:obj:`splitwise.user.Friend`): Object representing added user details

              errors(:obj:`splitwise.error.SplitwiseError`): Object representing errors
        """
        # Get the user data
        request_data = user.__dict__
        request_data["group_id"] = group_id

        if "id" in request_data:
            request_data["user_id"] = request_data["id"]
            del request_data["id"]
        try:
            content = self.__makeRequest(Splitwise.ADD_USER_TO_GROUP_URL,
                                         "POST", request_data)
        except SplitwiseNotAllowedException as e:
            e.setMessage("You are not allowed to access group with id %d" %
                         group_id)
            raise
        except SplitwiseNotFoundException as e:
            e.setMessage("Group with id %d does not exist" % group_id)
            raise
        content = json.loads(content)
        errors = None
        success = False
        user = None
        if 'success' in content:
            success = content["success"]

        if 'errors' in content:
            if len(content['errors']) != 0:
                errors = SplitwiseError(content['errors'])

        if 'user' in content:
            if content['user'] is not None:
                user = Friend(content['user'])

        return success, user, errors
예제 #6
0
    def createComment(self, expense_id, content):
        """ Creates a new comment.

        Args:
            expense_id: Expense transaction on which a comment has to be made
            comment content: Content of the comment

        Returns:
            tuple: tuple containing:
              comment(:obj:`splitwise.comment.Comment`): Object with Comment detail

              errors(:obj:`splitwise.error.SplitwiseError`): Object representing errors
        """

        comment = None
        errors = None

        if content is None:
            raise SplitwiseBadRequestException(
                "Incorrect query parameters sent. git statContent cannot be empty"
            )

        data = dict()
        data["expense_id"] = expense_id
        data["content"] = content

        try:
            content = self.__makeRequest(Splitwise.CREATE_COMMENT_URL, "POST",
                                         data)
        except SplitwiseNotAllowedException as e:
            e.setMessage("You are not allowed to access expense with id %d" %
                         id)
            raise
        except SplitwiseNotFoundException as e:
            e.setMessage("Expense with id %d does not exist" % id)

        content = json.loads(content)

        if 'comment' in content:
            if len(content['comment']) > 0:
                comment = Comment(content['comment'])

        if 'errors' in content:
            if len(content['errors']) != 0:
                errors = SplitwiseError(content['errors'])

        return comment, errors
예제 #7
0
    def updateUser(self, user):
        """ Updates the user.

        Args:
            :obj:`splitwise.user.CurrentUser`: User object with atleast id set

        Returns:
            tuple: tuple containing:
              user(:obj:`splitwise.user.CurrentUser`): Object with User detail

              errors(:obj:`splitwise.error.SplitwiseError`): Object representing errors
        """

        if user.getId() is None:
            raise SplitwiseBadRequestException(
                "User ID is required to update user")

        user_data = user.__dict__

        try:
            content = self.__makeRequest(Splitwise.UPDATE_USER_URL, 'POST',
                                         user_data)
        except SplitwiseNotAllowedException as e:
            e.setMessage("You are not allowed to access user with id %d" %
                         user.getId())
            raise
        except SplitwiseNotFoundException as e:
            e.setMessage("User with id %d does not exist" % user.getId())
            raise
        content = json.loads(content)

        user = None
        errors = None
        if "user" in content:
            if content["user"] is not None:
                user = CurrentUser(content["user"])

        if "errors" in content:
            if len(content['errors']) != 0:
                errors = SplitwiseError(content["errors"])

        return user, errors