diff --git a/accounting/urls.py b/accounting/urls.py index 337897a..748817c 100644 --- a/accounting/urls.py +++ b/accounting/urls.py @@ -70,13 +70,13 @@ urlpatterns = [ path("transactions//create", views.transaction_edit, name="transactions.create"), path("transactions//store", - mia_core_views.todo, name="transactions.store"), + views.transaction_store, name="transactions.store"), path("transactions//", views.transaction_show, name="transactions.show"), path("transactions///edit", views.transaction_edit, name="transactions.edit"), path("transactions///update", - mia_core_views.todo, name="transactions.update"), + views.transaction_store, name="transactions.update"), path("transactions//delete", mia_core_views.todo, name="transactions.delete"), path("transactions/sort/", diff --git a/accounting/views.py b/accounting/views.py index 33c8cf3..e05c789 100644 --- a/accounting/views.py +++ b/accounting/views.py @@ -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.")) diff --git a/mia_core/status.py b/mia_core/status.py new file mode 100644 index 0000000..bffac80 --- /dev/null +++ b/mia_core/status.py @@ -0,0 +1,83 @@ +# The core application of the Mia project. +# by imacat , 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