Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUGFIX] csv export null fk #225

Open
wants to merge 1 commit into
base: field-filter-chain-qualifiers
Choose a base branch
from
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,5 +1,8 @@
## 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 @@ -285,6 +285,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
if not fk_ids:
return

if type(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 @@ -29,6 +29,7 @@ def setUp(self):
animal = Animal(name='test')
animal.save()

self.animal = animal
self.pictures = []

for i in range(3):
Expand Down Expand Up @@ -124,6 +125,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
14 changes: 13 additions & 1 deletion tests/testapp/views/animal.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,22 @@
from binder.views import ModelView

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']
transformed_searches = {'zoo_id': int}

csv_settings = CsvExportView.CsvExportSettings(
withs=['zoo', 'caretaker'],
column_map=[
('id', 'ID'),
('zoo.id', 'Zoo ID'),
('caretaker.id', 'Caretaker ID'),
],
)