示例#1
0
文件: views.py 项目: stall/tallstreet
def get_local_results(offset, size, category, user):
    localresult = TallstreetUniverse.get_results(offset, size, category)
    results = []
    for result in localresult:
        result.urlkey = result._url.id_or_name()
        try:
            if not cache.get("url:" + urlquote(result._url.id_or_name())):
                url = result.url
        except:
            logging.error("No url")
            continue
        i = 0
        related_keywords_display = []
        for related_keywords in result.url.related_keywords:
            if i > 5:
                break
            i += 1
            related_keywords_display.append(related_keywords)

        result.url.related_keywords_display = related_keywords_display
        if user and user.is_authenticated():
            portfolio = TallstreetPortfolio.get_invested(user, result._url, category)
            if portfolio:
                result.money_invested = portfolio.money
        results.append(result)

    return results
示例#2
0
文件: feeds.py 项目: stall/tallstreet
	def items(self, obj):
		ratings = []
		historychanges =  TallstreetHistoryChanges.get_all()
		for historychange in historychanges:
			history = TallstreetHistory.get_or_insert(historychange.key().id_or_name())
			
			if not history:
				historychange.delete()
				continue
			try:
				universe = history.universe
			except:
				historychange.delete()
				continue
			logging.debug(history.universe.money)
			history.universe.money += long(round(history.universe.money * (1.0 * history.changes[0] / 100)))
			
			investors = TallstreetPortfolio.get_investors(history.universe.url, history.universe.tag)
			
			logging.debug(history.universe.url.url)
			logging.debug(history.universe.money)
			history.universe.put()
			logging.debug(history.changes)
			for investor in investors:
				update_portfolio_gain(investor.parent(), history.universe.url, history.universe.tag, history.changes[0])
				
			historychange.delete()
		users =  User.need_new_money()		
		for user in users:
			update_new_money(user)
			
		return []
示例#3
0
def dequeuehistory(request):
	date = datetime.datetime(datetime.datetime.today().year, datetime.datetime.today().month, datetime.datetime.today().day)
	
	historychanges = TallstreetHistoryChanges.get_or_insert(request.POST['historychanges'])
	history = TallstreetHistory.get_or_insert(request.POST['historychanges'])
	
	if not history:
		historychanges.delete()
		return HttpResponse("Can't Find History", mimetype="text/plain")
	if date in history.dates:
		historychanges.delete()
		return HttpResponse("Already Done", mimetype="text/plain")
	
	universe = TallstreetUniverse.get_by_key_name(request.POST['universe'])
	history.universe = universe
	history.changes.insert(0, long(historychanges.change))
	history.dates.insert(0, date)
	history.put()
	
	logging.debug(history.universe.money)
	history.universe.money += long(round(history.universe.money * (1.0 * history.changes[0] / 100)))
	
	investors = TallstreetPortfolio.get_investors(history.universe.url, history.universe.tag)
	
	logging.debug(history.universe.url.url)
	logging.debug(history.universe.money)
	history.universe.put()
	logging.debug(history.changes)
	for investor in investors:
		update_portfolio_gain(investor.parent(), history.universe.url, history.universe.tag, history.changes[0])
		
	historychanges.delete()
	
	logging.debug("Success")
	return HttpResponse("Success", mimetype="text/plain")
示例#4
0
  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  
示例#5
0
def portfolio(request, category):
	payload = {}
	results = TallstreetPortfolio.all().ancestor(request.user).order('url').fetch(1000)
	payload["results"] = []
	for result in results:
		result.urlkey = result._url.id_or_name()
		try:
			url = result.url
		except:
			logging.error("No url" )
			continue	
		payload["results"].append(result)
	return render("portfolio.html", payload, request)
示例#6
0
def update_portfolio_gain(user, url, keyword, gain):
	portfolio = TallstreetPortfolio.get_invested(user, url, keyword)
	change = long(round(portfolio.money * (1.0 * gain / 100)))

	if change == 0:
		return 0	
	portfolio.money = portfolio.money + change
		
	user.money += change
	if portfolio.money <= 0:
		portfolio.delete()
		portfolio = None
	else:
		portfolio.put()
	transaction = TallstreetTransaction(parent=user, change=change, new_amount=portfolio.money, account_balance=user.money, account_balance_outstanding=user.money_outstanding, portfolio=portfolio, user=user)
	if change > 0:
		transaction.description = "Gained in [%s] under %s" % (url.key().id_or_name(), keyword.tag)
	else:
		transaction.description = "Lost in [%s] under %s" % (url.key().id_or_name(), keyword.tag)
	transaction.put()
	
	user.put()
	return change	
示例#7
0
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
示例#8
0
			
		for keyword in dburl.related_keywords:
			payload["tags"][keyword.tag.tag] = min(keyword.money / 1000 + 10, 30)
	else:
		page = fetch(url)
		soup = BeautifulSoup(page.content)
		payload["title"] = soup.html.head.title.string
		desc = soup.find("meta", {"name": "description"})
		if desc:
			payload["description"] = desc["content"]
		payload["url"] = url
		payload["new"] = True
		
	
	if keywords == []:
		invested = TallstreetPortfolio.get_keywords(request.user, dburl)
		for keyword in invested:
			if payload["tags"].has_key(keyword.keyword.tag):
				del payload["tags"][keyword.keyword.tag]	
			if keyword.keyword.tag == new_keyword:
				new_keyword = ""
			keywords.append({"keyword": keyword.keyword.tag, "amount": keyword.money, 'edit': False})
			payload["base_balance"] -= keyword.money
			payload["this_investment"] += keyword.money
		if payload["tags"].has_key(new_keyword):
			del payload["tags"][new_keyword]			
		keywords.append({"keyword": new_keyword, "amount": "0", 'edit': True})
	payload["keywords"] = keywords
	
	return render("invest.html", payload, request)