Ejemplo n.º 1
0
def tables(request, account_id, space_id):
    account = get_object_or_404(Account, pk=account_id)
    space = get_object_or_404(Space, pk=space_id)
    if check_permission(request, account, space) is False:
        return HttpResponseRedirect("/%s/status/" % (account.id))

    # ---branching for POST vs. GET request
    if request.method == "POST":  # If a form has been submitted...
        # ... and the user has updated the date range on the tables tab
        if "refresh_table_data" in request.POST:
            table_start_date = datetime.strptime(request.POST["table_start_date"], "%m/%d/%Y")
            table_end_date = datetime.strptime(request.POST["table_end_date"], "%m/%d/%Y")
            space_view_url = "/%s/spaces/%s/tables?start=%s&end=%s" % (
                account.id,
                space.id,
                table_start_date.strftime("%m-%d-%Y"),
                table_end_date.strftime("%m-%d-%Y"),
            )
            return HttpResponseRedirect(space_view_url)
        # ... and the user has hit 'export' on the tables tab
        elif "export_tables_tab" in request.POST:
            table_start_date = datetime.strptime(request.POST["table_start_date"], "%m/%d/%Y")
            table_end_date = datetime.strptime(request.POST["table_end_date"], "%m/%d/%Y")

            table_data = get_space_tables_data(space, table_start_date, table_end_date)

            data_out = generate_space_tables_csv(request, space, table_data, table_start_date, table_end_date)
            return CSVResponse(data=data_out, output_name="BuildingSpeak_Report_Space_Tables_Data")

    # If there are dates in the url query string, use them for the tables tab date range, otherwise set defaults
    today = datetime.now()
    three_years_ago = datetime(today.year - 3, today.month, today.day)
    table_start_date = (
        datetime.strptime(request.GET["start"], "%m-%d-%Y") if "start" in request.GET else three_years_ago
    )
    table_end_date = datetime.strptime(request.GET["end"], "%m-%d-%Y") if "end" in request.GET else today

    table_data = get_space_tables_data(space, table_start_date, table_end_date)

    main_page_help_text = "Space Tables - monthly aggregated metrics by utility type."
    tab_help_text = "click to export table data to CSV file"

    context = {
        "sidebar": "buildingspeakapp/shared/account_sidebar.html",
        "tab": "tables",
        "main_page_help_text": main_page_help_text,
        "tab_help_text": tab_help_text,
        "user": request.user,
        "account": account,
        "accounts": request.user.account_set.order_by("id"),
        "buildings": account.building_set.order_by("name"),
        "spaces": Space.objects.filter(Q(building__account=account) | Q(meters__account=account))
        .distinct()
        .order_by("name"),
        "meters": account.meter_set.order_by("name"),
        "equipments": Equipment.objects.filter(Q(buildings__account=account) | Q(meters__account=account))
        .distinct()
        .order_by("name"),
        "measures": EfficiencyMeasure.objects.filter(
            Q(equipments__buildings__account=account) | Q(meters__account=account)
        )
        .distinct()
        .order_by("name"),
        "alerts": space.get_all_alerts(reverse_boolean=True),
        "space": space,
        "table_data": table_data,
        "table_start_date": table_start_date.strftime("%m/%d/%Y"),
        "table_end_date": table_end_date.strftime("%m/%d/%Y"),
    }

    template_name = "buildingspeakapp/spaces/tables.html"
    return render(request, template_name, RequestContext(request, context))