Added the read-only view for the options.

This commit is contained in:
依瑪貓 2023-03-22 16:08:16 +08:00
parent 619540da49
commit 7066f75e72
7 changed files with 171 additions and 7 deletions

View File

@ -22,7 +22,7 @@ import json
import sqlalchemy as sa import sqlalchemy as sa
from accounting import db from accounting import db
from accounting.models import Option, Account from accounting.models import Option, Account, Currency
from accounting.utils.current_account import CurrentAccount from accounting.utils.current_account import CurrentAccount
from accounting.utils.user import get_current_user_pk from accounting.utils.user import get_current_user_pk
@ -42,6 +42,14 @@ class RecurringItem:
self.account_code: str = account_code self.account_code: str = account_code
self.description_template: str = description_template self.description_template: str = description_template
@property
def account_text(self) -> str:
"""Returns the account text.
:return: The account text.
"""
return str(Account.find_by_code(self.account_code))
class Recurring: class Recurring:
"""The recurring expenses or incomes.""" """The recurring expenses or incomes."""
@ -90,6 +98,14 @@ class Options:
""" """
self.__set_option("default_currency", value) self.__set_option("default_currency", value)
@property
def default_currency_text(self) -> str:
"""Returns the text of the default currency code.
:return: The text of the default currency code.
"""
return str(db.session.get(Currency, self.default_currency))
@property @property
def default_ie_account_code(self) -> str: def default_ie_account_code(self) -> str:
"""Returns the default account code for the income and expenses log. """Returns the default account code for the income and expenses log.
@ -107,6 +123,17 @@ class Options:
""" """
self.__set_option("default_ie_account", value) self.__set_option("default_ie_account", value)
@property
def default_ie_account_code_text(self) -> str:
"""Returns the text of the default currency code.
:return: The text of the default currency code.
"""
code: str = self.default_ie_account_code
if code == CurrentAccount.CURRENT_AL_CODE:
return str(CurrentAccount.current_assets_and_liabilities())
return str(CurrentAccount(db.session.get(Account, code)))
@property @property
def default_ie_account(self) -> CurrentAccount: def default_ie_account(self) -> CurrentAccount:
"""Returns the default account code for the income and expenses log. """Returns the default account code for the income and expenses log.

View File

@ -35,7 +35,17 @@ bp: Blueprint = Blueprint("option", __name__)
"""The view blueprint for the currency management.""" """The view blueprint for the currency management."""
@bp.get("", endpoint="form") @bp.get("", endpoint="detail")
@has_permission(can_admin)
def show_options() -> str:
"""Shows the options.
:return: The options.
"""
return render_template("accounting/option/detail.html", obj=options)
@bp.get("edit", endpoint="edit")
@has_permission(can_admin) @has_permission(can_admin)
def show_option_form() -> str: def show_option_form() -> str:
"""Shows the option form. """Shows the option form.
@ -52,7 +62,7 @@ def show_option_form() -> str:
return render_template("accounting/option/form.html", form=form) return render_template("accounting/option/form.html", form=form)
@bp.post("", endpoint="update") @bp.post("update", endpoint="update")
@has_permission(can_admin) @has_permission(can_admin)
def update_options() -> redirect: def update_options() -> redirect:
"""Updates the options. """Updates the options.
@ -63,7 +73,7 @@ def update_options() -> redirect:
form.populate_obj(options) form.populate_obj(options)
if not options.is_modified: if not options.is_modified:
flash(s(lazy_gettext("The options were not modified.")), "success") flash(s(lazy_gettext("The options were not modified.")), "success")
return redirect(inherit_next(url_for("accounting.option.form"))) return redirect(inherit_next(url_for("accounting.option.detail")))
options.commit() options.commit()
flash(s(lazy_gettext("The options are saved successfully.")), "success") flash(s(lazy_gettext("The options are saved successfully.")), "success")
return redirect(inherit_next(url_for("accounting.option.form"))) return redirect(inherit_next(url_for("accounting.option.detail")))

View File

@ -53,7 +53,7 @@ First written: 2023/1/26
</li> </li>
{% if accounting_can_admin() %} {% if accounting_can_admin() %}
<li> <li>
<a class="dropdown-item {% if request.endpoint and request.endpoint.startswith("accounting.option.") %} active {% endif %}" href="{{ url_for("accounting.option.form") }}"> <a class="dropdown-item {% if request.endpoint and request.endpoint.startswith("accounting.option.") %} active {% endif %}" href="{{ url_for("accounting.option.detail") }}">
<i class="fa-solid fa-gear"></i> <i class="fa-solid fa-gear"></i>
{{ A_("Settings") }} {{ A_("Settings") }}
</a> </a>

View File

@ -0,0 +1,63 @@
{#
The Mia! Accounting Flask Project
detail.html: The option detail
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.
Author: imacat@mail.imacat.idv.tw (imacat)
First written: 2023/3/22
#}
{% extends "accounting/base.html" %}
{% block header %}{% block title %}{{ A_("Settings") }}{% endblock %}{% endblock %}
{% block content %}
<div class="mb-3 accounting-toolbar">
<a class="btn btn-primary d-none d-md-inline" role="button" href="{{ url_for("accounting.option.edit")|accounting_inherit_next }}">
<i class="fa-solid fa-pen-to-square"></i>
{{ A_("Edit") }}
</a>
</div>
<div class="d-md-none accounting-material-fab">
<a class="btn btn-primary" role="button" href="{{ url_for("accounting.option.edit")|accounting_inherit_next }}">
<i class="fa-solid fa-pen-to-square"></i>
</a>
</div>
<div id="accounting-default-currency" class="form-control mb-3 accounting-material-text-field accounting-not-empty">
<label class="form-label" for="accounting-default-currency">{{ A_("Default Currency") }}</label>
<div>{{ obj.default_currency_text }}</div>
</div>
<div id="accounting-default-ie-account" class="form-control mb-3 accounting-material-text-field accounting-not-empty">
<label class="form-label" for="accounting-default-ie-account">{{ A_("Default Account for the Income and Expenses Log") }}</label>
<div>{{ obj.default_ie_account_code_text }}</div>
</div>
{% with expense_income = "expense",
label = A_("Expense"),
recurring_items = obj.recurring.expenses %}
{% include "accounting/option/include/detail-recurring-expense-income.html" %}
{% endwith %}
{% with expense_income = "income",
label = A_("Income"),
recurring_items = obj.recurring.incomes %}
{% include "accounting/option/include/detail-recurring-expense-income.html" %}
{% endwith %}
{% endblock %}

View File

@ -29,7 +29,14 @@ First written: 2023/3/22
{% block content %} {% block content %}
<form id="accounting-form" action="{{ url_for("accounting.option.form") }}" method="post" data-recurring-item-template="{{ form.recurring.item_template }}"> <div class="mb-3 accounting-toolbar">
<a class="btn btn-primary" role="button" href="{{ url_for("accounting.option.detail")|accounting_inherit_next }}">
<i class="fa-solid fa-circle-chevron-left"></i>
<span class="d-none d-md-inline">{{ A_("Back") }}</span>
</a>
</div>
<form id="accounting-form" action="{{ url_for("accounting.option.update") }}" method="post" data-recurring-item-template="{{ form.recurring.item_template }}">
{{ form.csrf_token }} {{ form.csrf_token }}
{% if request.args.next %} {% if request.args.next %}
<input type="hidden" name="next" value="{{ request.args.next }}"> <input type="hidden" name="next" value="{{ request.args.next }}">

View File

@ -0,0 +1,29 @@
{#
The Mia! Accounting Flask Project
detail-recurring-expense-income.html: The recurring expense or income in the option detail
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.
Author: imacat@mail.imacat.idv.tw (imacat)
First written: 2023/3/22
#}
<div id="accounting-recurring-{{ expense_income }}" class="form-control mb-3 accounting-material-text-field accounting-not-empty">
<label class="form-label" for="accounting-recurring-{{ expense_income }}">{{ label }}</label>
<ul class="list-group mb-2 mt-2">
{% for item in recurring_items %}
{% include "accounting/option/include/detail-recurring-item.html" %}
{% endfor %}
</ul>
</div>

View File

@ -0,0 +1,28 @@
{#
The Mia! Accounting Flask Project
detail-recurring-item.html: The recurring item in the option detail
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.
Author: imacat@mail.imacat.idv.tw (imacat)
First written: 2023/3/22
#}
{# <ul> For SonarQube not to complain about incorrect HTML #}
<li class="list-group-item list-group-item-action">
<div class="small">{{ item.account_text }}</div>
<div>{{ item.name }}</div>
<div class="small">{{ item.description_template }}</div>
</li>
{# </ul> For SonarQube not to complain about incorrect HTML #}