Issue 499 - Move id field back to properties and adjust logic to check id field in both root and properties (#515)

* moved id field to root in sample geojson provider files (geopython#499)

* modified geojson data provider to get id from root (geopython#499)

* updated geojson provider tests (geopython#499)

* updated position of id field (geopython#499)

* refactored code (geopython#499)

* refactored geojson provider (geopython#499)
This commit is contained in:
alex-mathew
2020-08-26 03:32:29 +05:30
committed by GitHub
parent 85939109b1
commit 7ddcc9963c
3 changed files with 325 additions and 509 deletions
+23 -13
View File
@@ -64,6 +64,7 @@ class GeoJSONProvider(BaseProvider):
def __init__(self, provider_def):
"""initializer"""
BaseProvider.__init__(self, provider_def)
self.fields = self.get_fields()
@@ -143,10 +144,12 @@ class GeoJSONProvider(BaseProvider):
"""
all_data = self._load()
# if matches
for feature in all_data['features']:
if str(feature[self.id_field]) == identifier:
id = feature.get(self.id_field, None) or\
feature['properties'].get(self.id_field, None)
if id == identifier:
return feature
# default, no match
err = 'item {} not found'.format(identifier)
LOGGER.error(err)
@@ -157,10 +160,12 @@ class GeoJSONProvider(BaseProvider):
:param new_feature: new GeoJSON feature dictionary
"""
all_data = self._load()
# Hijack the feature id and make sure it's unique
new_feature[self.id_field] = str(uuid.uuid4())
if self.id_field not in new_feature and\
self.id_field not in new_feature['properties']:
new_feature['properties'][self.id_field] = str(uuid.uuid4())
all_data['features'].append(new_feature)
@@ -176,11 +181,14 @@ class GeoJSONProvider(BaseProvider):
all_data = self._load()
for i, feature in enumerate(all_data['features']):
if feature[self.id_field] == identifier:
# ensure new_feature retains id
new_feature[self.id_field] = identifier
all_data['features'][i] = new_feature
break
if self.id_field in feature:
if feature[self.id_field] == identifier:
new_feature['properties'][self.id_field] = identifier
all_data['features'][i] = new_feature
elif self.id_field in feature['properties']:
if feature['properties'][self.id_field] == identifier:
new_feature['properties'][self.id_field] = identifier
all_data['features'][i] = new_feature
with open(self.data, 'w') as dst:
dst.write(json.dumps(all_data))
@@ -192,10 +200,12 @@ class GeoJSONProvider(BaseProvider):
all_data = self._load()
for i, feature in enumerate(all_data['features']):
if feature[self.id_field] == identifier:
all_data['features'].pop(i)
break
if self.id_field in feature:
if feature[self.id_field] == identifier:
all_data['features'].pop(i)
elif self.id_field in feature['properties']:
if feature['properties'][self.id_field] == identifier:
all_data['features'].pop(i)
with open(self.data, 'w') as dst:
dst.write(json.dumps(all_data))
File diff suppressed because it is too large Load Diff
-22
View File
@@ -141,28 +141,6 @@ def test_update(fixture, config):
assert 'Null' in results['properties']['name']
def test_update_safe_id(fixture, config):
p = GeoJSONProvider(config)
new_feature = {
'type': 'Feature',
'id': 'SOMETHING DIFFERENT',
'geometry': {
'type': 'Point',
'coordinates': [0.0, 0.0]},
'properties': {
'name': 'Null Island'}}
p.update('123-456', new_feature)
# Don't let the id change, should not exist
with pytest.raises(ProviderItemNotFoundError):
p.get('SOMETHING DIFFERENT')
# Should still be at the old id
results = p.get('123-456')
assert 'Null' in results['properties']['name']
"""
def __init__(self, definition):
BaseProvider.__init__(self, definition)