Skip to content

Commit

Permalink
Handle missing model attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
Anthony Lukach committed Mar 27, 2017
1 parent 5548bd0 commit 83fe18d
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 11 deletions.
13 changes: 4 additions & 9 deletions cadasta/organization/download/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 5 additions & 2 deletions cadasta/organization/tests/test_downloads.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down

0 comments on commit 83fe18d

Please sign in to comment.