def test_report_ignitions_has_dates(self): url = self.url + "?report=ignitions" req = self.rf.get(url) data = { 'toDate': None, 'fromDate': None } response = self.client.post(url, data=data) form = response.context['form'] assert form.is_valid() eq_(response.context['title'], 'Ignitions summary') eq_(form.cleaned_data.get('toDate'), date.today())
def endorse_authorise_summary(self, request, extra_context=None): """ Display summaries of prescriptions, approvals and ignitions. DEV: """ form = EndorseAuthoriseSummaryForm(request.GET) report_set = {'summary', 'approvals', 'ignitions'} report = request.GET.get('report', 'summary') if report not in report_set: report = 'summary' export_csv = True if request.GET.get('Export_CSV') == 'export_csv' else False if request.GET.get('fromDate'): fromDate = request.GET.get('fromDate') fromDate = datetime.datetime.strptime(fromDate, '%d-%m-%Y').date() else: # default - beginning of financial year yr = datetime.date.today().year fromDate = datetime.date(yr, 7, 1) if request.GET.get('toDate'): toDate = request.GET.get('toDate') toDate = datetime.datetime.strptime(toDate, '%d-%m-%Y').date() else: toDate = datetime.date.today() burns = [] if report == 'summary': title = _("Endorsements summary") queryset = Prescription.objects.filter( endorsement_status=Prescription.ENDORSEMENT_SUBMITTED) elif report == 'approvals': title = _("Approvals summary") queryset = Prescription.objects.filter( approval_status=Prescription.APPROVAL_SUBMITTED) elif report == 'ignitions': title = _("Ignitions summary") queryset = None burns = self.get_burns(fromDate, toDate) else: raise ValueError("Report {} must be in {}".format(report, report_set)) if export_csv: return self.export_to_csv(request, fromDate, toDate, burns) if queryset: queryset.prefetch_related('endorsing_roles') if form.is_valid(): region = form.cleaned_data.get('region', None) district = form.cleaned_data.get('district', None) if region: queryset = queryset.filter(region=region) if district: queryset = queryset.filter(district=district) context = { 'title': title, 'prescriptions': queryset, 'form': form, 'report': report, 'burns': burns, 'fromDate': fromDate, 'toDate': toDate, } context.update(extra_context or {}) return TemplateResponse(request, "admin/endorse_authorise_summary.html", context, current_app=self.name)
def endorse_authorise_summary(self, request, extra_context=None): """ Display summaries of prescriptions, approvals and ignitions. DEV: """ form = EndorseAuthoriseSummaryForm(request.GET) report_set = {'summary', 'approvals', 'ignitions'} report = request.GET.get('report', 'summary') if report not in report_set: report = 'summary' export_csv = True if request.GET.get( 'Export_CSV') == 'export_csv' else False if request.GET.get('fromDate'): fromDate = request.GET.get('fromDate') fromDate = datetime.datetime.strptime(fromDate, '%d-%m-%Y').date() else: # default - beginning of current financial year today = datetime.date.today() financial_year = datetime.date(today.year, 7, 1) if today < financial_year: #today is early than the first day of this year's financial year, set fromDate to last year's financial year fromDate = datetime.date(today.year - 1, 7, 1) else: #today is later than the first day of this year's financial year, set fromDate to this year's financial year fromDate = financial_year if request.GET.get('toDate'): toDate = request.GET.get('toDate') toDate = datetime.datetime.strptime(toDate, '%d-%m-%Y').date() else: toDate = datetime.date.today() burns = [] if report == 'summary': title = _("Endorsements summary") queryset = Prescription.objects.filter( endorsement_status=Prescription.ENDORSEMENT_SUBMITTED) elif report == 'approvals': title = _("Approvals summary") queryset = Prescription.objects.filter( approval_status=Prescription.APPROVAL_SUBMITTED) elif report == 'ignitions': title = _("Ignitions summary") queryset = None burns = self.get_burns(fromDate, toDate) else: raise ValueError("Report {} must be in {}".format( report, report_set)) if export_csv: return self.export_to_csv(request, fromDate, toDate, burns) if queryset: queryset.prefetch_related('endorsing_roles') if form.is_valid(): region = form.cleaned_data.get('region', None) district = form.cleaned_data.get('district', None) if region: queryset = queryset.filter(region=region) if district: queryset = queryset.filter(district=district) context = { 'title': title, 'prescriptions': queryset, 'form': form, 'report': report, 'burns': burns, 'fromDate': fromDate, 'toDate': toDate, } context.update(extra_context or {}) return TemplateResponse(request, "admin/endorse_authorise_summary.html", context, current_app=self.name)