def test_sort(self): """Sort dynamic records by different fields.""" a = {'d': 1, 'e': 2} b = {'d': 2, 'e': 1} self.assertEqual([a, a, b], sort_records([a, a, b], 'd', False)) self.assertEqual([b, a, a], sort_records([a, a, b], 'd', True)) self.assertEqual([b, a, a], sort_records([a, a, b], 'e', False)) self.assertEqual([a, a, b], sort_records([a, a, b], 'e', True))
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 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