Added the stored status and a dummy transaction_store() view to the accounting application.

This commit is contained in:
依瑪貓 2020-07-28 08:22:42 +08:00
parent e31119a19d
commit e90defd25d
3 changed files with 112 additions and 5 deletions

View File

@ -70,13 +70,13 @@ urlpatterns = [
path("transactions/<txn-type:type>/create",
views.transaction_edit, name="transactions.create"),
path("transactions/<txn-type:type>/store",
mia_core_views.todo, name="transactions.store"),
views.transaction_store, name="transactions.store"),
path("transactions/<txn-type:type>/<txn:transaction>",
views.transaction_show, name="transactions.show"),
path("transactions/<txn-type:type>/<txn:transaction>/edit",
views.transaction_edit, name="transactions.edit"),
path("transactions/<txn-type:type>/<txn:transaction>/update",
mia_core_views.todo, name="transactions.update"),
views.transaction_store, name="transactions.update"),
path("transactions/<txn:transaction>/delete",
mia_core_views.todo, name="transactions.delete"),
path("transactions/sort/<date:date>",

View File

@ -27,15 +27,16 @@ from django.http import HttpResponseRedirect
from django.shortcuts import render
from django.urls import reverse
from django.utils import dateformat, timezone
from django.utils.translation import pgettext
from django.views.decorators.http import require_GET
from django.utils.translation import pgettext, gettext_noop
from django.views.decorators.http import require_GET, require_POST
from mia_core.status import success_redirect
from .models import Record, Transaction, Account, RecordSummary
from .utils import ReportUrl, get_cash_accounts, get_ledger_accounts, \
find_imbalanced, find_order_holes
from mia_core.digest_auth import digest_login_required
from mia_core.period import Period
from mia_core.utils import Pagination, get_multi_lingual_search
from mia_core.utils import Pagination, get_multi_lingual_search, UrlBuilder
# noinspection PyUnusedLocal
@ -882,3 +883,26 @@ def transaction_edit(request, type, transaction=None):
return render(request, F"accounting/transactions/{type}/form.html", {
"item": transaction,
})
@require_POST
@digest_login_required
def transaction_store(request, type, transaction=None):
"""The view to store an accounting transaction.
Args:
request (HttpRequest): The request.
type (str): The transaction type.
transaction (Transaction): The transaction.
Returns:
HttpResponse: The response.
"""
if transaction is None:
transaction = Transaction()
return success_redirect(
request,
str(UrlBuilder(reverse("accounting:transactions.show",
args=(type, transaction)))
.add_param("r", request.GET.get("r"))),
gettext_noop("This transaction was saved successfully."))

83
mia_core/status.py Normal file
View File

@ -0,0 +1,83 @@
# 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
from mia_core.utils import UrlBuilder
def success_redirect(request, url, success):
id = _store(request, {"success": success})
return HttpResponseRedirect(str(UrlBuilder(url).add_param("s", id)))
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:
request (HttpRequest): The request
status (dict): The dict of the status
Returns:
str: The status 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
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:
request (HttpRequest): The request
id (str): The status ID
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):
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