Ejemplo n.º 1
0
    def run(self, username, stock_symbol, amount):
        self.session = get_session()
        user = self.session.query(User).filter_by(username=username).first()
        if not user:
            raise UserNotFoundError(username)
            xml.log_error('BUY', 'username not found')
        # Check for existing uncommitted transaction
        if self.session.query(Transaction).filter_by(
                user=user, operation='BUY', committed=False).count() > 0:
            raise BuyTransactionActiveError()
            xml.log_error('BUY', 'Outstanding Buy Exists')

        # Getting stock quote
        quote_client = get_quote_client()
        quote = quote_client.get_quote(stock_symbol, username)

        # Work out quantity of stock to buy, fail if not enough for one stock
        amount = Money.from_string(amount)
        quantity = amount_to_quantity(quote, amount)
        if quantity == 0:
            raise InsufficientFundsError()

        price = quote * quantity
        if user.account_balance < price:
            raise InsufficientFundsError()

        transaction = Transaction(user=user, quantity=quantity, operation='BUY', 
                stock_symbol=stock_symbol, stock_value=quote, committed=False)

        self.session.add(transaction)
        self.session.commit()

        xml.log_transaction('BUY', transaction) 
        return xml.QuoteResponse(quantity=quantity, price=price)
Ejemplo n.º 2
0
    def run(self, username):
        self.session = get_session()
        user = self.session.query(User).filter_by(username=username).first()
        if not user:
            xml.log_error('CANCEL_BUY', 'username not found')
            raise UserNotFoundError(username)
        transaction = self.session.query(Transaction).filter_by(
            username=user.username, operation='BUY', committed=False
        ).first()
        if not transaction:
            raise NoBuyTransactionError(username)

        self.session.delete(transaction)
        self.session.commit()

        xml.log_transaction('CANCEL_BUY', transaction, status_message='success')
        return xml.ResultResponse('success')
Ejemplo n.º 3
0
    def run(self, username='', stock_symbol=''):

        if not stock_symbol:
            xml.log_error('QUOTE', 'No stock symbol given')
            raise InvalidInputError('No stock symbol given')

        if len(stock_symbol) > 4:
            xml.log_error('QUOTE', 'stock symbol too long')
            raise InvalidInputError('stock symbol too long: %d' % \
                    len(stock_symbol))

        quote_client = get_quote_client()
        quote = quote_client.get_quote(stock_symbol, username)

        #create log
        xml.log_event('QUOTE', username, stock_symbol)

        return xml.QuoteResponse(quantity=1, price=quote)
Ejemplo n.º 4
0
    def run(self, username):
        self.session = get_session()
        user = self.session.query(User).filter_by(username=username).first()
        if not user:
            xml.log_error('COMMIT_BUY', 'username not found')
            raise UserNotFoundError(username)
        transaction = self.session.query(Transaction).filter_by(
            username=user.username, operation='BUY', committed=False
        ).first()
        if not transaction:
            xml.log_error('COMMIT_BUY', 'no buy tranaction found')
            raise NoBuyTransactionError(username)

        if (datetime.now() - transaction.creation_time) > config.TRANSACTION_TIMEOUT:
            self.session.delete(transaction)
            self.session.commit()
            raise ExpiredBuyTransactionError(username)

        price = transaction.stock_value * transaction.quantity

        user.account_balance -= price
        transaction.committed = True

        # create or update the StockPurchase for this stock symbol
        stock = self.session.query(StockPurchase).filter_by(
            user=user, stock_symbol=transaction.stock_symbol
        ).first()
        if not stock:
            stock = StockPurchase(user=user,
                    stock_symbol=transaction.stock_symbol,
                    quantity=transaction.quantity)
        else:
            stock.quantity = stock.quantity + transaction.quantity

        self.session.commit()
        xml.log_transaction('COMMIT_BUY', transaction, status_message='success')

        return xml.ResultResponse('success')