Exemplo n.º 1
0
async def transfer_to_account(account_id: int,
                              body: schemas.TransferAccountRequest):
    account1 = await Account.get(account_id)
    if account1 is None:
        raise ObjectNotFound(model="account", identifier=account_id)

    account2 = await Account.get(body.account_id)
    if account2 is None:
        raise ObjectNotFound(model="account", identifier=body.account_id)

    await account1.transfer(account2, body.amount)
Exemplo n.º 2
0
    def find_by_name(self, name, many=False):
        """Find child by informed name (and raises an exception if doesn't
        find).

        Attributes:

            * name - object name to find
            * many - boolean attribute that means it returns many objects
              or not - in the case there are more than one object with the
              same name
        """
        found = []

        # Get object children
        children = self.get_children()

        for child in children:
            # Child with the name it is searching for
            if getattr(child, 'name', None) == name:
                found.append(child)

            # Search on child's children
            try:
                print(child, name)
                ch_found = child.find_by_name(name, many=True)
            except ObjectNotFound:
                ch_found = []

            found.extend(ch_found)

        # Cleans using a set
        found = list(set(found))

        # Found nothing
        if not found:
            raise ObjectNotFound('There is no child with name "%s"' % name)

        # Found many
        elif len(found) > 1 and not many:
            raise ManyObjectsFound('There are many childs with name "%s"' %
                                   name)

        return many and found or found[0]
Exemplo n.º 3
0
async def deposit_to_account(account_id: int,
                             body: schemas.DepositAccountRequest):
    account = await Account.get(account_id)
    if account is None:
        raise ObjectNotFound(model="account", identifier=account_id)
    await account.change_balance(body.amount)