diff --git a/src/layman/layer/filesystem/thumbnail.py b/src/layman/layer/filesystem/thumbnail.py index a78206c8a..e3e8e4813 100644 --- a/src/layman/layer/filesystem/thumbnail.py +++ b/src/layman/layer/filesystem/thumbnail.py @@ -1,6 +1,5 @@ import os import pathlib -from urllib.parse import urljoin from layman import settings from layman import patch_mode @@ -64,11 +63,10 @@ def get_layer_thumbnail_path(username, layername): def generate_layer_thumbnail(workspace, layername): - geoserver_workspace = geoserver_wms.get_geoserver_workspace(workspace) headers = { settings.LAYMAN_GS_AUTHN_HTTP_HEADER_ATTRIBUTE: settings.LAYMAN_GS_USER, } - wms_url = urljoin(settings.LAYMAN_GS_URL, geoserver_workspace + '/ows') + wms_url = geoserver_wms.get_wms_url(workspace) from layman.layer.geoserver.util import wms_proxy from layman.common.geoserver import get_layer_thumbnail, get_layer_square_bbox wms = wms_proxy(wms_url, headers=headers) diff --git a/src/layman/layer/geoserver/sld.py b/src/layman/layer/geoserver/sld.py index ad8c8b381..ff67ebc43 100644 --- a/src/layman/layer/geoserver/sld.py +++ b/src/layman/layer/geoserver/sld.py @@ -21,6 +21,20 @@ } +def get_workspace_style_url(workspace, style=None): + style = style or '' + geoserver_workspace = wms.get_geoserver_workspace(workspace) + return urljoin(settings.LAYMAN_GS_REST_WORKSPACES, + geoserver_workspace + '/styles/' + style) + + +def get_workspace_layer_url(workspace, layer=None): + layer = layer or '' + geoserver_workspace = wms.get_geoserver_workspace(workspace) + return urljoin(settings.LAYMAN_GS_REST_WORKSPACES, + geoserver_workspace + '/layers/' + layer) + + def pre_publication_action_check(username, layername): pass @@ -34,9 +48,7 @@ def patch_layer(username, layername): def delete_layer(workspace, layername): - geoserver_workspace = wms.get_geoserver_workspace(workspace) - style_url = urljoin(settings.LAYMAN_GS_REST_WORKSPACES, - geoserver_workspace + '/styles/' + layername) + style_url = get_workspace_style_url(workspace, layername) r = requests.get(style_url + '.sld', auth=settings.LAYMAN_GS_AUTH, timeout=5, @@ -105,7 +117,7 @@ def create_layer_style(workspace, layername): r.raise_for_status() sld_file = io.BytesIO(r.content) r = requests.post( - urljoin(settings.LAYMAN_GS_REST_WORKSPACES, geoserver_workspace + '/styles/'), + get_workspace_style_url(workspace), data=json.dumps( { "style": { @@ -148,8 +160,7 @@ def create_layer_style(workspace, layername): sld_file.seek(0) r = requests.put( - urljoin(settings.LAYMAN_GS_REST_WORKSPACES, - geoserver_workspace + '/styles/' + layername), + get_workspace_style_url(workspace, layername), data=sld_file.read(), headers={ 'Accept': 'application/json', @@ -161,8 +172,7 @@ def create_layer_style(workspace, layername): if r.status_code == 400: raise LaymanError(14, data=r.text) r.raise_for_status() - r = requests.put(urljoin(settings.LAYMAN_GS_REST_WORKSPACES, - geoserver_workspace + '/layers/' + layername), + r = requests.put(get_workspace_layer_url(workspace, layername), data=json.dumps( { "layer": { @@ -185,10 +195,9 @@ def get_metadata_comparison(username, layername): def get_style_response(workspace, stylename, headers=None, auth=None): - geoserver_workspace = wms.get_geoserver_workspace(workspace) if headers is None: headers = headers_sld - url = settings.LAYMAN_GS_REST + f'workspaces/{geoserver_workspace}/styles/{stylename}' + url = get_workspace_style_url(workspace, stylename) r = requests.get(url, auth=auth, diff --git a/src/layman/layer/rest_test.py b/src/layman/layer/rest_test.py index 8e5e21188..3464ea13f 100644 --- a/src/layman/layer/rest_test.py +++ b/src/layman/layer/rest_test.py @@ -2,7 +2,6 @@ import io import json import os -from multiprocessing import Process import requests import time import xml.etree.ElementTree as ET @@ -25,6 +24,7 @@ from layman.layer.filesystem.thumbnail import get_layer_thumbnail_path from layman import uuid from layman.layer import db +from layman.layer.geoserver import wms as geoserver_wms, sld as geoserver_sld from layman import celery as celery_util from .micka import csw from layman.common.micka import util as micka_common_util @@ -289,7 +289,7 @@ def test_post_layers_simple(client): assert isinstance(layer_info[key_to_check], str) \ or 'status' not in layer_info[key_to_check] - wms_url = urljoin(settings.LAYMAN_GS_URL, username + '_wms/ows') + wms_url = geoserver_wms.get_wms_url(username) wms = wms_proxy(wms_url) assert layername in wms.contents @@ -455,7 +455,7 @@ def test_post_layers_shp(client): flask_client.wait_till_layer_ready(username, layername) # last_task['last'].get() - wms_url = urljoin(settings.LAYMAN_GS_URL, username + '_wms/ows') + wms_url = geoserver_wms.get_wms_url(username) wms = wms_proxy(wms_url) assert 'ne_110m_admin_0_countries_shp' in wms.contents uuid.check_redis_consistency(expected_publ_num_by_type={ @@ -555,7 +555,7 @@ def test_post_layers_complex(client): # last_task['last'].get() assert celery_util.is_task_ready(last_task) - wms_url = urljoin(settings.LAYMAN_GS_URL, username + '_wms/ows') + wms_url = geoserver_wms.get_wms_url(username) wms = wms_proxy(wms_url) assert 'countries' in wms.contents assert wms['countries'].title == 'staty' @@ -580,8 +580,7 @@ def test_post_layers_complex(client): ]: assert 'status' not in resp_json[source] - style_url = urljoin(settings.LAYMAN_GS_REST_WORKSPACES, - username + '_wms/styles/' + layername) + style_url = geoserver_sld.get_workspace_style_url(username, layername) r = requests.get(style_url + '.sld', auth=settings.LAYMAN_GS_AUTH ) @@ -670,8 +669,7 @@ def test_uppercase_attr(client): ]: assert 'status' not in resp_json[source], f"{source}: {resp_json[source]}" - style_url = urljoin(settings.LAYMAN_GS_REST_WORKSPACES, - username + '_wms/styles/' + layername) + style_url = geoserver_sld.get_workspace_style_url(username, layername) r = requests.get(style_url + '.sld', auth=settings.LAYMAN_GS_AUTH ) @@ -807,7 +805,7 @@ def test_patch_layer_style(client): resp_json = rv.get_json() assert resp_json['title'] == "countries in blue" - wms_url = urljoin(settings.LAYMAN_GS_URL, username + '_wms/ows') + wms_url = geoserver_wms.get_wms_url(username) wms = wms_proxy(wms_url) assert layername in wms.contents assert wms[layername].title == 'countries in blue' @@ -872,13 +870,12 @@ def test_post_layers_sld_1_1_0(client): time.sleep(0.1) layer_info = util.get_layer_info(username, layername) - wms_url = urljoin(settings.LAYMAN_GS_URL, username + '_wms/ows') + wms_url = geoserver_wms.get_wms_url(username) wms = wms_proxy(wms_url) assert layername in wms.contents assert wms[layername].title == 'countries_sld_1_1_0' - style_url = urljoin(settings.LAYMAN_GS_REST_WORKSPACES, - username + '_wms/styles/' + layername) + style_url = geoserver_sld.get_workspace_style_url(username, layername) r = requests.get(style_url + '.sld', auth=settings.LAYMAN_GS_AUTH )