def test_get_all_indiv_fiscal_reports(self): # just to make sure: no reports are present in the database yet indiv_reports = IndividualFiscalReport.objects.filter(year=self.reference_date.year) self.assertEqual(len(indiv_reports), 0) reports = IndividualFiscalReport.getAllReports(self.reference_date.year) self.assertEqual(len(reports), 2) indiv_reports = IndividualFiscalReport.objects.filter(year=self.reference_date.year) self.assertEqual(len(indiv_reports), 2)
def individual_fiscal_report(request): msg = None if request.method == 'POST': year = request.POST.get('year', '') if year == '': msg = 'Please fill in a year.' else: contributor_id = request.POST.get('contributor', '') reportWriter = ReportWriter() response = HttpResponse(content_type="application/pdf") if contributor_id == '': reports = IndividualFiscalReport.getAllReports(year) if reports == []: msg = 'No contributors made enough donations in {0} for a fiscal report'.format(year) else: response["Content-Disposition"] = "attachment; filename='all_fiscal_reports_{0}.pdf'".format(str(year)) report_values = [IndividualFiscalReport.getValuesForRenderedReport(contributor=report.contributor, year=year) for report in reports] reportWriter.write_all_fiscal_reports(outputobject=response, report_values=report_values) return response else: contributor = Contributor.objects.get(pk=contributor_id) print 'year: {0}, contributor_id: {1}'.format(year, contributor_id) report_values = IndividualFiscalReport.getValuesForRenderedReport(contributor=contributor, year=year) if not report_values: print 'Total amount below minimum fiscal amount' msg = "Contributor {0} did not donate enough in {1} for a fiscal report".format(contributor, year) else: response["Content-Disposition"] = "attachment; filename='fiscal_report_{0}.pdf'".format(report_values['contributor'].encode('utf-8')) reportWriter.write_fiscal_report(outputobject=response, report_values=report_values) return response form = IndividualFiscalReportForm() context_data = { "individual_form": form, "report": "individual fiscal report", "app_label": "simplereports", } if msg: context_data['errors'] = msg context = RequestContext(request, context_data) return render_to_response("individual_fiscal_report.html", context)