-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #559 from GrahamDumpleton/redirection-query-params
Fixup addition of redirection query param for notification.
- Loading branch information
Showing
4 changed files
with
125 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
training-portal/src/project/apps/workshops/views/helpers.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
"""Helper functions for dealing with URLs.""" | ||
|
||
from urllib.parse import urlparse, parse_qs, urlencode, urlunparse, urljoin | ||
|
||
|
||
def update_query_params(url, params): | ||
"""Update the query parameters of a URL with the given parameters. If the URL | ||
is malformed or cannot be parsed, the original URL is returned.""" | ||
|
||
try: | ||
# Parse the URL. | ||
|
||
parsed_url = urlparse(url) | ||
|
||
# Handle URLs with no scheme or netloc (e.g., paths like '/page'). | ||
|
||
if not parsed_url.scheme and not parsed_url.netloc: | ||
# Treat it as a relative path URL. | ||
|
||
base_url = "http://dummy" # Temporary base for relative path handling | ||
parsed_url = urlparse(urljoin(base_url, url)) | ||
|
||
# Parse existing query parameters. | ||
|
||
query_params = parse_qs(parsed_url.query) | ||
|
||
# Update or add the new parameters. | ||
|
||
query_params.update({key: [value] for key, value in params.items()}) | ||
|
||
# Reconstruct the URL with the updated query string. | ||
|
||
updated_query = urlencode(query_params, doseq=True) | ||
updated_url = urlunparse(parsed_url._replace(query=updated_query)) | ||
|
||
# If the URL was originally a path, strip out the dummy scheme and netloc. | ||
|
||
if parsed_url.scheme == "http" and parsed_url.netloc == "dummy": | ||
return updated_url.replace("http://dummy", "") | ||
|
||
return updated_url | ||
|
||
except Exception: # pylint: disable=broad-except | ||
# In case of any parsing errors, return the original URL. | ||
|
||
return url |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters