def test_months_without_date(self): "If months is given with no date then use today as the day." form = GraphRangeForm({'months': 6}) self.assertTrue(form.is_valid()) data = form.cleaned_data self.assertEqual(data['months'], 6) self.assertEqual(data['start_date'], datetime.date.today())
def test_date_without_months(self): "If date is given with no months then use a default 8 months." form = GraphRangeForm({'start_date': '2011-06-28'}) self.assertTrue(form.is_valid()) data = form.cleaned_data self.assertEqual(data['months'], 8) self.assertEqual(data['start_date'], datetime.date(2011, 6, 28))
def system_usage(request): "Return JSON data for overall system usage either for a date range or to date." # Start date/months or to date range_form = GraphRangeForm(request.GET) start_date = None months = None data = {} if range_form.is_valid(): start_date = range_form.cleaned_data.get("start_date", None) months = range_form.cleaned_data.get("months", None) else: return HttpResponseBadRequest(json.dumps({"error": "Invalid report range"}), mimetype="application/json") if start_date is not None and months is not None: # Generate report for each month requested rows = [] day = start_date for i in range(months + 1): day = start_date - relativedelta(months=i) stats = {"appointments": appointment_stats(day=day), "broadcasts": broadcast_stats(day=day)} rows.append((day.isoformat(), stats)) data["range"] = rows else: # Report data to date data["to_date"] = {"appointments": appointment_stats(), "broadcasts": broadcast_stats()} return HttpResponse(json.dumps(data), mimetype="application/json")
def test_empty_form(self): "Nothing is required so an empty form is valid." form = GraphRangeForm({}) self.assertTrue(form.is_valid())