def download_dynamic_report(request): """ Download dynamic report as CSV. Query String Parameters: :id: ID of the report to download :values: Fields to include in the resulting CSV, as well as the order in which to include them. :order_by: The sort order for the resulting CSV. Outputs: The report with the specified options rendered as a CSV file. """ # SECURITY: check report_id vs company owner!!! report_id = request.GET.get('id', 0) values = request.GET.getlist('values', None) order_by = request.GET.get('order_by', None) report = get_object_or_404(DynamicReport, pk=report_id) report_configuration = ( report.report_presentation .configuration.build_configuration()) if order_by: report.order_by = order_by report.save() values = report_configuration.get_header() records = [report_configuration.format_record(r) for r in report.python] presentation_driver = ( report.report_presentation.presentation_type.presentation_type) presentation = presentation_drivers[presentation_driver] response = HttpResponse(content_type=presentation.content_type) disposition = get_content_disposition( report.name, presentation.filename_extension) response['Content-Disposition'] = disposition output = StringIO() presentation.write_presentation(values, records, output) response.write(output.getvalue()) return response
def download_dynamic_report(request): """ Download dynamic report in some format. Query String Parameters: :id: ID of the report to export :values: Fields to include in the data, as well as the order in which to include them. :order_by: The the field to sort the records by :direction: 'ascending' or 'decescending', for sort order :report_presentation_id: id of report presentation to use for file format. Outputs: The report with the specified options rendered in the desired format. """ company = get_company_or_404(request) report_id = request.GET.get('id', 0) values = request.GET.getlist('values') order_by = request.GET.get('order_by') order_direction = request.GET.get('direction', 'ascending') report_presentation_id = request.GET.get('report_presentation_id') report = get_object_or_404(DynamicReport.objects.filter(owner=company), pk=report_id) report_presentation = (ReportPresentation.objects.get( id=report_presentation_id)) config = report.report_data.configuration report_configuration = config.build_configuration() if order_by: report.order_by = order_by report.save() if len(values) == 0: values = report_configuration.get_header() records = [ report_configuration.format_record(r, values) for r in report.python ] sorted_records = records if order_by: reverse = False if order_direction == 'descending': reverse = True sorted_records = sort_records(records, order_by, reverse) presentation_driver = ( report_presentation.presentation_type.presentation_type) presentation = presentation_drivers[presentation_driver] response = HttpResponse(content_type=presentation.content_type) disposition = get_content_disposition(report.name, presentation.filename_extension) response['Content-Disposition'] = disposition output = StringIO() values = [ c.alias for value in values for c in report_configuration.columns if c.column == value ] presentation.write_presentation(values, sorted_records, output) response.write(output.getvalue()) return response
def assert_filename(self, expected_filename, report_name): expected_disposition = ( "attachment; filename=%s.zzz" % expected_filename) actual = get_content_disposition(report_name, 'zzz') self.assertEqual(expected_disposition, actual)
def download_dynamic_report(request): """ Download dynamic report in some format. Query String Parameters: :id: ID of the report to export :values: Fields to include in the data, as well as the order in which to include them. :order_by: The the field to sort the records by :direction: 'ascending' or 'decescending', for sort order :report_presentation_id: id of report presentation to use for file format. Outputs: The report with the specified options rendered in the desired format. """ company = get_company_or_404(request) report_id = request.GET.get('id', 0) values = request.GET.getlist('values') order_by = request.GET.get('order_by') order_direction = request.GET.get('direction', 'ascending') report_presentation_id = request.GET.get('report_presentation_id') report = get_object_or_404( DynamicReport.objects.filter(owner=company), pk=report_id) report_presentation = ( ReportPresentation.objects.get(id=report_presentation_id)) config = report.report_data.configuration report_configuration = config.build_configuration() if order_by: report.order_by = order_by report.save() if len(values) == 0: values = report_configuration.get_header() records = [ report_configuration.format_record(r, values) for r in report.python ] sorted_records = records if order_by: reverse = False if order_direction == 'descending': reverse = True sorted_records = sort_records(records, order_by, reverse) presentation_driver = ( report_presentation.presentation_type.presentation_type) presentation = presentation_drivers[presentation_driver] response = HttpResponse(content_type=presentation.content_type) disposition = get_content_disposition( report.name, presentation.filename_extension) response['Content-Disposition'] = disposition output = StringIO() values = [ c.alias for value in values for c in report_configuration.columns if c.column == value ] presentation.write_presentation(values, sorted_records, output) response.write(output.getvalue()) return response