Skip to content

Commit 2304dcf

Browse files
author
Steve Lamb
committed
Merge pull request #62 from azavea/topic/support_aggregates
Topic/support aggregates
2 parents 3ac10ab + 9ba2e85 commit 2304dcf

File tree

5 files changed

+39
-8
lines changed

5 files changed

+39
-8
lines changed

djqscsv/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
from djqscsv import (render_to_csv_response, write_csv, # NOQA
2-
generate_filename, CSVException) # NOQA
2+
generate_filename, CSVException) # NOQA

djqscsv/_csql.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,19 @@
66
This module may later be officially supported.
77
"""
88

9+
def _identity(x):
10+
return x
911

1012
def _transform(dataset, arg):
1113
if isinstance(arg, str):
12-
return (dataset[0].index(arg), arg)
13-
elif isinstance(arg, tuple):
14-
return (dataset[0].index(arg[0]), arg[1])
14+
field = arg
15+
display_name = arg
16+
transformer = _identity
17+
else:
18+
field, display_name, transformer = arg
19+
if field is None:
20+
field = dataset[0][0]
21+
return (dataset[0].index(field), display_name, transformer)
1522

1623

1724
def SELECT(dataset, *args):
@@ -23,7 +30,7 @@ def SELECT(dataset, *args):
2330
results += [[header[1] for header in index_headers]]
2431

2532
# add the rest of the rows
26-
results += [[datarow[i] for i, h in index_headers]
33+
results += [[trans(datarow[i]) for i, h, trans in index_headers]
2734
for datarow in dataset[1:]]
2835
return results
2936

@@ -34,5 +41,8 @@ def EXCLUDE(dataset, *args):
3441
return SELECT(dataset, *antiargs)
3542

3643

44+
def CONSTANT(value, display_name):
45+
return (None, display_name, lambda x: value)
46+
3747
def AS(field, display_name):
38-
return (field, display_name)
48+
return (field, display_name, _identity)

djqscsv/djqscsv.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,10 @@ def write_csv(queryset, file_obj, **kwargs):
9090
if extra_columns:
9191
field_names += extra_columns
9292

93+
aggregate_columns = list(values_qs.query.aggregate_select)
94+
if aggregate_columns:
95+
field_names += aggregate_columns
96+
9397
if field_order:
9498
# go through the field_names and put the ones
9599
# that appear in the ordering list first

test_app/djqscsv_tests/context.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@
66

77
import djqscsv.djqscsv as djqscsv # NOQA
88

9-
from djqscsv._csql import SELECT, EXCLUDE, AS
9+
from djqscsv._csql import SELECT, EXCLUDE, AS, CONSTANT

test_app/djqscsv_tests/tests/test_csv_creation.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from django.db.models import Count
12
from django.test import TestCase
23
from django.core.exceptions import ValidationError
34

@@ -8,7 +9,7 @@
89

910
from djqscsv_tests.context import djqscsv
1011

11-
from djqscsv_tests.context import SELECT, EXCLUDE, AS
12+
from djqscsv_tests.context import SELECT, EXCLUDE, AS, CONSTANT
1213

1314
from djqscsv_tests.models import Person
1415

@@ -213,6 +214,22 @@ def test_no_values_matches_models_file(self):
213214
use_verbose_names=False)
214215

215216

217+
class AggregateTests(CSVTestCase):
218+
219+
def setUp(self):
220+
self.qs = create_people_and_get_queryset().annotate(num_hobbies=Count('hobby'))
221+
222+
def test_aggregate(self):
223+
csv_with_aggregate = SELECT(self.FULL_PERSON_CSV,
224+
'ID',
225+
"Person's name",
226+
'address',
227+
"Info on Person",
228+
'hobby_id',
229+
CONSTANT('1', 'num_hobbies'))
230+
self.assertQuerySetBecomesCsv(self.qs, csv_with_aggregate)
231+
232+
216233
class ExtraOrderingTests(CSVTestCase):
217234

218235
def setUp(self):

0 commit comments

Comments
 (0)