|
28 | 28 |
|
29 | 29 | import logging |
30 | 30 | from typing import Any |
| 31 | +from datetime import datetime, timedelta, timezone |
31 | 32 |
|
32 | 33 | from pygeoapi import l10n, join_util |
33 | 34 | from pygeoapi.api import ( |
|
39 | 40 | from pygeoapi.provider.base import ProviderTypeError, ProviderGenericError |
40 | 41 | from pygeoapi.util import ( |
41 | 42 | 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 |
43 | 45 | ) |
44 | 46 |
|
45 | 47 | LOGGER = logging.getLogger(__name__) |
@@ -707,6 +709,12 @@ def join_details(api: API, request: APIRequest, |
707 | 709 | l10n.set_response_language(headers, request.locale) |
708 | 710 |
|
709 | 711 | 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) |
710 | 718 | title = f'{collections[dataset]['title']} - Join Source' |
711 | 719 | content = _render_html(api, request, dataset, |
712 | 720 | 'collections/joinsource.html', title, output) |
@@ -761,6 +769,19 @@ def create_join(api: API, request: APIRequest, |
761 | 769 |
|
762 | 770 | uri = f'{api.get_collections_url()}/{dataset}' |
763 | 771 | 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 | + |
764 | 785 | output = { |
765 | 786 | 'id': join_id, |
766 | 787 | 'timeStamp': get_current_datetime(), |
@@ -819,19 +840,6 @@ def create_join(api: API, request: APIRequest, |
819 | 840 | msg = f'Failed to create join: {str(e)}' |
820 | 841 | return _server_error(api, request, headers, msg) |
821 | 842 |
|
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 | | - |
835 | 843 | return headers, HTTPStatus.OK, to_json(output, api.pretty_print) |
836 | 844 |
|
837 | 845 |
|
|
0 commit comments