def dashboard(request): "Reporting dashboard." 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) context = {} to_date = { "messages": messages_by_direction(), "users": user_stats(), "appointments": appointment_stats(), "reminders": reminder_stats(), } patients = Contact.objects.filter(patient__isnull=False) staff = Contact.objects.filter(patient__isnull=True) this_month = { "messages": messages_by_direction(day=report_date), "users": user_stats(day=report_date), "patient_messages": messages_by_direction(day=report_date, filters={"contact__in": patients}), "staff_messages": messages_by_direction(day=report_date, filters={"contact__in": staff}), "other_messages": messages_by_direction(day=report_date, filters={"contact__isnull": True}), "appointments": appointment_stats(day=report_date), "reminders": reminder_stats(day=report_date), "broadcasts": broadcast_stats(day=report_date), } prev_month = report_date - datetime.timedelta(days=report_date.day + 1) last_month = { "appointments": appointment_stats(day=prev_month), "reminders": reminder_stats(day=prev_month), "broadcasts": broadcast_stats(day=prev_month), "users": user_stats(day=prev_month), } context["to_date"] = to_date context["this_month"] = this_month context["last_month"] = last_month context["report_date"] = report_date context["report_form"] = form return render(request, "reports/dashboard.html", context)
def test_max_year(self): "Cannot select a year greater than the current year." form = ReportForm({'report_year': datetime.date.today().year + 1}) self.assertFalse(form.is_valid())
def test_empty_form(self): "Nothing is required so an empty form is valid." form = ReportForm({}) self.assertTrue(form.is_valid())