Skip to content

Commit

Permalink
Add update to commit response (#991)
Browse files Browse the repository at this point in the history
* Add 'update' section to commit response

* Update tests

* ⬆️ Client
  g3w-suite/g3w-client@25934c8
  g3w-suite/g3w-client-plugin-editing@2997529

* Add testing for 'update' section

* ⬆️ Client
  g3w-suite/g3w-client@06b4184

* ⬆️ Client
  g3w-suite/g3w-client@52329a4

* ⬆️ Client
  g3w-suite/g3w-client-plugin-editing@127b1c6

* ⬆️ Client
  g3w-suite/g3w-client-plugin-editing@2290dee

* Change media into feature properties to object with mimetype.

* ⬆️ Client
  g3w-suite/g3w-client@edeab99

* ⬆️ Client
  g3w-suite/g3w-client@e947913

* ⬆️ Client
  g3w-suite/g3w-client@e5ac297
  g3w-suite/g3w-client-plugin-editing#141

* ⬆️ Client
  g3w-suite/g3w-client@5d97bd9

* ⬆️ Client
  g3w-suite/g3w-client@cdd4c3a

* ⬆️ Client
  g3w-suite/g3w-client@0bdc178

* ⬆️ Client
  g3w-suite/g3w-client@9ae00cb

* ⬆️ Client
  g3w-suite/g3w-client@d3b4d26

* ⬆️ Client
  g3w-suite/g3w-client@614474e

* ⬆️ Client
  g3w-suite/g3w-client@614474e
  g3w-suite/g3w-client-plugin-editing@370fdbf

---------

Co-authored-by: wlorenzetti <[email protected]>
Co-authored-by: volterra79 <[email protected]>
  • Loading branch information
3 people authored Dec 23, 2024
1 parent 4af4f36 commit 6647b74
Show file tree
Hide file tree
Showing 9 changed files with 152 additions and 122 deletions.
2 changes: 1 addition & 1 deletion g3w-admin/client/static/client/app.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion g3w-admin/client/static/client/app.min.js.map

Large diffs are not rendered by default.

160 changes: 80 additions & 80 deletions g3w-admin/client/static/client/vendor.min.js

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions g3w-admin/client/static/client/vendor.min.js.map

Large diffs are not rendered by default.

61 changes: 40 additions & 21 deletions g3w-admin/editing/api/base/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ def save_vector_data(self, metadata_layer, post_layer_data, has_transactions, po
# data for response
insert_ids = list()
lock_ids = list()
update_ids = list()

# FIXME: check this out
# for add check if is a metadata_layer and referenced field is a pk
Expand Down Expand Up @@ -346,6 +347,10 @@ def save_vector_data(self, metadata_layer, post_layer_data, has_transactions, po
if errors:
raise ValidationError(errors)

to_res = {}
to_res_lock = {}
to_res_update = {}

# Save the feature
if mode_editing == EDITING_POST_DATA_ADDED:
if has_transactions:
Expand Down Expand Up @@ -398,10 +403,9 @@ def save_vector_data(self, metadata_layer, post_layer_data, has_transactions, po
raise Exception(_('Error changing geometry: %s') % ', '.join(
qgis_layer.dataProvider().errors()))

to_res = {}
to_res_lock = {}

if mode_editing == EDITING_POST_DATA_ADDED:

if mode_editing in (EDITING_POST_DATA_ADDED, EDITING_POST_DATA_UPDATED):

# to exclude QgsFormater used into QgsJsonjExporter is necessary build by hand single json feature
ex = QgsJsonExporter(qgis_layer)
Expand All @@ -410,6 +414,8 @@ def save_vector_data(self, metadata_layer, post_layer_data, has_transactions, po
fnames = [f.name() for f in feature.fields()]
jfeature = json.loads(ex.exportFeature(feature, dict(zip(fnames, feature.attributes()))))

self.change_media([jfeature])

# For date and datetime and time fields:
if field_datetime_values:
for fname, fvalue in jfeature['properties'].items():
Expand All @@ -419,24 +425,34 @@ def save_vector_data(self, metadata_layer, post_layer_data, has_transactions, po
except:
pass

if mode_editing == EDITING_POST_DATA_ADDED:
to_res.update({
'clientid': geojson_feature['id'],
# This might be the internal QGIS feature id (< 0)
'id': server_fid(feature, metadata_layer.qgis_layer.dataProvider()),
'properties': jfeature['properties']
})

# Locking features new:
to_res_lock = metadata_layer.lock.modelLock2dict(
metadata_layer.lock.lockFeature(
str(server_fid(feature, metadata_layer.qgis_layer.dataProvider())), save=True)
)
else:
to_res_update.update({
# This might be the internal QGIS feature id (< 0)
'id': server_fid(feature, metadata_layer.qgis_layer.dataProvider()),
'properties': jfeature['properties']
})

to_res.update({
'clientid': geojson_feature['id'],
# This might be the internal QGIS feature id (< 0)
'id': server_fid(feature, metadata_layer.qgis_layer.dataProvider()),
'properties': jfeature['properties']
})

# lock news:
to_res_lock = metadata_layer.lock.modelLock2dict(
metadata_layer.lock.lockFeature(
str(server_fid(feature, metadata_layer.qgis_layer.dataProvider())), save=True)
)

if bool(to_res):
insert_ids.append(to_res)
if bool(to_res_lock):
lock_ids.append(to_res_lock)
if bool(to_res_update):
update_ids.append(to_res_update)

# Send post vase signal
post_save_maplayer.send(
Expand Down Expand Up @@ -502,7 +518,7 @@ def save_vector_data(self, metadata_layer, post_layer_data, has_transactions, po
raise Exception(_('Cannot delete feature: %s') %
', '.join(qgis_layer.dataProvider().errors()))

return insert_ids, lock_ids
return insert_ids, lock_ids, update_ids

def response_commit_mode(self, request):
"""
Expand All @@ -516,7 +532,7 @@ def response_commit_mode(self, request):
# post_layer_data = data[self.layer_name]
post_layer_data = request.data

new_relations = dict()
relations = dict()

# Store references to all layers that have been made editable,
# used to commit/rollback at the end of the loop and on errors
Expand Down Expand Up @@ -547,7 +563,7 @@ def response_commit_mode(self, request):


# Save main layer
ref_insert_ids, ref_lock_ids = self.save_vector_data(
ref_insert_ids, ref_lock_ids, ref_update_ids = self.save_vector_data(
self.metadata_layer, post_layer_data, has_transactions, reproject=self.reproject)

# Save relations
Expand Down Expand Up @@ -577,13 +593,14 @@ def save_relation(post_relation_data, referencing_layer, ref_insert_ids) -> None
reproject = not self.layer.project.group.srid.auth_srid == self.metadata_relations[
referencing_layer].layer.srid

insert_ids, lock_ids = self.save_vector_data(self.metadata_relations[referencing_layer],
insert_ids, lock_ids, update_ids = self.save_vector_data(self.metadata_relations[referencing_layer],
post_relation_data, has_transactions,
referenced_layer_insert_ids=ref_insert_ids,
reproject=reproject)
new_relations[referencing_layer] = {
relations[referencing_layer] = {
'new': insert_ids,
'new_lockids': lock_ids
'new_lockids': lock_ids,
'update': update_ids
}

# Check for cascading relations
Expand Down Expand Up @@ -647,7 +664,9 @@ def save_relation(post_relation_data, referencing_layer, ref_insert_ids) -> None
'response': {
'new': ref_insert_ids,
'new_lockids': ref_lock_ids,
'new_relations': new_relations
'relations': relations,
'update': ref_update_ids,

}
})
except:
Expand Down
2 changes: 1 addition & 1 deletion g3w-admin/editing/static/editing/js/plugin.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion g3w-admin/editing/static/editing/js/plugin.js.map

Large diffs are not rendered by default.

31 changes: 21 additions & 10 deletions g3w-admin/editing/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -1596,23 +1596,23 @@ def test_editing_commit_mode_api_cascade_relations(self):
newid = jresult['response']['new'][0]['id']
newlockid = jresult['response']['new_lockids'][0]['lockid']

newid_mantaince_work = jresult['response']['new_relations'][maintenance_works_id]['new'][0]['id']
newlockid_mantaince_work = jresult['response']['new_relations'][maintenance_works_id]['new_lockids'][0]['lockid']
newid_mantaince_work = jresult['response']['relations'][maintenance_works_id]['new'][0]['id']
newlockid_mantaince_work = jresult['response']['relations'][maintenance_works_id]['new_lockids'][0]['lockid']

newid_mantaince_rel1 = jresult['response']['new_relations'][maintenance_rel1_id]['new'][0]['id']
newlockid_mantaince_rel1 = jresult['response']['new_relations'][maintenance_rel1_id]['new_lockids'][0][
newid_mantaince_rel1 = jresult['response']['relations'][maintenance_rel1_id]['new'][0]['id']
newlockid_mantaince_rel1 = jresult['response']['relations'][maintenance_rel1_id]['new_lockids'][0][
'lockid']

newid_mantaince_rel2 = jresult['response']['new_relations'][maintenance_rel2_id]['new'][0]['id']
newlockid_mantaince_rel2 = jresult['response']['new_relations'][maintenance_rel2_id]['new_lockids'][0][
newid_mantaince_rel2 = jresult['response']['relations'][maintenance_rel2_id]['new'][0]['id']
newlockid_mantaince_rel2 = jresult['response']['relations'][maintenance_rel2_id]['new_lockids'][0][
'lockid']

newid_rating = jresult['response']['new_relations'][buildings_rating_id]['new'][0]['id']
newlockid_reiting = jresult['response']['new_relations'][buildings_rating_id]['new_lockids'][0][
newid_rating = jresult['response']['relations'][buildings_rating_id]['new'][0]['id']
newlockid_reiting = jresult['response']['relations'][buildings_rating_id]['new_lockids'][0][
'lockid']

newid_rating_rel1 = jresult['response']['new_relations'][rating_rel1_id]['new'][0]['id']
newlockid_reiting_rel1 = jresult['response']['new_relations'][rating_rel1_id]['new_lockids'][0][
newid_rating_rel1 = jresult['response']['relations'][rating_rel1_id]['new'][0]['id']
newlockid_reiting_rel1 = jresult['response']['relations'][rating_rel1_id]['new_lockids'][0][
'lockid']


Expand Down Expand Up @@ -1853,6 +1853,17 @@ def test_editing_commit_mode_api_cascade_relations(self):
response = self.client.post(commit_path, payload, format='json')
self.assertEqual(response.status_code, 200)

jresult = json.loads(response.content)

self.assertTrue('update' in jresult['response'])
self.assertEqual(jresult['response']['update'][0]['id'], str(newid))
self.assertTrue('update' in jresult['response']['relations'][maintenance_works_id])
self.assertEqual(jresult['response']['relations'][maintenance_works_id]['update'][0]['id'],
str(newid_mantaince_work))
self.assertEqual(jresult['response']['relations'][maintenance_rel1_id]['update'][0]['id'],
str(newid_mantaince_rel1))


# Check for mantaince rel1 layer: manitenance_rel1_3ce8bd5c_c62d_45bc_8f81_b301e6592128
data_path = reverse('core-vector-api',
args=['data', 'qdjango', self.editing_cascade_relations_project.instance.pk,
Expand Down
6 changes: 3 additions & 3 deletions g3w-admin/editing/tests/test_api_transaction_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -511,14 +511,14 @@ def test_add_feature_relations(self):
self.assertTrue('properties' in jresult['response']['new'][0])
self.assertEqual(jresult['response']['new'][0]['properties']['name'], "name father")
self.assertEqual(jresult['response']['new'][0]['properties']['value'], 4444)
self.assertNotEqual(jresult['response']['new_relations'], {})
self.assertNotEqual(jresult['response']['relations'], {})

# Verify
parent_id_server = jresult['response']['new'][0]['id']
parent_id = get_layer_fids_from_server_fids([parent_id_server], self.poligoni.qgis_layer)[0]
self.assertTrue(
self.poligoni.qgis_layer.getFeature(parent_id).isValid())
child_id_server = jresult['response']['new_relations']['test_afb61649_1fb2_426e_b588_04217314f0c4']['new'][0]['id']
child_id_server = jresult['response']['relations']['test_afb61649_1fb2_426e_b588_04217314f0c4']['new'][0]['id']
child_id = get_layer_fids_from_server_fids([child_id_server], self.test.qgis_layer)[0]
self.assertTrue(
self.test.qgis_layer.getFeature(child_id).isValid())
Expand All @@ -531,7 +531,7 @@ def test_add_feature_relations(self):
# There is no cascade here! Delete the child.
response = client.post(child_commit_path, {
"delete": [child_id_server],
"lockids": jresult['response']['new_relations']['test_afb61649_1fb2_426e_b588_04217314f0c4']['new_lockids']
"lockids": jresult['response']['relations']['test_afb61649_1fb2_426e_b588_04217314f0c4']['new_lockids']
}, format='json')

self.assertTrue(json.loads(response.content)['result'])
Expand Down

0 comments on commit 6647b74

Please sign in to comment.