2020-07-28 08:22:42 +08:00
|
|
|
# The core application of the Mia project.
|
|
|
|
# by imacat <imacat@mail.imacat.idv.tw>, 2020/7/24
|
|
|
|
|
|
|
|
# Copyright (c) 2020 imacat.
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
|
|
|
"""The session-based status management of the Mia core application.
|
|
|
|
|
|
|
|
"""
|
|
|
|
import random
|
|
|
|
|
|
|
|
from django.http import HttpResponseRedirect
|
|
|
|
|
2020-08-01 23:56:41 +08:00
|
|
|
from .utils import UrlBuilder
|
2020-07-28 08:22:42 +08:00
|
|
|
|
|
|
|
|
|
|
|
def success_redirect(request, url, success):
|
2020-07-28 19:34:14 +08:00
|
|
|
"""Redirects to a specific URL on error, with the status ID appended
|
2020-07-28 20:01:58 +08:00
|
|
|
as the query parameter "s". The status will be loaded with the
|
|
|
|
retrieve_status template tag.
|
2020-07-28 19:34:14 +08:00
|
|
|
|
|
|
|
Args:
|
2020-07-28 20:01:58 +08:00
|
|
|
request (HttpRequest): The request.
|
2020-07-28 19:34:14 +08:00
|
|
|
url (str): The destination URL.
|
|
|
|
success (str): The success text message.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
HttpResponseRedirect: The redirect response.
|
|
|
|
"""
|
2020-07-28 08:22:42 +08:00
|
|
|
id = _store(request, {"success": success})
|
2020-08-01 23:56:41 +08:00
|
|
|
return HttpResponseRedirect(str(UrlBuilder(url).set("s", id)))
|
2020-07-28 08:22:42 +08:00
|
|
|
|
|
|
|
|
2020-08-01 23:56:41 +08:00
|
|
|
def error_redirect(request, url, form):
|
2020-07-28 20:01:58 +08:00
|
|
|
"""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.
|
|
|
|
"""
|
2020-08-01 23:56:41 +08:00
|
|
|
id = _store(request, {"form": form})
|
|
|
|
return HttpResponseRedirect(str(UrlBuilder(url).set("s", id)))
|
2020-07-28 20:01:58 +08:00
|
|
|
|
|
|
|
|
2020-07-28 22:22:06 +08:00
|
|
|
def retrieve_status(request):
|
|
|
|
"""Retrieves the previously-stored status.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
request (HttpRequest): The request.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
dict: The previously-stored status.
|
|
|
|
"""
|
|
|
|
if "s" not in request.GET:
|
|
|
|
return None
|
|
|
|
return _retrieve(request, request.GET["s"])
|
|
|
|
|
|
|
|
|
2020-07-28 08:22:42 +08:00
|
|
|
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().
|
|
|
|
|
|
|
|
Args:
|
2020-07-28 20:01:58 +08:00
|
|
|
request (HttpRequest): The request.
|
|
|
|
status (dict): The dict of the status.
|
2020-07-28 08:22:42 +08:00
|
|
|
|
|
|
|
Returns:
|
|
|
|
str: The status ID
|
|
|
|
"""
|
|
|
|
if "stored_status" not in request.session:
|
|
|
|
request.session["stored_status"] = {}
|
|
|
|
id = _new_status_id(request.session["stored_status"])
|
2020-07-28 08:43:30 +08:00
|
|
|
request.session["stored_status"][id] = status
|
2020-07-28 08:22:42 +08:00
|
|
|
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().
|
|
|
|
|
|
|
|
Args:
|
2020-07-28 20:01:58 +08:00
|
|
|
request (HttpRequest): The request.
|
|
|
|
id (str): The status ID.
|
2020-07-28 08:22:42 +08:00
|
|
|
|
|
|
|
Returns:
|
|
|
|
dict: The status, or None if the status does not exist.
|
|
|
|
"""
|
|
|
|
if "stored_status" not in request.session:
|
|
|
|
return None
|
|
|
|
if id not in request.session["stored_status"]:
|
|
|
|
return None
|
|
|
|
return request.session["stored_status"][id]
|
|
|
|
|
|
|
|
|
|
|
|
def _new_status_id(status_store):
|
2020-07-28 20:01:58 +08:00
|
|
|
"""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.
|
|
|
|
"""
|
2020-07-28 08:22:42 +08:00
|
|
|
while True:
|
|
|
|
id = ""
|
|
|
|
while len(id) < 16:
|
|
|
|
n = random.randint(1, 64)
|
|
|
|
if n < 26:
|
|
|
|
id = id + chr(ord("a") + n)
|
|
|
|
elif n < 52:
|
|
|
|
id = id + chr(ord("a") + (n - 26))
|
|
|
|
elif n < 62:
|
|
|
|
id = id + chr(ord("0") + (n - 52))
|
|
|
|
else:
|
|
|
|
id = id + "-_."[n - 62]
|
|
|
|
if id not in status_store:
|
|
|
|
return id
|