def tables(request, account_id, building_id): account = get_object_or_404(Account, pk=account_id) building = get_object_or_404(Building, pk=building_id) if check_permission(request, account, building) 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") building_view_url = "/%s/buildings/%s/tables?start=%s&end=%s" % ( account.id, building.id, table_start_date.strftime("%m-%d-%Y"), table_end_date.strftime("%m-%d-%Y"), ) return HttpResponseRedirect(building_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_building_tables_data(building, table_start_date, table_end_date) data_out = generate_building_tables_csv(request, building, table_data, table_start_date, table_end_date) return CSVResponse(data=data_out, output_name="BuildingSpeak_Report_Building_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_building_tables_data(building, table_start_date, table_end_date) main_page_help_text = "Building 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", "tab_help_text": tab_help_text, "main_page_help_text": main_page_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": building.get_all_alerts(reverse_boolean=True), "building": building, "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/buildings/tables.html" return render(request, template_name, RequestContext(request, context))