diff --git a/src/accounting/currency/views.py b/src/accounting/currency/views.py index 7abe0d6..56e3a3c 100644 --- a/src/accounting/currency/views.py +++ b/src/accounting/currency/views.py @@ -160,6 +160,9 @@ def delete_currency(currency: Currency) -> redirect: :return: The redirection to the currency list on success, or the currency detail on error. """ + if not currency.can_delete: + flash(s(lazy_gettext("The currency cannot be deleted.")), "error") + return redirect(inherit_next(__get_detail_uri(currency))) currency.delete() db.session.commit() flash(s(lazy_gettext("The currency is deleted successfully.")), "success") diff --git a/src/accounting/models.py b/src/accounting/models.py index 7c8ddab..c447623 100644 --- a/src/accounting/models.py +++ b/src/accounting/models.py @@ -432,6 +432,17 @@ class Currency(db.Model): return True return False + @property + def can_delete(self) -> bool: + """Returns whether the currency can be deleted. + + :return: True if the currency can be deleted, or False otherwise. + """ + from accounting.template_globals import default_currency_code + if self.code == default_currency_code(): + return False + return len(self.line_items) == 0 + def delete(self) -> None: """Deletes the currency. diff --git a/src/accounting/templates/accounting/currency/detail.html b/src/accounting/templates/accounting/currency/detail.html index 5597386..0b3ca6d 100644 --- a/src/accounting/templates/accounting/currency/detail.html +++ b/src/accounting/templates/accounting/currency/detail.html @@ -37,10 +37,17 @@ First written: 2023/2/6 {% endif %} {% if accounting_can_edit() %} - + {% if obj.can_delete %} + + {% else %} + + {% endif %} {% endif %} @@ -52,7 +59,7 @@ First written: 2023/2/6 {% endif %} -{% if accounting_can_edit() %} +{% if accounting_can_edit() and obj.can_delete %}