From e4d7404a452dc2f050e46c4f82ed3b55aefe91f8 Mon Sep 17 00:00:00 2001 From: Piotr Dobrowolski Date: Thu, 7 Mar 2024 18:51:59 +0100 Subject: [PATCH] add admin summary view, fix product id calculation --- bitvend/admin.py | 25 ++++++++++++++++++++--- bitvend/mdb.py | 2 +- bitvend/templates/admin/base.html | 15 ++++++++++++++ bitvend/templates/admin/manual.html | 14 ++++++++++--- bitvend/templates/admin/transactions.html | 2 +- bitvend/templates/base.html | 5 ++++- mdb/device.py | 5 ++--- 7 files changed, 56 insertions(+), 12 deletions(-) create mode 100644 bitvend/templates/admin/base.html diff --git a/bitvend/admin.py b/bitvend/admin.py index 60ed5ea..5736490 100644 --- a/bitvend/admin.py +++ b/bitvend/admin.py @@ -10,11 +10,13 @@ from flask import ( url_for, session, abort, + current_app, ) from flask_login import current_user, fresh_login_required from bitvend import dev, spaceauth from bitvend.models import db, Transaction +from sqlalchemy import func from bitvend.forms import ManualForm from spaceauth import cap_required @@ -25,7 +27,8 @@ def get_user_groups(): groups_ts = session.get("groups_ts", 0) if not groups or groups_uid != current_user.uid or time.time() - groups_ts >= 60.0: - groups = spaceauth.remote.get("/api/1/userinfo").data.get("groups") + resp = spaceauth.remote.get("/api/1/userinfo") + groups = resp.data.get("groups") session["groups"] = groups session["groups_ts"] = time.time() @@ -39,6 +42,9 @@ def admin_required(fn): @wraps(fn) def wrapped(*args, **kwargs): groups = get_user_groups() + if not groups: + return current_app.login_manager.unauthorized() + if "vending-admin" not in groups: abort(403) @@ -50,7 +56,14 @@ def admin_required(fn): bp = Blueprint("admin", __name__) -@bp.route("/manual", methods=["GET", "POST"]) +@bp.route("/") +@fresh_login_required +@admin_required +def admin_index(): + return redirect(url_for("admin.manual")) + + +@bp.route("/manual/", methods=["GET", "POST"]) @fresh_login_required @admin_required def manual(): @@ -60,7 +73,13 @@ def manual(): db.session.commit() flash("Operation successful.", "success") - return render_template("admin/manual.html", form=form) + return render_template( + "admin/manual.html", + form=form, + summary=db.session.query(func.sum(Transaction.amount)) + .filter(Transaction.uid != "__bitcoin__") + .scalar(), + ) @bp.route("/transactions/", defaults={"page": 1}) diff --git a/bitvend/mdb.py b/bitvend/mdb.py index a5142dd..f53f2eb 100644 --- a/bitvend/mdb.py +++ b/bitvend/mdb.py @@ -31,7 +31,7 @@ class BitvendCashlessMDBDevice(CashlessMDBDevice): if self.current_tx_id: with self.app.app_context(): tx = Transaction.query.get(self.current_tx_id) - tx.product_id = product + tx.product_id = product + 9 tx.product_value = value if tx.amount is None: diff --git a/bitvend/templates/admin/base.html b/bitvend/templates/admin/base.html new file mode 100644 index 0000000..318c3dd --- /dev/null +++ b/bitvend/templates/admin/base.html @@ -0,0 +1,15 @@ +{% extends "base.html" %} + +{% block body %} +
+
+
+ {% macro link(endpoint) %}{{ caller() }}{% endmacro %} + {% call link('admin.manual') %}Dashboard{% endcall %} + {% call link('admin.transactions') %}Transactions{% endcall %} +
+ +
+
{% block content %}{% endblock %}
+
+{% endblock %} diff --git a/bitvend/templates/admin/manual.html b/bitvend/templates/admin/manual.html index 51d8a82..4cb9d70 100644 --- a/bitvend/templates/admin/manual.html +++ b/bitvend/templates/admin/manual.html @@ -1,9 +1,17 @@ -{% extends "base.html" %} +{% extends "admin/base.html" %} -{% from "_helpers.html" import render_field, render_submit %} +{% from "_helpers.html" import render_field, render_submit, format_currency %} {% block content %} -

Manual transaction

+
+
+
+

Cash summary {{ format_currency(summary) }}

+
+
+
+ +

Manual transaction

Creates manual transaction for current user. Use this when filling machine with money.

diff --git a/bitvend/templates/admin/transactions.html b/bitvend/templates/admin/transactions.html index 74c69d8..0594cdd 100644 --- a/bitvend/templates/admin/transactions.html +++ b/bitvend/templates/admin/transactions.html @@ -1,4 +1,4 @@ -{% extends "base.html" %} +{% extends "admin/base.html" %} {% from "_helpers.html" import format_currency, render_pagination %} {% set _tx_styles = { diff --git a/bitvend/templates/base.html b/bitvend/templates/base.html index a67f5a1..1320004 100644 --- a/bitvend/templates/base.html +++ b/bitvend/templates/base.html @@ -85,7 +85,10 @@ {% endfor %} {% endwith %} - {% block content %}{% endblock %} + {% block body %} + {% block content %} + {% endblock %} + {% endblock %}