def buy_stock(self, tag, qty): if tag.available >= qty: price = tag.price * qty if price <= self.cash: stock = self.stock_set.filter("tag =", tag).get() if stock: stock.quantity += qty stock.purchase_price = max(stock.purchase_price, tag.price) stock.put() else: stock = Stock(user=self, tag=tag, quantity=qty, purchase_price=tag.price) msg = Message.create_message_without_put(self, "Purchased %d of %s for %s (%s per stock)" % (qty, tag.name, currency(price), currency(tag.price))) self.cash -= price tag.available -= qty db.put([stock, self, tag, msg]) return True return False
def pay_dividend(self, stock, paid_dividend): msg = Message.create_message_without_put(self, 'Received dividend of %s for owning %d of %s' % (currency(paid_dividend), stock.quantity, stock.tag.name)) self.cash += paid_dividend db.put([self, msg])
def pay_for_offer(self, offer, qty, price): logging.info("Offers found, now fulfilling each offer") stock = self.stock_set.filter('tag =', offer.tag).get() logging.info("We have a stock %s" % (stock)) if stock and stock.quantity >= qty: total = qty * price msg = Message.create_message_without_put(self, 'Offer accepted for %d of %s at %s' % (qty, offer.tag.name, currency(total))) self.cash += total stock.quantity -= qty stock.on_offer -= qty db.put([self, msg, stock]) return True return False