예제 #1
0
    def get(self, request, *args, **kwargs):
        form_class = self.get_form_class()
        form = self.get_form(form_class)
        sqs = form.search()

        # Filter a queryset by pk
        result_ids = list(r.pk for r in sqs)
        queryset = Dataset.objects.filter(id__in=result_ids)

        # Use the idx feature of postgres' intarray extension to order the same as the search order
        sql_id_array = "'{{{}}}'::int[]".format(",".join(result_ids))
        sql_order_expression = '(idx({}, id))'.format(sql_id_array)
        queryset = queryset.extra(select={'ordering': sql_order_expression})
        queryset = queryset.extra(order_by=['ordering'])

        output_fieldnames = [
            f.name for f in Dataset._meta.get_fields() if f.name != 'id'
        ]
        csv_data = generate_csv(queryset, output_fieldnames)

        response = StreamingHttpResponse(csv_data, content_type="text/csv")
        response[
            'Content-Disposition'] = 'attachment; filename="criminal-justice-{}-rows.csv"'.format(
                sqs.count())
        return response
예제 #2
0
    def get(self, request, *args, **kwargs):
        concrete_model = self.model._meta.concrete_model
        output_fieldnames = [f.name for f in concrete_model._meta.get_fields() if f.name != 'id']
        qs = self.get_queryset()

        csv_data = generate_csv(qs, output_fieldnames)

        response = StreamingHttpResponse(csv_data, content_type="text/csv")
        response['Content-Disposition'] = 'attachment; filename="criminal-justice-{}-rows.csv"'.format(qs.count())
        return response
예제 #3
0
    def get(self, request, *args, **kwargs):
        concrete_model = self.model._meta.concrete_model
        output_fieldnames = [
            f.name for f in concrete_model._meta.get_fields() if f.name != 'id'
        ]
        qs = self.get_queryset()

        csv_data = generate_csv(qs, output_fieldnames)

        response = StreamingHttpResponse(csv_data, content_type="text/csv")
        response[
            'Content-Disposition'] = 'attachment; filename="criminal-justice-{}-rows.csv"'.format(
                qs.count())
        return response
예제 #4
0
    def get(self, request, *args, **kwargs):
        form_class = self.get_form_class()
        form = self.get_form(form_class)
        sqs = form.search()

        # Filter a queryset by pk
        result_ids = list(r.pk for r in sqs)
        queryset = Dataset.objects.filter(id__in=result_ids)

        # Use the idx feature of postgres' intarray extension to order the same as the search order
        sql_id_array = "'{{{}}}'::int[]".format(",".join(result_ids))
        sql_order_expression = '(idx({}, id))'.format(sql_id_array)
        queryset = queryset.extra(select={'ordering': sql_order_expression})
        queryset = queryset.extra(order_by=['ordering'])

        output_fieldnames = [f.name for f in Dataset._meta.get_fields() if f.name != 'id']
        csv_data = generate_csv(queryset, output_fieldnames)

        response = StreamingHttpResponse(csv_data, content_type="text/csv")
        response['Content-Disposition'] = 'attachment; filename="criminal-justice-{}-rows.csv"'.format(sqs.count())
        return response