Skip to content

Commit

Permalink
Merge pull request #62 from azavea/topic/support_aggregates
Browse files Browse the repository at this point in the history
Topic/support aggregates
  • Loading branch information
Steve Lamb committed Oct 15, 2014
2 parents 3ac10ab + 9ba2e85 commit 2304dcf
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 8 deletions.
2 changes: 1 addition & 1 deletion djqscsv/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
from djqscsv import (render_to_csv_response, write_csv, # NOQA
generate_filename, CSVException) # NOQA
generate_filename, CSVException) # NOQA
20 changes: 15 additions & 5 deletions djqscsv/_csql.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,19 @@
This module may later be officially supported.
"""

def _identity(x):
return x

def _transform(dataset, arg):
if isinstance(arg, str):
return (dataset[0].index(arg), arg)
elif isinstance(arg, tuple):
return (dataset[0].index(arg[0]), arg[1])
field = arg
display_name = arg
transformer = _identity
else:
field, display_name, transformer = arg
if field is None:
field = dataset[0][0]
return (dataset[0].index(field), display_name, transformer)


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

# add the rest of the rows
results += [[datarow[i] for i, h in index_headers]
results += [[trans(datarow[i]) for i, h, trans in index_headers]
for datarow in dataset[1:]]
return results

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


def CONSTANT(value, display_name):
return (None, display_name, lambda x: value)

def AS(field, display_name):
return (field, display_name)
return (field, display_name, _identity)
4 changes: 4 additions & 0 deletions djqscsv/djqscsv.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ def write_csv(queryset, file_obj, **kwargs):
if extra_columns:
field_names += extra_columns

aggregate_columns = list(values_qs.query.aggregate_select)
if aggregate_columns:
field_names += aggregate_columns

if field_order:
# go through the field_names and put the ones
# that appear in the ordering list first
Expand Down
2 changes: 1 addition & 1 deletion test_app/djqscsv_tests/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@

import djqscsv.djqscsv as djqscsv # NOQA

from djqscsv._csql import SELECT, EXCLUDE, AS
from djqscsv._csql import SELECT, EXCLUDE, AS, CONSTANT
19 changes: 18 additions & 1 deletion test_app/djqscsv_tests/tests/test_csv_creation.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from django.db.models import Count
from django.test import TestCase
from django.core.exceptions import ValidationError

Expand All @@ -8,7 +9,7 @@

from djqscsv_tests.context import djqscsv

from djqscsv_tests.context import SELECT, EXCLUDE, AS
from djqscsv_tests.context import SELECT, EXCLUDE, AS, CONSTANT

from djqscsv_tests.models import Person

Expand Down Expand Up @@ -213,6 +214,22 @@ def test_no_values_matches_models_file(self):
use_verbose_names=False)


class AggregateTests(CSVTestCase):

def setUp(self):
self.qs = create_people_and_get_queryset().annotate(num_hobbies=Count('hobby'))

def test_aggregate(self):
csv_with_aggregate = SELECT(self.FULL_PERSON_CSV,
'ID',
"Person's name",
'address',
"Info on Person",
'hobby_id',
CONSTANT('1', 'num_hobbies'))
self.assertQuerySetBecomesCsv(self.qs, csv_with_aggregate)


class ExtraOrderingTests(CSVTestCase):

def setUp(self):
Expand Down

0 comments on commit 2304dcf

Please sign in to comment.