def api_top_drops(request): """ Used by the pie graph on the index page """ def parse(x): return {'name': x[1], 'y': x[0], 'url': '/drink/%s' % x[2]} if 'machine_id' in request.params: return map(parse, drink_log.top_drinks_for_machine(request.params['machine_id'], 15)) elif 'username' in request.params: return map(parse, drink_log.top_drinks(15, request.params['username'])) else: start_date, end_date = get_timerange(request) return map(parse, drink_log.top_drinks(15, start_date = start_date, end_date = end_date))
def users_view(request): def parse(x): return [x[0].strftime('%b, %d, %Y')] + [x[1]] + [x[2]] + [x[3]] + [x[4]] username = request.matchdict['username'] drop_count = drink_log.get_drop_count(username) if drop_count: return {'username': username, 'hours': drink_log.top_hours(username), 'top_drinks': drink_log.top_drinks(15, username), 'drop_count': drop_count, 'error': False, 'latest_drops': map(parse, drink_log.get_latest_drops(username)), 'money': money_log.money_spent(username)} else: return {'message': 'There is no information about ' + username, 'username': '******', 'error': True}
def users_view(request): username = request.matchdict["username"] drop_count = drink_log.get_drop_count(username) if drop_count: return { "username": username, "hours": drink_log.top_hours(username), "top_drinks": drink_log.top_drinks(15, username), "drop_count": drop_count, "error": False, "latest_drops": drink_log.get_latest_drops(username), "money": money_log.money_spent(username), } else: return {"message": "There is no information about " + username, "error": True}
def index_view(request): start_date = end_date = error = None if request.method == "POST": if ("cal_year", u"Calendar Year") in request.POST.items(): end_date = datetime.datetime.now() start_date = datetime.datetime(year=end_date.year, month=1, day=1) elif ("school_year", u"School Year ") in request.POST.items(): end_date = datetime.datetime.now() if end_date.month >= 8: start_date = datetime.datetime(year=end_date.year, month=8, day=15) else: start_date = datetime.datetime(year=end_date.year - 1, month=8, day=15) elif ("last_month", u" Last Month ") in request.POST.items(): end_date = datetime.datetime.now() start_date = end_date - datetime.timedelta(days=30) elif ("ever", u" Ever ") in request.POST.items(): start_date = datetime.datetime(year=2011, month=10, day=17) end_date = datetime.datetime.now() else: start_date = datetime.datetime.strptime(request.POST.get("start"), "%m/%d/%Y") end_date = datetime.datetime.strptime(request.POST.get("end"), "%m/%d/%Y") if not start_date or not end_date or start_date > end_date: start_date = datetime.datetime(year=2011, month=10, day=17) end_date = datetime.datetime.now() top_drinks = drink_log.top_drinks(15, start_date=start_date, end_date=end_date) top_spenders = money_log.top_spenders(15, start_date=start_date, end_date=end_date) hours = drink_log.top_hours(start_date=start_date, end_date=end_date) punchcard = drink_log.punchcard(start_date=start_date, end_date=end_date) start_date_format = start_date.strftime("%m/%d/%Y") end_date_format = end_date.strftime("%m/%d/%Y") return { "top_drinks": top_drinks, "hours": hours, "top_users": top_spenders, "start_date": start_date_format, "end_date": end_date_format, "error": error, "new_hours": punchcard, }