def report_graph_data(request): today = datetime.date.today() report_date = today initial = {'report_year': report_date.year, 'report_month': report_date.month} form = ReportForm(request.GET or None, initial=initial) if form.is_valid(): report_year = form.cleaned_data.get('report_year') or report_date.year report_month = form.cleaned_data.get('report_month') or report_date.month last_day = calendar.monthrange(report_year, report_month)[1] report_date = datetime.date(report_year, report_month, last_day) data = [] start_date = datetime.date(report_date.year, report_date.month, 1) end_date = report_date incoming_count = Message.objects.filter( date__range=(start_date, end_date), direction='I' ).count() outgoing_count = Message.objects.filter( date__range=(start_date, end_date), direction='O' ).count() row = [end_date.isoformat(), incoming_count, outgoing_count] data.append(row) for i in range(1, 7): month = report_date.month - i year = report_date.year if month <= 0: month += 12 year -= 1 last_day = calendar.monthrange(year, month)[1] start_date = datetime.date(year, month, 1) end_date = datetime.date(year, month, last_day) incoming_count = Message.objects.filter( date__range=(start_date, end_date), direction='I' ).count() outgoing_count = Message.objects.filter( date__range=(start_date, end_date), direction='O' ).count() row = [end_date.isoformat(), incoming_count, outgoing_count] data.append(row) return HttpResponse(json.dumps(data), mimetype='application/json')
def dashboard(request): today = datetime.date.today() report_date = today initial = {'report_year': report_date.year, 'report_month': report_date.month} form = ReportForm(request.GET or None, initial=initial) if form.is_valid(): report_year = form.cleaned_data.get('report_year') or report_date.year report_month = form.cleaned_data.get('report_month') or report_date.month last_day = calendar.monthrange(report_year, report_month)[1] report_date = datetime.date(report_year, report_month, last_day) start_date = datetime.date(report_date.year, report_date.month, 1) end_date = report_date context = usage_report_context(start_date, end_date) context['report_date'] = report_date context['report_form'] = form # Identify patients whose wisepill devices' batteries' levels are low, but known context['low_battery_patients'] = Patient.objects.\ filter(batterystrength__lte=WISEPILL_LOW_BATTERY).\ exclude(batterystrength=-1) # Graph data return render_to_response('broadcast/dashboard.html', context, RequestContext(request))