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 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.utils import new_pk
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.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, \
strip_form, new_pk, PaginationException
from .forms import AccountForm

View File

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