def tables(request, account_id): account = get_object_or_404(Account, pk=account_id) if check_permission(request, account) 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') account_view_url = '/%s/tables?start=%s&end=%s' % (account.id, table_start_date.strftime('%m-%d-%Y'), table_end_date.strftime('%m-%d-%Y')) return HttpResponseRedirect(account_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_account_tables_data(account = account, table_start_date = table_start_date, table_end_date = table_end_date) data_out = generate_account_tables_csv(request, account, table_data, table_start_date, table_end_date) return CSVResponse(data = data_out, output_name = 'BuildingSpeak_Report_Account_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_account_tables_data(account = account, table_start_date = table_start_date, table_end_date = table_end_date) main_page_help_text = 'Account 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', 'on_acct_page': True, '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': account.get_all_alerts(reverse_boolean=True), '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/accounts/tables.html' return render(request, template_name, RequestContext(request, context))