Skip to content

Commit 54939ac

Browse files
committed
Introducing JoinManager (wip)
1 parent cc2b73d commit 54939ac

File tree

7 files changed

+739
-25
lines changed

7 files changed

+739
-25
lines changed

pygeoapi/api/joins.py

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
import logging
3030
from typing import Any
31+
from datetime import datetime, timedelta, timezone
3132

3233
from pygeoapi import l10n, join_util
3334
from pygeoapi.api import (
@@ -39,7 +40,8 @@
3940
from pygeoapi.provider.base import ProviderTypeError, ProviderGenericError
4041
from pygeoapi.util import (
4142
get_provider_by_type, to_json, filter_providers_by_type,
42-
filter_dict_by_key_value, get_current_datetime, render_j2_template
43+
filter_dict_by_key_value, get_current_datetime, render_j2_template,
44+
str_to_datetime
4345
)
4446

4547
LOGGER = logging.getLogger(__name__)
@@ -707,6 +709,12 @@ def join_details(api: API, request: APIRequest,
707709
l10n.set_response_language(headers, request.locale)
708710

709711
if request.format == F_HTML: # render
712+
join_created = str_to_datetime(output['details']['created'])
713+
if datetime.now(timezone.utc) - join_created < timedelta(seconds=10):
714+
# If join is less than 10 seconds old, we were probably redirected
715+
# by create_join(): add a message that join was successful
716+
output['description'] = l10n.translate(
717+
'Join source created successfully.', request.locale)
710718
title = f'{collections[dataset]['title']} - Join Source'
711719
content = _render_html(api, request, dataset,
712720
'collections/joinsource.html', title, output)
@@ -761,6 +769,19 @@ def create_join(api: API, request: APIRequest,
761769

762770
uri = f'{api.get_collections_url()}/{dataset}'
763771
join_id = details['id']
772+
773+
# Set response language to requested provider locale
774+
# (if it supports language) and/or otherwise the requested pygeoapi
775+
# locale (or fallback default locale)
776+
l10n.set_response_language(headers, prv_locale, request.locale)
777+
778+
if request.format == F_HTML:
779+
# For HTML only, we'll redirect to the join details page instead
780+
# to avoid form resubmission. This is known as the PRG pattern:
781+
# https://en.wikipedia.org/wiki/Post/Redirect/Get
782+
headers['Location'] = f'{uri}/joins/{join_id}'
783+
return headers, HTTPStatus.SEE_OTHER, 'created successfully'
784+
764785
output = {
765786
'id': join_id,
766787
'timeStamp': get_current_datetime(),
@@ -819,19 +840,6 @@ def create_join(api: API, request: APIRequest,
819840
msg = f'Failed to create join: {str(e)}'
820841
return _server_error(api, request, headers, msg)
821842

822-
# Set response language to requested provider locale
823-
# (if it supports language) and/or otherwise the requested pygeoapi
824-
# locale (or fallback default locale)
825-
l10n.set_response_language(headers, prv_locale, request.locale)
826-
827-
if request.format == F_HTML:
828-
# Render same page as join details to show result of POST
829-
title = f'{collections[dataset]['title']} - Join Source'
830-
content = _render_html(api, request, dataset,
831-
'collections/joinsource.html', title, output,
832-
description="Join source created successfully.")
833-
return headers, HTTPStatus.OK, content
834-
835843
return headers, HTTPStatus.OK, to_json(output, api.pretty_print)
836844

837845

pygeoapi/flask_app.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -436,8 +436,8 @@ def joins(collection_id, joinId=None):
436436
collection_id, joinId)
437437
elif request.method == 'POST':
438438
# Create a new join
439-
return execute_from_flask(joins_api.create_join, request,
440-
collection_id)
439+
return execute_from_flask(joins_api.create_join,
440+
request, collection_id)
441441
else:
442442
# Delete an existing join
443443
return execute_from_flask(joins_api.delete_join, request,

pygeoapi/join/__init__.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# =================================================================
2+
# Authors: Sander Schaminee <sander.schaminee@geocat.net>
3+
#
4+
# Copyright (c) 2025 Sander Schaminee
5+
#
6+
# Permission is hereby granted, free of charge, to any person
7+
# obtaining a copy of this software and associated documentation
8+
# files (the "Software"), to deal in the Software without
9+
# restriction, including without limitation the rights to use,
10+
# copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
# copies of the Software, and to permit persons to whom the
12+
# Software is furnished to do so, subject to the following
13+
# conditions:
14+
#
15+
# The above copyright notice and this permission notice shall be
16+
# included in all copies or substantial portions of the Software.
17+
#
18+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19+
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
20+
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21+
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
22+
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23+
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24+
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25+
# OTHER DEALINGS IN THE SOFTWARE.
26+
#
27+
# =================================================================

0 commit comments

Comments
 (0)