Added the error_redirect() utility, and revised the documentation of the stored status utilities in the Mia core application.

This commit is contained in:
依瑪貓 2020-07-28 20:01:58 +08:00
parent 958e3048bb
commit 50c06bd3d3

View File

@ -27,11 +27,11 @@ from mia_core.utils import UrlBuilder
def success_redirect(request, url, success): def success_redirect(request, url, success):
"""Redirects to a specific URL on error, with the status ID appended """Redirects to a specific URL on error, with the status ID appended
as the query parameter I<s>. The status will be loaded upon the as the query parameter "s". The status will be loaded with the
next request at L</before_dispatch>. You have to return and exit retrieve_status template tag.
from the controller manually after the redirection.
Args: Args:
request (HttpRequest): The request.
url (str): The destination URL. url (str): The destination URL.
success (str): The success text message. success (str): The success text message.
@ -42,13 +42,32 @@ def success_redirect(request, url, success):
return HttpResponseRedirect(str(UrlBuilder(url).add_param("s", id))) return HttpResponseRedirect(str(UrlBuilder(url).add_param("s", id)))
def error_redirect(request, url, form, errors_by_field):
"""Redirects to a specific URL on error, with the status ID appended
as the query parameter "s". The status will be loaded with the
retrieve_status template tag.
Args:
request (HttpRequest): The request.
url (str): The destination URL.
form (dict[str]): The received POSTed form.
errors_by_field (dict[str]): The failed fields and their corresponding
error messages.
Returns:
HttpResponseRedirect: The redirect response.
"""
id = _store(request, {"form": form, "errors_by_field": errors_by_field})
return HttpResponseRedirect(str(UrlBuilder(url).add_param("s", id)))
def _store(request, status): def _store(request, status):
"""Stores the status into the session, and returns the status ID that can """Stores the status into the session, and returns the status ID that can
be used to retrieve the status later with retrieve(). be used to retrieve the status later with retrieve().
Args: Args:
request (HttpRequest): The request request (HttpRequest): The request.
status (dict): The dict of the status status (dict): The dict of the status.
Returns: Returns:
str: The status ID str: The status ID
@ -65,8 +84,8 @@ def _retrieve(request, id):
be used to retrieve the status later with retrieve(). be used to retrieve the status later with retrieve().
Args: Args:
request (HttpRequest): The request request (HttpRequest): The request.
id (str): The status ID id (str): The status ID.
Returns: Returns:
dict: The status, or None if the status does not exist. dict: The status, or None if the status does not exist.
@ -79,6 +98,14 @@ def _retrieve(request, id):
def _new_status_id(status_store): def _new_status_id(status_store):
"""Generates and returns a new status ID that does not exist yet.
Args:
status_store (dict): The status storage.
Returns:
str: The newly-generated status ID.
"""
while True: while True:
id = "" id = ""
while len(id) < 16: while len(id) < 16: