示例#1
0
    def create(self, source, target, date, amount, description=''):
        if source is None:
            raise TypeError('source Account is None.')

        if target is None:
            raise TypeError('target is None')

        if source.key() == target.key():
            raise ValueError('source and target Accounts are the same.')

        # TODO: calculate exchange rate between account's currencies
        exchange_rate = 1.0
        app.logger.debug(source)
        app.logger.debug(target)
        app.logger.debug(date)
        app.logger.debug(amount)
        app.logger.debug(description)
        app.logger.debug(exchange_rate)
        transaction = Transaction(source=source, target=target, date=date, amount=amount,
                        exchange_rate=exchange_rate, description=description)
        transaction.put()

        # XXX: precision problems?
        reverse_amount = exchange_rate * amount
        reverse_rate = 1 / exchange_rate
        reverse = Transaction(source=target, target=source, date=date, amount=reverse_amount,
                        exchange_rate=reverse_rate, description=description, reverse=transaction)
        reverse.put()

        transaction.reverse = reverse
        transaction.put()

        return transaction