Added the "accounting.utils.next_uri" utilities to fixed how the next URI works.
This commit is contained in:
parent
2964f206a6
commit
241ad337c8
@ -106,4 +106,9 @@ def init_app(app: Flask, user_utils: AbstractUserUtils,
|
||||
from . import account
|
||||
account.init_app(app, bp)
|
||||
|
||||
from .utils.next_url import append_next, inherit_next, or_next
|
||||
bp.add_app_template_filter(append_next, "append_next")
|
||||
bp.add_app_template_filter(inherit_next, "inherit_next")
|
||||
bp.add_app_template_filter(or_next, "or_next")
|
||||
|
||||
app.register_blueprint(bp)
|
||||
|
@ -26,6 +26,7 @@ from werkzeug.datastructures import ImmutableMultiDict
|
||||
from accounting.database import db
|
||||
from accounting.locale import lazy_gettext
|
||||
from accounting.models import Account, BaseAccount
|
||||
from accounting.utils.next_url import inherit_next, or_next
|
||||
from accounting.utils.pagination import Pagination
|
||||
from accounting.utils.permission import can_view, has_permission, can_edit
|
||||
from .forms import AccountForm, sort_accounts_in
|
||||
@ -79,13 +80,14 @@ def add_account() -> redirect:
|
||||
for error in form.errors[key]:
|
||||
flash(error, "error")
|
||||
session["form"] = urlencode(list(request.form.items()))
|
||||
return redirect(url_for("accounting.account.create"))
|
||||
return redirect(inherit_next(url_for("accounting.account.create")))
|
||||
account: Account = Account()
|
||||
form.populate_obj(account)
|
||||
db.session.add(account)
|
||||
db.session.commit()
|
||||
flash(lazy_gettext("The account is added successfully"), "success")
|
||||
return redirect(url_for("accounting.account.detail", account=account))
|
||||
return redirect(inherit_next(url_for("accounting.account.detail",
|
||||
account=account)))
|
||||
|
||||
|
||||
@bp.get("/<account:account>", endpoint="detail")
|
||||
@ -130,16 +132,19 @@ def update_account(account: Account) -> redirect:
|
||||
for error in form.errors[key]:
|
||||
flash(error, "error")
|
||||
session["form"] = urlencode(list(request.form.items()))
|
||||
return redirect(url_for("accounting.account.edit", account=account))
|
||||
return redirect(inherit_next(url_for("accounting.account.edit",
|
||||
account=account)))
|
||||
with db.session.no_autoflush:
|
||||
form.populate_obj(account)
|
||||
if not db.session.is_modified(account):
|
||||
flash(lazy_gettext("The account was not modified."), "success")
|
||||
return redirect(url_for("accounting.account.detail", account=account))
|
||||
return redirect(inherit_next(url_for("accounting.account.detail",
|
||||
account=account)))
|
||||
form.post_update(account)
|
||||
db.session.commit()
|
||||
flash(lazy_gettext("The account is updated successfully."), "success")
|
||||
return redirect(url_for("accounting.account.detail", account=account))
|
||||
return redirect(inherit_next(url_for("accounting.account.detail",
|
||||
account=account)))
|
||||
|
||||
|
||||
@bp.post("/<account:account>/delete", endpoint="delete")
|
||||
@ -156,4 +161,4 @@ def delete_account(account: Account) -> redirect:
|
||||
sort_accounts_in(account.base_code, account.id)
|
||||
db.session.commit()
|
||||
flash(lazy_gettext("The account is deleted successfully."), "success")
|
||||
return redirect(url_for("accounting.account.list"))
|
||||
return redirect(or_next(url_for("accounting.account.list")))
|
||||
|
@ -26,12 +26,12 @@ First written: 2023/1/31
|
||||
{% block content %}
|
||||
|
||||
<div class="btn-group mb-3">
|
||||
<a class="btn btn-primary" href="{{ request.args.get("next") or url_for("accounting.account.list") }}">
|
||||
<a class="btn btn-primary" href="{{ url_for("accounting.account.list")|or_next }}">
|
||||
<i class="fa-solid fa-circle-chevron-left"></i>
|
||||
{{ A_("Back") }}
|
||||
</a>
|
||||
{% if can_edit_accounting() %}
|
||||
<a class="btn btn-primary d-none d-md-inline" href="{{ url_for("accounting.account.edit", account=obj) + ("?next=" + request.args["next"] if "next" in request.args else "") }}">
|
||||
<a class="btn btn-primary d-none d-md-inline" href="{{ url_for("accounting.account.edit", account=obj)|inherit_next }}">
|
||||
<i class="fa-solid fa-gear"></i>
|
||||
{{ A_("Settings") }}
|
||||
</a>
|
||||
@ -44,7 +44,7 @@ First written: 2023/1/31
|
||||
|
||||
{% if can_edit_accounting() %}
|
||||
<div class="d-md-none material-fab">
|
||||
<a class="btn btn-primary" href="{{ url_for("accounting.account.edit", account=obj) + ("?next=" + request.args["next"] if "next" in request.args else "") }}">
|
||||
<a class="btn btn-primary" href="{{ url_for("accounting.account.edit", account=obj)|inherit_next }}">
|
||||
<i class="fa-solid fa-pen-to-square"></i>
|
||||
</a>
|
||||
</div>
|
||||
@ -53,6 +53,9 @@ First written: 2023/1/31
|
||||
{% if can_edit_accounting() %}
|
||||
<form id="delete-form" action="{{ url_for("accounting.account.delete", account=obj) }}" method="post">
|
||||
<input id="csrf_token" type="hidden" name="csrf_token" value="{{ csrf_token() }}">
|
||||
{% if "next" in request.args %}
|
||||
<input type="hidden" name="next" value="{{ request.args["next"] }}">
|
||||
{% endif %}
|
||||
<div class="modal fade" id="delete-modal" tabindex="-1" aria-labelledby="delete-model-label" aria-hidden="true">
|
||||
<div class="modal-dialog">
|
||||
<div class="modal-content">
|
||||
|
@ -23,6 +23,6 @@ First written: 2023/2/1
|
||||
|
||||
{% block header %}{% block title %}{{ A_("%(account)s Settings", account=account) }}{% endblock %}{% endblock %}
|
||||
|
||||
{% block back_url %}{{ url_for("accounting.account.detail", account=account) + ("?next=" + request.args["next"] if "next" in request.args else "") }}{% endblock %}
|
||||
{% block back_url %}{{ url_for("accounting.account.detail", account=account)|inherit_next }}{% endblock %}
|
||||
|
||||
{% block action_url %}{{ url_for("accounting.account.update", account=account) }}{% endblock %}
|
||||
|
@ -27,14 +27,14 @@ First written: 2023/1/30
|
||||
|
||||
{% if can_edit_accounting() %}
|
||||
<div class="btn-group mb-3 d-none d-md-block">
|
||||
<a class="btn btn-primary" href="{{ url_for("accounting.account.create") }}">
|
||||
<a class="btn btn-primary" href="{{ url_for("accounting.account.create")|append_next }}">
|
||||
<i class="fa-solid fa-user-plus"></i>
|
||||
{{ A_("New") }}
|
||||
</a>
|
||||
</div>
|
||||
|
||||
<div class="d-md-none material-fab">
|
||||
<a class="btn btn-primary" href="{{ url_for("accounting.account.create") }}">
|
||||
<a class="btn btn-primary" href="{{ url_for("accounting.account.create")|append_next }}">
|
||||
<i class="fa-solid fa-plus"></i>
|
||||
</a>
|
||||
</div>
|
||||
@ -61,7 +61,7 @@ First written: 2023/1/30
|
||||
|
||||
<div class="list-group">
|
||||
{% for item in list %}
|
||||
<a class="list-group-item list-group-item-action" href="{{ url_for("accounting.account.detail", account=item) }}">
|
||||
<a class="list-group-item list-group-item-action" href="{{ url_for("accounting.account.detail", account=item)|append_next }}">
|
||||
{{ item }}
|
||||
</a>
|
||||
{% endfor %}
|
||||
|
75
src/accounting/utils/next_url.py
Normal file
75
src/accounting/utils/next_url.py
Normal file
@ -0,0 +1,75 @@
|
||||
# The Mia! Accounting Flask Project.
|
||||
# Author: imacat@mail.imacat.idv.tw (imacat), 2023/2/1
|
||||
|
||||
# Copyright (c) 2023 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 utilities to handle the next URL.
|
||||
|
||||
This module should not import any other module from the application.
|
||||
|
||||
"""
|
||||
from urllib.parse import urlparse, parse_qsl, ParseResult, urlencode, \
|
||||
urlunparse
|
||||
|
||||
from flask import request
|
||||
|
||||
|
||||
def append_next(uri: str) -> str:
|
||||
"""Appends the current URI as the next URI to the query argument.
|
||||
|
||||
:param uri: The URI.
|
||||
:return: The URI with the current URI appended as the next URI.
|
||||
"""
|
||||
next_uri: str = request.full_path if request.query_string else request.path
|
||||
return __set_next(uri, next_uri)
|
||||
|
||||
|
||||
def inherit_next(uri: str) -> str:
|
||||
"""Inherits the current next URI to the query argument, if exists.
|
||||
|
||||
:param uri: The URI.
|
||||
:return: The URI with the current next URI added at the query argument.
|
||||
"""
|
||||
next_uri: str | None = request.form.get("next") \
|
||||
if request.method == "POST" else request.args.get("next")
|
||||
if next_uri is None:
|
||||
return uri
|
||||
return __set_next(uri, next_uri)
|
||||
|
||||
|
||||
def or_next(uri: str) -> str:
|
||||
"""Returns the next URI, if exists, or the supplied URI.
|
||||
|
||||
:param uri: The URI.
|
||||
:return: The next URI or the supplied URI.
|
||||
"""
|
||||
next_uri: str | None = request.form.get("next") \
|
||||
if request.method == "POST" else request.args.get("next")
|
||||
return uri if next_uri is None else next_uri
|
||||
|
||||
|
||||
def __set_next(uri: str, next_uri: str) -> str:
|
||||
"""Sets the next URI to the query arguments.
|
||||
|
||||
:param uri: The URI.
|
||||
:param next_uri: The next URI.
|
||||
:return: The URI with the next URI set.
|
||||
"""
|
||||
uri_p: ParseResult = urlparse(uri)
|
||||
params: list[tuple[str, str]] = parse_qsl(uri_p.query)
|
||||
params = [x for x in params if x[0] == "next"]
|
||||
params.append(("next", next_uri))
|
||||
parts: list[str] = list(uri_p)
|
||||
parts[4] = urlencode(params)
|
||||
return urlunparse(parts)
|
Loading…
Reference in New Issue
Block a user