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.
|
|
|
|
|
2020-08-09 13:48:00 +08:00
|
|
|
"""The session-based POST data storage management of the Mia core application.
|
2020-07-28 08:22:42 +08:00
|
|
|
|
|
|
|
"""
|
|
|
|
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
|
|
|
|
2020-08-09 13:48:00 +08:00
|
|
|
STORAGE_KEY = "stored_post"
|
2020-07-28 08:22:42 +08:00
|
|
|
|
2020-08-09 13:48:00 +08:00
|
|
|
|
|
|
|
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.
|
2020-07-28 20:01:58 +08:00
|
|
|
|
|
|
|
Args:
|
|
|
|
request (HttpRequest): The request.
|
|
|
|
url (str): The destination URL.
|
2020-08-09 13:48:00 +08:00
|
|
|
post (dict[str]): The POST data.
|
2020-07-28 20:01:58 +08:00
|
|
|
|
|
|
|
Returns:
|
|
|
|
HttpResponseRedirect: The redirect response.
|
|
|
|
"""
|
2020-08-09 13:48:00 +08:00
|
|
|
post_id = _store(request, post)
|
|
|
|
return HttpResponseRedirect(str(UrlBuilder(url).query(s=post_id)))
|
2020-07-28 20:01:58 +08:00
|
|
|
|
|
|
|
|
2020-08-09 11:52:12 +08:00
|
|
|
def get_previous_post(request):
|
2020-08-09 13:48:00 +08:00
|
|
|
"""Retrieves the previously-stored POST data.
|
2020-08-09 11:52:12 +08:00
|
|
|
|
|
|
|
Args:
|
|
|
|
request (HttpRequest): The request.
|
|
|
|
|
|
|
|
Returns:
|
2020-08-09 13:48:00 +08:00
|
|
|
dict: The previously-stored POST data.
|
2020-08-09 11:52:12 +08:00
|
|
|
"""
|
|
|
|
if "s" not in request.GET:
|
|
|
|
return None
|
2020-08-09 13:48:00 +08:00
|
|
|
return _retrieve(request, request.GET["s"])
|
2020-08-09 11:52:12 +08:00
|
|
|
|
|
|
|
|
2020-08-09 13:48:00 +08:00
|
|
|
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().
|
2020-07-28 08:22:42 +08:00
|
|
|
|
|
|
|
Args:
|
2020-07-28 20:01:58 +08:00
|
|
|
request (HttpRequest): The request.
|
2020-08-09 13:48:00 +08:00
|
|
|
post (dict): The POST data.
|
2020-07-28 08:22:42 +08:00
|
|
|
|
|
|
|
Returns:
|
2020-08-09 13:48:00 +08:00
|
|
|
str: The POST data ID
|
2020-07-28 08:22:42 +08:00
|
|
|
"""
|
2020-08-09 13:48:00 +08:00
|
|
|
if STORAGE_KEY not in request.session:
|
|
|
|
request.session[STORAGE_KEY] = {}
|
2020-08-09 13:53:47 +08:00
|
|
|
post_id = _new_post_id(request.session[STORAGE_KEY])
|
|
|
|
request.session[STORAGE_KEY][post_id] = post
|
|
|
|
return post_id
|
2020-07-28 08:22:42 +08:00
|
|
|
|
|
|
|
|
2020-08-09 13:53:47 +08:00
|
|
|
def _retrieve(request, post_id):
|
2020-08-09 13:48:00 +08:00
|
|
|
"""Retrieves the POST data from the storage.
|
2020-07-28 08:22:42 +08:00
|
|
|
|
|
|
|
Args:
|
2020-07-28 20:01:58 +08:00
|
|
|
request (HttpRequest): The request.
|
2020-08-09 13:53:47 +08:00
|
|
|
post_id (str): The POST data ID.
|
2020-07-28 08:22:42 +08:00
|
|
|
|
|
|
|
Returns:
|
2020-08-09 13:48:00 +08:00
|
|
|
dict: The POST data, or None if the corresponding data does not exist.
|
2020-07-28 08:22:42 +08:00
|
|
|
"""
|
2020-08-09 13:48:00 +08:00
|
|
|
if STORAGE_KEY not in request.session:
|
2020-07-28 08:22:42 +08:00
|
|
|
return None
|
2020-08-09 13:53:47 +08:00
|
|
|
if post_id not in request.session[STORAGE_KEY]:
|
2020-07-28 08:22:42 +08:00
|
|
|
return None
|
2020-08-09 13:53:47 +08:00
|
|
|
return request.session[STORAGE_KEY][post_id]
|
2020-07-28 08:22:42 +08:00
|
|
|
|
|
|
|
|
2020-08-09 13:48:00 +08:00
|
|
|
def _new_post_id(post_store):
|
|
|
|
"""Generates and returns a new POST ID that does not exist yet.
|
2020-07-28 20:01:58 +08:00
|
|
|
|
|
|
|
Args:
|
2020-08-09 13:48:00 +08:00
|
|
|
post_store (dict): The POST storage.
|
2020-07-28 20:01:58 +08:00
|
|
|
|
|
|
|
Returns:
|
2020-08-09 13:48:00 +08:00
|
|
|
str: The newly-generated POST ID.
|
2020-07-28 20:01:58 +08:00
|
|
|
"""
|
2020-07-28 08:22:42 +08:00
|
|
|
while True:
|
2020-08-09 13:53:47 +08:00
|
|
|
post_id = ""
|
|
|
|
while len(post_id) < 16:
|
2020-07-28 08:22:42 +08:00
|
|
|
n = random.randint(1, 64)
|
|
|
|
if n < 26:
|
2020-08-09 13:53:47 +08:00
|
|
|
post_id = post_id + chr(ord("a") + n)
|
2020-07-28 08:22:42 +08:00
|
|
|
elif n < 52:
|
2020-08-09 13:53:47 +08:00
|
|
|
post_id = post_id + chr(ord("a") + (n - 26))
|
2020-07-28 08:22:42 +08:00
|
|
|
elif n < 62:
|
2020-08-09 13:53:47 +08:00
|
|
|
post_id = post_id + chr(ord("0") + (n - 52))
|
2020-07-28 08:22:42 +08:00
|
|
|
else:
|
2020-08-09 13:53:47 +08:00
|
|
|
post_id = post_id + "-_."[n - 62]
|
|
|
|
if post_id not in post_store:
|
|
|
|
return post_id
|