From b06e863f91015d384bc18aea3dd48188c9b8d18d Mon Sep 17 00:00:00 2001 From: Santiago Lamora Date: Fri, 14 Oct 2022 11:28:53 +0200 Subject: [PATCH 1/3] Create biling Blueprint --- ereuse_devicehub/billing/__init__.py | 0 ereuse_devicehub/billing/views.py | 7 +++++++ examples/app.py | 2 ++ 3 files changed, 9 insertions(+) create mode 100644 ereuse_devicehub/billing/__init__.py create mode 100644 ereuse_devicehub/billing/views.py diff --git a/ereuse_devicehub/billing/__init__.py b/ereuse_devicehub/billing/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/ereuse_devicehub/billing/views.py b/ereuse_devicehub/billing/views.py new file mode 100644 index 000000000..fd88ba83d --- /dev/null +++ b/ereuse_devicehub/billing/views.py @@ -0,0 +1,7 @@ +import logging + +from flask import Blueprint + +billing = Blueprint('billing', __name__, url_prefix='/billing') + +logger = logging.getLogger(__name__) diff --git a/examples/app.py b/examples/app.py index 623bfaef5..5ef2b956e 100644 --- a/examples/app.py +++ b/examples/app.py @@ -7,6 +7,7 @@ from decouple import config from ereuse_devicehub.api.views import api +from ereuse_devicehub.billing.views import billing from ereuse_devicehub.config import DevicehubConfig from ereuse_devicehub.devicehub import Devicehub from ereuse_devicehub.inventory.views import devices @@ -43,6 +44,7 @@ app.register_blueprint(labels) app.register_blueprint(api) app.register_blueprint(workbench) +app.register_blueprint(billing) # configure & enable CSRF of Flask-WTF # NOTE: enable by blueprint to exclude API views From 407abf79b82723d7f82535ea1900135280521e50 Mon Sep 17 00:00:00 2001 From: Santiago Lamora Date: Fri, 14 Oct 2022 14:39:11 +0200 Subject: [PATCH 2/3] 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 000000000..a38b53776 --- /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 fd88ba83d..1effbe4ae 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")) From deec522dfb23b01dae42af6579c4b46ec5c83dc1 Mon Sep 17 00:00:00 2001 From: Santiago Lamora Date: Thu, 27 Oct 2022 23:12:17 +0200 Subject: [PATCH 3/3] Add TODO: datetime.now + TZ --- ereuse_devicehub/billing/views.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/ereuse_devicehub/billing/views.py b/ereuse_devicehub/billing/views.py index 1effbe4ae..a3781466a 100644 --- a/ereuse_devicehub/billing/views.py +++ b/ereuse_devicehub/billing/views.py @@ -23,6 +23,8 @@ class BillingIndexView(View): def dispatch_request(self): # TODO (@slamora): replace hardcoded and get current time + # https://dateutil.readthedocs.io/en/stable/_modules/dateutil/tz/tz.html?highlight=now() + # datetime.now(tzutc()) year = 2022 month = 9 snapshot_register, snapshot_update = self.count_snapshot(year, month) @@ -32,6 +34,7 @@ def dispatch_request(self): "month": month, "snapshot_register": snapshot_register, "snapshot_update": snapshot_update, + # TODO (@slamora): data erasure count } context = { "current_month_usage": current_month_usage,