Skip to content
This repository was archived by the owner on Dec 17, 2024. It is now read-only.
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file.
38 changes: 38 additions & 0 deletions ereuse_devicehub/billing/templates/billing/home.html
Original file line number Diff line number Diff line change
@@ -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 %}
60 changes: 60 additions & 0 deletions ereuse_devicehub/billing/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
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

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
# 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)

current_month_usage = {
"year": year,
"month": month,
"snapshot_register": snapshot_register,
"snapshot_update": snapshot_update,
# TODO (@slamora): data erasure count
}
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"))
2 changes: 2 additions & 0 deletions examples/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down