Add BillingIndexView to render current usage (partial)

This commit is contained in:
Santiago L 2022-10-14 14:39:11 +02:00
parent b06e863f91
commit 407abf79b8
2 changed files with 89 additions and 1 deletions

View File

@ -0,0 +1,38 @@
{% extends "ereuse_devicehub/base_site.html" %}
{% block main %}
<div class="pagetitle">
<h1>Billing</h1>
<nav>
<ol class="breadcrumb">
<li class="breadcrumb-item active">{{ page_title }}</li>
</ol>
</nav>
</div><!-- End Page Title -->
<section class="section">
Current usage
<table class="table table-striped">
<thead>
<tr>
<th scope="col">Year</th>
<th scope="col">Month</th>
<th scope="col">Snapshot (register)</th>
<th scope="col">Snapshot (update)</th>
<th scope="col">Drives Erasure (uniques)</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">{{ current_month_usage.year }}</th>
<th scope="row">{{ current_month_usage.month }}</th>
<td>{{ current_month_usage.snapshot_register }}</td>
<td>{{ current_month_usage.snapshot_update }}</td>
<td>{{ current_month_usage.drives_erasure }}</td>
</tr>
</tbody>
</table>
</section>
{% endblock main %}

View File

@ -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"))