Renamed mia_core.status to mia_core.stored_post, and changed the stored status to stored POST data in the Mia core application.

This commit is contained in:
依瑪貓 2020-08-09 13:48:00 +08:00
parent 049b9371e5
commit 0ee5eeaacf
3 changed files with 35 additions and 37 deletions

View File

@ -32,7 +32,7 @@ from django.utils import timezone
from django.utils.translation import gettext as _ from django.utils.translation import gettext as _
from mia_core.period import Period from mia_core.period import Period
from mia_core.status import get_previous_post from mia_core.stored_post import get_previous_post
from mia_core.templatetags.mia_core import smart_month from mia_core.templatetags.mia_core import smart_month
from mia_core.utils import new_pk from mia_core.utils import new_pk
from .forms import TransactionForm, RecordForm from .forms import TransactionForm, RecordForm

View File

@ -38,7 +38,7 @@ from django.views.generic import RedirectView, ListView, DetailView, DeleteView
from mia_core.digest_auth import login_required from mia_core.digest_auth import login_required
from mia_core.period import Period from mia_core.period import Period
from mia_core.status import error_redirect, get_previous_post from mia_core.stored_post import error_redirect, get_previous_post
from mia_core.utils import Pagination, get_multi_lingual_search, UrlBuilder, \ from mia_core.utils import Pagination, get_multi_lingual_search, UrlBuilder, \
strip_form, new_pk, PaginationException strip_form, new_pk, PaginationException
from .forms import AccountForm from .forms import AccountForm

View File

@ -15,7 +15,7 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
"""The session-based status management of the Mia core application. """The session-based POST data storage management of the Mia core application.
""" """
import random import random
@ -24,85 +24,83 @@ from django.http import HttpResponseRedirect
from .utils import UrlBuilder from .utils import UrlBuilder
STORAGE_KEY = "stored_post"
def error_redirect(request, url, form):
"""Redirects to a specific URL on error, with the status ID appended def error_redirect(request, url, post):
as the query parameter "s". The status will be loaded with the """Redirects to a specific URL on error, with the POST data ID appended
retrieve_status template tag. as the query parameter "s". The POST data can be loaded with the
get_previous_post() utility.
Args: Args:
request (HttpRequest): The request. request (HttpRequest): The request.
url (str): The destination URL. url (str): The destination URL.
form (dict[str]): The received POSTed form. post (dict[str]): The POST data.
Returns: Returns:
HttpResponseRedirect: The redirect response. HttpResponseRedirect: The redirect response.
""" """
status_id = _store(request, {"form": form}) post_id = _store(request, post)
return HttpResponseRedirect(str(UrlBuilder(url).query(s=status_id))) return HttpResponseRedirect(str(UrlBuilder(url).query(s=post_id)))
def get_previous_post(request): def get_previous_post(request):
"""Retrieves the previously-stored status. """Retrieves the previously-stored POST data.
Args: Args:
request (HttpRequest): The request. request (HttpRequest): The request.
Returns: Returns:
dict: The previously-stored status. dict: The previously-stored POST data.
""" """
if "s" not in request.GET: if "s" not in request.GET:
return None return None
status = _retrieve(request, request.GET["s"]) return _retrieve(request, request.GET["s"])
if "form" not in status:
return None
return status["form"]
def _store(request, status): def _store(request, post):
"""Stores the status into the session, and returns the status ID that can """Stores the POST data into the session, and returns the POST data ID that
be used to retrieve the status later with retrieve(). can be used to retrieve it later with _retrieve().
Args: Args:
request (HttpRequest): The request. request (HttpRequest): The request.
status (dict): The dict of the status. post (dict): The POST data.
Returns: Returns:
str: The status ID str: The POST data ID
""" """
if "stored_status" not in request.session: if STORAGE_KEY not in request.session:
request.session["stored_status"] = {} request.session[STORAGE_KEY] = {}
id = _new_status_id(request.session["stored_status"]) id = _new_post_id(request.session[STORAGE_KEY])
request.session["stored_status"][id] = status request.session[STORAGE_KEY][id] = post
return id return id
def _retrieve(request, id): def _retrieve(request, id):
"""Stores the status into the session, and returns the status ID that can """Retrieves the POST data from the storage.
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 POST data ID.
Returns: Returns:
dict: The status, or None if the status does not exist. dict: The POST data, or None if the corresponding data does not exist.
""" """
if "stored_status" not in request.session: if STORAGE_KEY not in request.session:
return None return None
if id not in request.session["stored_status"]: if id not in request.session[STORAGE_KEY]:
return None return None
return request.session["stored_status"][id] return request.session[STORAGE_KEY][id]
def _new_status_id(status_store): def _new_post_id(post_store):
"""Generates and returns a new status ID that does not exist yet. """Generates and returns a new POST ID that does not exist yet.
Args: Args:
status_store (dict): The status storage. post_store (dict): The POST storage.
Returns: Returns:
str: The newly-generated status ID. str: The newly-generated POST ID.
""" """
while True: while True:
id = "" id = ""
@ -116,5 +114,5 @@ def _new_status_id(status_store):
id = id + chr(ord("0") + (n - 52)) id = id + chr(ord("0") + (n - 52))
else: else:
id = id + "-_."[n - 62] id = id + "-_."[n - 62]
if id not in status_store: if id not in post_store:
return id return id