Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@

## HEAD

### BUGFIX csv export with null foreign keys
CSV export will now work with null foreign keys

### Chained qualifiers
Chained qualifiers have been added [T35707](https://phabricator.codeyellow.nl/T35707). For more information on how they work and how to use it see [documentation](/docs/api.md)

Expand Down
5 changes: 5 additions & 0 deletions binder/plugins/views/csvexport.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,11 @@ def get_datum(data, key, prefix=''):
else:
# Assume that we have a mapping now
fk_ids = data[head_key]

# If foreign key is null don't try to fetch the data and stop
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if not fk_ids:
return

if not isinstance(fk_ids, list):
fk_ids = [fk_ids]

Expand Down
14 changes: 14 additions & 0 deletions tests/plugins/test_csvexport.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ def setUp(self):
animal = Animal(name='test')
animal.save()

self.animal = animal
self.pictures = []

for i in range(3):
Expand Down Expand Up @@ -125,6 +126,19 @@ def test_download_extra_params(self):
self.assertEqual(data[2], [str(caretaker_2.id), 'Bar', 'boo!'])
self.assertEqual(data[3], [str(caretaker_3.id), 'Baz', 'boo!'])

def test_null_foreign_keys_will_not_fail(self):
response = self.client.get('/animal/download/')
self.assertEqual(200, response.status_code)
response_data = csv.reader(io.StringIO(response.content.decode("utf-8")))

data = list(response_data)

# First line needs to be the header
self.assertEqual(data[0], ['ID', 'Zoo ID', 'Caretaker ID'])

# All other data needs to be ordered using the default ordering (by id, asc)
self.assertEqual(data[1], [str(self.animal.id), '', ''])

def test_context_aware_download_xlsx(self):
response = self.client.get('/picture/download/?response_type=xlsx')
self.assertEqual(200, response.status_code)
Expand Down
13 changes: 12 additions & 1 deletion tests/testapp/views/animal.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
from binder.views import ModelView, Stat

from ..models import Animal
from binder.plugins.views import CsvExportView

# From the api docs
class AnimalView(ModelView):


class AnimalView(ModelView, CsvExportView):
model = Animal
m2m_fields = ['costume']
searches = ['name__icontains']
Expand All @@ -21,3 +24,11 @@ class AnimalView(ModelView):
group_by='zoo.name',
),
}
csv_settings = CsvExportView.CsvExportSettings(
withs=['zoo', 'caretaker'],
column_map=[
('id', 'ID'),
('zoo.id', 'Zoo ID'),
('caretaker.id', 'Caretaker ID'),
],
)
Loading