From 83fe18d55e1949579a57bd3dc07243908efe3480 Mon Sep 17 00:00:00 2001 From: Anthony Lukach Date: Sun, 26 Mar 2017 21:54:09 -0600 Subject: [PATCH] Handle missing model attributes --- cadasta/organization/download/base.py | 13 ++++--------- cadasta/organization/tests/test_downloads.py | 7 +++++-- 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/cadasta/organization/download/base.py b/cadasta/organization/download/base.py index 004a24f80..26b98fddd 100644 --- a/cadasta/organization/download/base.py +++ b/cadasta/organization/download/base.py @@ -17,15 +17,10 @@ def get_schema_attrs(self, content_type): def get_values(self, item, model_attrs, schema_attrs): values = OrderedDict() for attr in model_attrs: - if '.' in attr: - attr_items = attr.split('.') - value = None - for a in attr_items: - value = (getattr(item, a) - if not value else getattr(value, a)) - values[attr] = value - else: - values[attr] = getattr(item, attr) + value = item + for a in attr.split('.'): + value = getattr(value, a, None) + values[attr] = value content_type = ContentType.objects.get_for_model(item) conditional_selector = self.get_conditional_selector(content_type) diff --git a/cadasta/organization/tests/test_downloads.py b/cadasta/organization/tests/test_downloads.py index d755b0fde..c5f7504d6 100644 --- a/cadasta/organization/tests/test_downloads.py +++ b/cadasta/organization/tests/test_downloads.py @@ -110,14 +110,17 @@ def test_get_values(self): 'key_2': ['choice_1', 'choice_2']}) model_attrs = ('id', 'party_id', 'spatial_unit_id', - 'tenure_type.label') + 'tenure_type.label', 'missing_attr', + 'spatial_unit_id.missing_nested') schema_attrs = exporter.get_schema_attrs(content_type) values = exporter.get_values(item, model_attrs, schema_attrs) assert values == { 'id': item.id, 'party_id': item.party_id, 'spatial_unit_id': item.spatial_unit_id, 'tenure_type.label': 'Leasehold', - 'key': 'text', 'key_2': 'choice_1, choice_2'} + 'key': 'text', 'key_2': 'choice_1, choice_2', + 'missing_attr': None, 'spatial_unit_id.missing_nested': None + } def test_get_values_with_conditional_selector(self): project = ProjectFactory.create(current_questionnaire='123abc')