Esempio n. 1
0
def api_punchcard(request):
    """
    Used by the Github style punchcard graph on the index page
    """
    def parse(x):
        return {'y': x[0] + 1, 'x': 3600000  * x[1], 'amount': x[2],
                'format': x[4], 'marker': {'radius': x[3]}}

    begin = {'x': 0, 'y': 0, 'amount': 0, 'marker': {'radius': 0}}
    end = {'x': 0, 'y': 8, 'amount': 0, 'marker': {'radius': 0}}
    start_date, end_date = get_timerange(request)
    return [begin] + map(parse, drink_log.punchcard(start_date = start_date, end_date = end_date)) + [end]
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,
    }