From ebb70896a2372b3dbbf3e2a6929486b8444e0445 Mon Sep 17 00:00:00 2001 From: RunasSudo Date: Wed, 4 Jan 2023 18:01:59 +1100 Subject: [PATCH] Add basic plugin API --- drcr/plugins.py | 28 +++++++++++++++++++++++++++ drcr/templates/chart_of_accounts.html | 3 +++ drcr/templates/index.html | 4 +++- drcr/views.py | 5 +++-- drcr/webapp.py | 19 +++++++++++++----- 5 files changed, 51 insertions(+), 8 deletions(-) create mode 100644 drcr/plugins.py diff --git a/drcr/plugins.py b/drcr/plugins.py new file mode 100644 index 0000000..a034f17 --- /dev/null +++ b/drcr/plugins.py @@ -0,0 +1,28 @@ +# DrCr: Web-based double-entry bookkeeping framework +# Copyright (C) 2022–2023 Lee Yingtong Li (RunasSudo) +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . + +from .config import PLUGINS + +import importlib + +advanced_reports = [] # list of tuplet (url, label) +account_kinds = [] # list of tuplet (id, label) +transaction_providers = [] # list of callable + +def init_plugins(): + for plugin in PLUGINS: + module = importlib.import_module(plugin) + module.plugin_init() diff --git a/drcr/templates/chart_of_accounts.html b/drcr/templates/chart_of_accounts.html index 9b0c97a..4f350aa 100644 --- a/drcr/templates/chart_of_accounts.html +++ b/drcr/templates/chart_of_accounts.html @@ -30,6 +30,9 @@ + {% for account_kind in account_kinds %} + + {% endfor %}
diff --git a/drcr/templates/index.html b/drcr/templates/index.html index 0320bf9..8407861 100644 --- a/drcr/templates/index.html +++ b/drcr/templates/index.html @@ -37,6 +37,8 @@

Advanced reports

{% endblock %} diff --git a/drcr/views.py b/drcr/views.py index 2e981ac..5ec7401 100644 --- a/drcr/views.py +++ b/drcr/views.py @@ -18,12 +18,13 @@ from flask import redirect, render_template, request from .database import db from .models import AccountConfiguration, Amount, Balance, Posting, TrialBalancer +from .plugins import account_kinds, advanced_reports from .reports import balance_sheet_report, income_statement_report from .webapp import all_transactions, app @app.route('/') def index(): - return render_template('index.html') + return render_template('index.html', advanced_reports=advanced_reports) @app.route('/chart-of-accounts') def chart_of_accounts(): @@ -34,7 +35,7 @@ def chart_of_accounts(): account_configurations = AccountConfiguration.get_all() # TODO: Handle orphans - return render_template('chart_of_accounts.html', accounts=accounts, account_configurations=account_configurations) + return render_template('chart_of_accounts.html', accounts=accounts, account_configurations=account_configurations, account_kinds=account_kinds) @app.route('/chart-of-accounts/add-kind', methods=['POST']) def account_add_kind(): diff --git a/drcr/webapp.py b/drcr/webapp.py index 1cfb869..7984c2f 100644 --- a/drcr/webapp.py +++ b/drcr/webapp.py @@ -19,6 +19,7 @@ from flask_sqlalchemy.record_queries import get_recorded_queries from .database import db from .models import Transaction +from .plugins import init_plugins, transaction_providers from .statements.models import StatementLine from .tax import aus_tax @@ -31,15 +32,23 @@ app.config['SQLALCHEMY_RECORD_QUERIES'] = app.debug db.init_app(app) def all_transactions(): - return ( - Transaction.query.all() + - [line.into_transaction() for line in StatementLine.query.filter(StatementLine.reconciliation == None)] + - [aus_tax.tax_transaction(aus_tax.taxable_income())] - ) + # All Transactions in database + transactions = db.session.scalars(db.select(Transaction)).all() + + # Unreconciled StatementLines + transactions.extend(line.into_transaction() for line in StatementLine.query.filter(StatementLine.reconciliation == None)) + + # Plugins + for transaction_provider in transaction_providers: + transactions.extend(transaction_provider()) + + return transactions from . import views from .journal import views from .statements import views + +init_plugins() from .tax import views @app.cli.command('initdb')