Skip to content

Commit

Permalink
Update map_layer table in PATCH Workspace Map
Browse files Browse the repository at this point in the history
  • Loading branch information
index-git authored and jirik committed Sep 18, 2023
1 parent b9e57c9 commit 63c5284
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 7 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
- [#868](https://github.com/LayerManager/layman/issues/868) Fill table `map_layer` with relations between maps and [internal layers](doc/models.md#internal-map-layer) (layers published on this Layman instance). Relations to [external layers](doc/models.md#internal-map-layer) (layers of other servers) are not imported into the table.
### Changes
- [#868](https://github.com/LayerManager/layman/issues/868) Endpoints [GET Publications](doc/rest.md#get-publications), [GET Layers](doc/rest.md#get-layers), [GET Workspace Layers](doc/rest.md#get-workspace-layers), [GET Maps](doc/rest.md#get-maps), [GET Workspace Maps](doc/rest.md#get-workspace-maps), [GET Workspace Layer](doc/rest.md#get-workspace-layer), [GET Workspace Map](doc/rest.md#get-workspace-map), [POST Workspace Layers](doc/rest.md#post-workspace-layers), [DELETE Workspace Layer](doc/rest.md#delete-workspace-layer), [DELETE Workspace Layers](doc/rest.md#delete-workspace-layers), [DELETE Workspace Map](doc/rest.md#delete-workspace-map), [DELETE Workspace Maps](doc/rest.md#delete-workspace-maps), [POST Workspace Maps](doc/rest.md#post-workspace-maps), [PATCH Workspace Layer](doc/rest.md#patch-workspace-layer), [PATCH Workspace Map](doc/rest.md#patch-workspace-map) and [WMS/WFS endpoints](doc/endpoints.md) respects [HTTP header `X-Forwarded-Prefix`](doc/client-proxy.md#x-forwarded-prefix-http-header) of the request in the response.
- [#868](https://github.com/LayerManager/layman/issues/868) Relations between map and [internal layers](doc/models.md#internal-map-layer) are updated in `map_layer` table when calling [POST Workspace Maps](doc/rest.md#post-workspace-maps), [DELETE Workspace Map](doc/rest.md#delete-workspace-map), and [DELETE Workspace Maps](doc/rest.md#delete-workspace-maps).
- [#868](https://github.com/LayerManager/layman/issues/868) Relations between map and [internal layers](doc/models.md#internal-map-layer) are updated in `map_layer` table when calling [POST Workspace Maps](doc/rest.md#post-workspace-maps), [PATCH Workspace Map](doc/rest.md#patch-workspace-map), [DELETE Workspace Map](doc/rest.md#delete-workspace-map), and [DELETE Workspace Maps](doc/rest.md#delete-workspace-maps).
- [#880](https://github.com/LayerManager/layman/issues/880) Use Docker Compose v2 (`docker compose`) in Makefile without `compatibility` flag and remove `Makefile_docker-compose_v1` file. Docker containers are named according to Docker Compose v2 and may have different name after upgrade.
- [#765](https://github.com/LayerManager/layman/issues/765) Stop saving OAuth2 claims in filesystem, use prime DB schema only.
- [#893](https://github.com/LayerManager/layman/issues/893) It is possible to specify logging level by new environment variable [LAYMAN_LOGLEVEL](doc/env-settings.md#LAYMAN_LOGLEVEL). Default level is `INFO`.
Expand Down
4 changes: 2 additions & 2 deletions src/layman/map/prime_db_schema/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from layman import celery_app
from layman.celery import AbortedException
from layman.common import empty_method_returns_true
from .util import insert_internal_layers
from .util import ensure_internal_layers
from .. import util, MAP_TYPE
from ...common.prime_db_schema.publications import set_bbox

Expand Down Expand Up @@ -32,7 +32,7 @@ def refresh_file_data(
set_bbox(workspace, MAP_TYPE, mapname, native_bbox, crs, )

map_layers = util.get_layers_from_json(mapjson)
insert_internal_layers(workspace, mapname, map_layers)
ensure_internal_layers(workspace, mapname, map_layers)

if self.is_aborted():
raise AbortedException
23 changes: 20 additions & 3 deletions src/layman/map/prime_db_schema/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,31 @@
DB_SCHEMA = settings.LAYMAN_PRIME_SCHEMA


def insert_internal_layers(workspace, mapname, layers):
map_id = get_publication_info(workspace, MAP_TYPE, mapname, context={'keys': ['id'], })['id']
for layer_workspace, layer_name, layer_index in layers:
def ensure_internal_layers(workspace, mapname, layers):
map_info = get_publication_info(workspace, MAP_TYPE, mapname, context={'keys': ['id', 'map_layers'], })
map_id = map_info['id']
db_layers = {(layer_dict['workspace'], layer_dict['name'], layer_dict['index'], ) for layer_dict in map_info['_map_layers']}

to_insert = set(layers) - db_layers
to_delete = db_layers - set(layers)

for layer_workspace, layer_name, layer_index in to_insert:
insert_query = f'''
insert into {DB_SCHEMA}.map_layer(id_map, layer_workspace, layer_name, layer_index) values (%s, %s, %s, %s);
'''
db_util.run_statement(insert_query, (map_id, layer_workspace, layer_name, layer_index,))

for layer_workspace, layer_name, layer_index in to_delete:
delete_query = f'''
delete from {DB_SCHEMA}.map_layer
where id_map = %s
and layer_workspace = %s
and layer_name = %s
and layer_index = %s
;
'''
db_util.run_statement(delete_query, (map_id, layer_workspace, layer_name, layer_index,))


def delete_internal_layer_relations(workspace, mapname):
map_id = get_publication_info(workspace, MAP_TYPE, mapname, context={'keys': ['id'], })['id']
Expand Down
2 changes: 1 addition & 1 deletion tests/asserts/final/publication/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
'identifier',
'map_endpoint',
'map_file_endpoint',
'operates_on',
'operates_on', # When sending map with no items after there were some items, items are not deleted (probably because of bug in Micka)
'organisation_name',
'publication_date',
'reference_system',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,29 @@
'operates_on': [LAYER_HRANICE_PRIVATE],
},
},
'patch_map_with_different_layers': {
'post_before_test_args': {
'file_paths': [os.path.join(DIRECTORY, 'internal_wms_and_wfs.json')],
},
'exp_before_rest_method': {
'map_layers': {
# layer, layer index, exists?
(LAYER_HRANICE, 1, True),
(LAYER_MISTA_NON_EXISTENT, 2, False),
(LAYER_HRANICE, 3, True),
},
'operates_on': [LAYER_HRANICE],
},
'rest_method': base_test_classes.RestMethodAll.PATCH,
'rest_args': {
'file_paths': [os.path.join(DIRECTORY, 'internal_hranice_private.json')],
'actor_name': PRIVATE_WORKSPACE,
},
'exp_after_rest_method': {
'map_layers': [(LAYER_HRANICE_PRIVATE, 1, True)],
'operates_on': [LAYER_HRANICE_PRIVATE],
},
},
}


Expand Down

0 comments on commit 63c5284

Please sign in to comment.