-
Notifications
You must be signed in to change notification settings - Fork 26
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 #3574 from open-formulieren/issue/3362-hash-redirects
🐛 Handle hash based frontend routing
- Loading branch information
Showing
13 changed files
with
375 additions
and
95 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
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
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,3 @@ | ||
from .frontend import get_frontend_redirect_url | ||
|
||
__all__ = ["get_frontend_redirect_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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import json | ||
from typing import Literal, TypeAlias | ||
|
||
from openforms.submissions.models import Submission | ||
|
||
SDKAction: TypeAlias = Literal["resume", "afspraak-annuleren", "cosign"] | ||
|
||
|
||
def get_frontend_redirect_url( | ||
submission: Submission, | ||
action: SDKAction, | ||
action_params: dict[str, str] | None = None, | ||
) -> str: | ||
"""Get the frontend redirect URL depending on the action. | ||
Some actions require arguments to be specified. The frontend will take care of building the right redirection | ||
based on the action and action arguments. | ||
""" | ||
f = submission.cleaned_form_url | ||
f.query.remove("_of_action") | ||
f.query.remove("_of_action_params") | ||
_query = { | ||
"_of_action": action, | ||
} | ||
if action_params: | ||
_query["_of_action_params"] = json.dumps(action_params) | ||
|
||
return f.add(_query).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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import json | ||
from typing import Any | ||
|
||
from django.http.response import HttpResponse | ||
from django.test import SimpleTestCase | ||
|
||
from furl import furl | ||
|
||
from .frontend import SDKAction | ||
|
||
|
||
class FrontendRedirectMixin: | ||
"""A mixin providing a helper method checking frontend redirects.""" | ||
|
||
def assertRedirectsToFrontend( | ||
self: SimpleTestCase, | ||
response: HttpResponse, | ||
frontend_base_url: str, | ||
action: SDKAction, | ||
action_params: dict[str, str] | None = None, | ||
**kwargs: Any | ||
) -> None: | ||
"""Assert that a response redirected to a specific frontend URL. | ||
:param response: The response to test the redirection on. | ||
:param frontend_base_url: The base URL of the frontend. | ||
:param action: The SDK action performed. | ||
:param action_params: Optional parameters for the action. | ||
:param `**kwargs`: Additional kwargs to be passed to :meth:`django.test.SimpleTestCase.assertRedirects`. | ||
""" | ||
|
||
expected_redirect_url = furl(frontend_base_url) | ||
expected_redirect_url.remove("_of_action") | ||
expected_redirect_url.remove("_of_action_params") | ||
expected_redirect_url.add( | ||
{ | ||
"_of_action": action, | ||
} | ||
) | ||
|
||
if action_params: | ||
expected_redirect_url.add({"_of_action_params": json.dumps(action_params)}) | ||
|
||
return self.assertRedirects( | ||
response, | ||
expected_redirect_url.url, | ||
**kwargs, | ||
) |
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,34 @@ | ||
# Generated by Django 3.2.21 on 2023-11-17 10:40 | ||
|
||
from django.db import migrations | ||
from django.db.migrations.state import StateApps | ||
from django.db.backends.base.schema import BaseDatabaseSchemaEditor | ||
|
||
from furl import furl | ||
|
||
|
||
def cleanup_submission_urls( | ||
apps: StateApps, schema_editor: BaseDatabaseSchemaEditor | ||
) -> None: | ||
|
||
Submission = apps.get_model("submissions", "Submission") | ||
|
||
for submission in Submission.objects.iterator(): | ||
f = furl(submission.form_url) | ||
f.remove(fragment=True) | ||
if f.path.segments[-1:] == ["startpagina"]: | ||
f.path.segments.remove("startpagina") | ||
submission.form_url = f.url | ||
|
||
submission.save() | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
("submissions", "0002_change_json_encoder"), | ||
] | ||
|
||
operations = [ | ||
migrations.RunPython(cleanup_submission_urls, migrations.RunPython.noop) | ||
] |
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
Oops, something went wrong.