From 407abf79b82723d7f82535ea1900135280521e50 Mon Sep 17 00:00:00 2001 From: Santiago Lamora Date: Fri, 14 Oct 2022 14:39:11 +0200 Subject: [PATCH] Add BillingIndexView to render current usage (partial) --- .../billing/templates/billing/home.html | 38 ++++++++++++++ ereuse_devicehub/billing/views.py | 52 ++++++++++++++++++- 2 files changed, 89 insertions(+), 1 deletion(-) create mode 100644 ereuse_devicehub/billing/templates/billing/home.html diff --git a/ereuse_devicehub/billing/templates/billing/home.html b/ereuse_devicehub/billing/templates/billing/home.html new file mode 100644 index 00000000..a38b5377 --- /dev/null +++ b/ereuse_devicehub/billing/templates/billing/home.html @@ -0,0 +1,38 @@ +{% extends "ereuse_devicehub/base_site.html" %} +{% block main %} + +
+

Billing

+ +
+ +
+ Current usage + + + + + + + + + + + + + + + + + + + + +
YearMonthSnapshot (register)Snapshot (update)Drives Erasure (uniques)
{{ current_month_usage.year }}{{ current_month_usage.month }}{{ current_month_usage.snapshot_register }}{{ current_month_usage.snapshot_update }}{{ current_month_usage.drives_erasure }}
+ +
+{% endblock main %} diff --git a/ereuse_devicehub/billing/views.py b/ereuse_devicehub/billing/views.py index fd88ba83..1effbe4a 100644 --- a/ereuse_devicehub/billing/views.py +++ b/ereuse_devicehub/billing/views.py @@ -1,7 +1,57 @@ import logging +import flask from flask import Blueprint +from flask.views import View +from flask_login import current_user, login_required +from sqlalchemy.sql import extract -billing = Blueprint('billing', __name__, url_prefix='/billing') +from ereuse_devicehub import __version__ +from ereuse_devicehub.resources.action.models import Snapshot + +billing = Blueprint( + "billing", __name__, url_prefix="/billing", template_folder="templates" +) logger = logging.getLogger(__name__) + + +class BillingIndexView(View): + methods = ["GET"] + decorators = [login_required] + template_name = "billing/home.html" + + def dispatch_request(self): + # TODO (@slamora): replace hardcoded and get current time + year = 2022 + month = 9 + snapshot_register, snapshot_update = self.count_snapshot(year, month) + + current_month_usage = { + "year": year, + "month": month, + "snapshot_register": snapshot_register, + "snapshot_update": snapshot_update, + } + context = { + "current_month_usage": current_month_usage, + "page_title": "Billing", + "version": __version__, + } + return flask.render_template(self.template_name, **context) + + def count_snapshot(self, year, month): + query = Snapshot.query.filter( + Snapshot.author_id == current_user.id, + extract('year', Snapshot.created) == year, + extract('month', Snapshot.created) == month, + ) + + all = query.count() + register = query.distinct(Snapshot.device_id).count() + update = all - register + + return (register, update) + + +billing.add_url_rule("/", view_func=BillingIndexView.as_view("billing_index"))