def HandleEntity(self, entity): url = datastore_types.Key.from_path("TallstreetUrls", 'url' + entity['url']) keyword = datastore_types.Key.from_path("TallstreetTags", 'tag' + entity['tag']) user = datastore_types.Key.from_path("User", "user" + entity['username']) portfolio = TallstreetPortfolio(key_name='url%skeyword%s' % (url, keyword)) portfolio.user = user portfolio.url = url portfolio.user = user portfolio.keyword = keyword portfolio.money = entity['money'] newent = datastore.Entity('TallstreetPortfolio', name='url%skeyword%s' % (url, keyword), parent=user) portfolio._to_entity(newent) return newent
def update_portfolio(user, keyword, url, investment, ip): portfolio = TallstreetPortfolio.get_invested(user, url, keyword) if not(portfolio): portfolio = TallstreetPortfolio(parent=user, key_name='url%skeyword%s' % (url.key(), keyword.key())) portfolio.url = url portfolio.keyword = keyword portfolio.user = user portfolio.money = investment if investment == 0: return 0 change = investment else: change = investment - portfolio.money if change == 0: return 0 portfolio.money = investment if user.money_outstanding - change < 0: raise forms.ValidationError("No Money left in account.") user.money_outstanding -= change if investment == 0: portfolio.delete() portfolio = None else: portfolio.put() transaction = TallstreetTransaction(parent=user, change=change, new_amount=investment, account_balance=user.money, account_balance_outstanding=user.money_outstanding, portfolio=portfolio, user=user, ip=ip) if change > 0: transaction.description = "Bought in [%s] under %s" % (url.key().id_or_name(), keyword.tag) else: transaction.description = "Sold in [%s] under %s" % (url.key().id_or_name(), keyword.tag) transaction.put() user.put() return change