mirror of
https://github.com/django-components/django-components.git
synced 2025-09-22 13:42:27 +00:00
refactor: update impl of format_url (#1163)
This commit is contained in:
parent
d4d834256a
commit
330578a2c7
2 changed files with 15 additions and 15 deletions
|
@ -161,19 +161,10 @@ def format_url(url: str, query: Optional[Dict] = None, fragment: Optional[str] =
|
||||||
|
|
||||||
`query` and `fragment` are optional, and not applied if `None`.
|
`query` and `fragment` are optional, and not applied if `None`.
|
||||||
"""
|
"""
|
||||||
url_parts = parse.urlsplit(url)
|
parts = parse.urlsplit(url)
|
||||||
|
fragment_enc = parse.quote(fragment or parts.fragment, safe="")
|
||||||
|
base_qs = dict(parse.parse_qsl(parts.query))
|
||||||
|
merged = {**base_qs, **(query or {})}
|
||||||
|
encoded_qs = parse.urlencode(merged, safe="")
|
||||||
|
|
||||||
escaped_fragment = parse.quote(str(fragment)) if fragment is not None else url_parts.fragment
|
return parse.urlunsplit(parts._replace(query=encoded_qs, fragment=fragment_enc))
|
||||||
|
|
||||||
# NOTE: parse_qsl returns a list of tuples. For simpicity we support only one
|
|
||||||
# value per key, so we need to unpack the first element.
|
|
||||||
query_from_url = {key: val[0] for key, val in parse.parse_qsl(url_parts.query)}
|
|
||||||
all_params = {**query_from_url, **(query or {})}
|
|
||||||
|
|
||||||
encoded_params = parse.urlencode(all_params)
|
|
||||||
updated_parts = url_parts._replace(
|
|
||||||
query=encoded_params,
|
|
||||||
fragment=escaped_fragment,
|
|
||||||
)
|
|
||||||
new_url = parse.urlunsplit(updated_parts)
|
|
||||||
return new_url
|
|
||||||
|
|
|
@ -9,6 +9,7 @@ from django.urls import path
|
||||||
|
|
||||||
from django_components import Component, ComponentView, get_component_url, register, types
|
from django_components import Component, ComponentView, get_component_url, register, types
|
||||||
from django_components.urls import urlpatterns as dc_urlpatterns
|
from django_components.urls import urlpatterns as dc_urlpatterns
|
||||||
|
from django_components.util.misc import format_url
|
||||||
|
|
||||||
from django_components.testing import djc_test
|
from django_components.testing import djc_test
|
||||||
from .testutils import setup_test_config
|
from .testutils import setup_test_config
|
||||||
|
@ -315,6 +316,14 @@ class TestComponentAsView(SimpleTestCase):
|
||||||
component_url3 = get_component_url(TestComponent, query={"f'oo": "b ar&ba'z"}, fragment='q u"x')
|
component_url3 = get_component_url(TestComponent, query={"f'oo": "b ar&ba'z"}, fragment='q u"x')
|
||||||
assert component_url3 == f"/components/ext/view/components/{TestComponent.class_id}/?f%27oo=b+ar%26ba%27z#q%20u%22x" # noqa: E501
|
assert component_url3 == f"/components/ext/view/components/{TestComponent.class_id}/?f%27oo=b+ar%26ba%27z#q%20u%22x" # noqa: E501
|
||||||
|
|
||||||
|
# Merges query params from original URL
|
||||||
|
component_url4 = format_url(
|
||||||
|
"/components/ext/view/components/123?foo=123&bar=456#abc",
|
||||||
|
query={"foo": "new", "baz": "new2"},
|
||||||
|
fragment='xyz',
|
||||||
|
)
|
||||||
|
assert component_url4 == "/components/ext/view/components/123?foo=new&bar=456&baz=new2#xyz"
|
||||||
|
|
||||||
def test_public_url(self):
|
def test_public_url(self):
|
||||||
did_call_get = False
|
did_call_get = False
|
||||||
did_call_post = False
|
did_call_post = False
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue