Skip to content

Commit b0d8b1a

Browse files
committed
feat: upgrade step to clean TS api URL in registry to keep only base URL
CITI-15
1 parent eed81d5 commit b0d8b1a

File tree

6 files changed

+86
-2
lines changed

6 files changed

+86
-2
lines changed

CHANGES.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ Changelog
55
1.2.12 (unreleased)
66
-------------------
77

8-
- Nothing changed yet.
8+
- CITI-15 : Add upgrade step to clean TS api URL in registry to keep only base URL.
9+
[remdub]
910

1011

1112
1.2.11 (2025-09-26)

src/imio/smartweb/policy/profiles/default/metadata.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<metadata>
3-
<version>1038</version>
3+
<version>1039</version>
44
<dependencies>
55
<dependency>profile-plone.app.contenttypes:plone-content</dependency>
66
<dependency>profile-plone.app.caching:default</dependency>
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
from imio.smartweb.policy.testing import (
2+
IMIO_SMARTWEB_POLICY_INTEGRATION_TESTING,
3+
) # noqa: E501
4+
from unittest.mock import patch
5+
from imio.smartweb.policy.utils import get_ts_api_base_url
6+
7+
import unittest
8+
9+
10+
class TestGetTsApiBaseUrl(unittest.TestCase):
11+
12+
layer = IMIO_SMARTWEB_POLICY_INTEGRATION_TESTING
13+
14+
@patch("imio.smartweb.core.utils.get_value_from_registry")
15+
@patch("imio.smartweb.core.utils.is_valid_url")
16+
@patch("imio.smartweb.policy.utils.urlparse")
17+
def test_valid_url(self, mock_urlparse, mock_is_valid_url, mock_get_value):
18+
mock_get_value.return_value = "https://kamoulox.be/formsdefs/something"
19+
mock_is_valid_url.return_value = True
20+
mock_urlparse.return_value.scheme = "https"
21+
mock_urlparse.return_value.netloc = "kamoulox.be"
22+
result = get_ts_api_base_url()
23+
self.assertEqual(result, "https://kamoulox.be/")
24+
25+
@patch("imio.smartweb.core.utils.get_value_from_registry")
26+
@patch("imio.smartweb.core.utils.is_valid_url")
27+
def test_invalid_url(self, mock_is_valid_url, mock_get_value):
28+
mock_get_value.return_value = "not-a-valid-url"
29+
mock_is_valid_url.return_value = False
30+
result = get_ts_api_base_url()
31+
self.assertIsNone(result)
32+
33+
@patch("imio.smartweb.core.utils.get_value_from_registry")
34+
@patch("imio.smartweb.core.utils.is_valid_url")
35+
def test_empty_url(self, mock_is_valid_url, mock_get_value):
36+
mock_get_value.return_value = ""
37+
mock_is_valid_url.return_value = False
38+
result = get_ts_api_base_url()
39+
self.assertIsNone(result)

src/imio/smartweb/policy/upgrades/configure.zcml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -586,4 +586,13 @@
586586
profile="imio.smartweb.policy:default"
587587
/>
588588

589+
<genericsetup:upgradeStep
590+
title="Clean TS api URL"
591+
description="Clean TS api URL in registry to keep only base URL"
592+
source="1038"
593+
destination="1039"
594+
handler=".upgrades.clean_ts_api_url"
595+
profile="imio.smartweb.policy:default"
596+
/>
597+
589598
</configure>

src/imio/smartweb/policy/upgrades/upgrades.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,25 @@ def set_keycloak_login_group(context):
6161
oidc.allowed_groups = ["iA.Smartweb"]
6262
else:
6363
logger.warning("OIDC plugin not found in acl_users; cannot set allowed_groups.")
64+
65+
66+
def clean_ts_api_url(context):
67+
from imio.smartweb.policy.utils import get_ts_api_base_url
68+
69+
url_before = api.portal.get_registry_record("smartweb.url_ts")
70+
if url_before is None:
71+
logger.info("TS api URL is empty in registry, nothing to clean.")
72+
return
73+
url_after = get_ts_api_base_url()
74+
if url_before == url_after:
75+
logger.info(
76+
f"TS api URL {url_before} is already clean in registry, nothing to do."
77+
)
78+
return
79+
if url_after is None:
80+
logger.warning(
81+
f"TS api URL {url_before} is not valid, cannot be cleaned. Please check the value in the registry."
82+
)
83+
return
84+
api.portal.set_registry_record("smartweb.url_ts", url_after)
85+
logger.info(f"TS api URL {url_before} cleaned in registry. New value: {url_after}.")

src/imio/smartweb/policy/utils.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from plone import api
66
from plone.portlets.interfaces import IPortletAssignmentMapping
77
from plone.portlets.interfaces import IPortletManager
8+
from urllib.parse import urlparse
89
from z3c.form.interfaces import IFormLayer
910
from zope.component import getMultiAdapter
1011
from zope.component import getUtility
@@ -171,3 +172,15 @@ def get_cookie_policy_content():
171172
<p>Pour supprimer rapidement les cookies, certains navigateurs (Firefox, Chrome, Edge, Internet Explorer) proposent un raccourci clavier par l'appui simultané sur les touches CTRL/CMD, MAJ et DELETE.</p>
172173
"""
173174
return cookie_policy_html
175+
176+
177+
def get_ts_api_base_url() -> str | None:
178+
"""Get the base url of the ts api."""
179+
from imio.smartweb.core.utils import get_value_from_registry
180+
from imio.smartweb.core.utils import is_valid_url
181+
182+
url_ts = get_value_from_registry("smartweb.url_ts")
183+
if is_valid_url(url_ts):
184+
parsed_url = urlparse(url_ts)
185+
return f"{parsed_url.scheme}://{parsed_url.netloc}/"
186+
return None

0 commit comments

Comments
 (0)