diff --git a/capsulflask/console.py b/capsulflask/console.py index 1aa2162..bc3ede1 100644 --- a/capsulflask/console.py +++ b/capsulflask/console.py @@ -152,6 +152,7 @@ def create(): return render_template( "create-capsul.html", created_os=created_os, + account_balance=format(account_balance, '.2f'), ssh_public_keys=ssh_public_keys, ssh_public_key_count=len(ssh_public_keys), no_ssh_public_keys=len(ssh_public_keys) == 0, @@ -214,12 +215,14 @@ def get_payments(): g.user_payments = get_model().list_payments_for_account(session["account"]) return g.user_payments + +average_number_of_days_in_a_month = 30.44 + def get_account_balance(): - average_number_of_days_in_a_month = 30.44 vm_cost_dollars = 0.0 for vm in get_vms(): - end_datetime = vm["deleted"] if vm["deleted"] else datetime.now() + end_datetime = vm["deleted"] if vm["deleted"] else datetime.utcnow() vm_months = ( end_datetime - vm["created"] ).days / average_number_of_days_in_a_month vm_cost_dollars += vm_months * float(vm["dollars_per_month"]) @@ -230,11 +233,30 @@ def get_account_balance(): @bp.route("/account-balance") @account_required def account_balance(): - errors = list() payments = get_payments() account_balance = get_account_balance() + vms_billed = list() + + for vm in get_vms(): + end_datetime = vm["deleted"] if vm["deleted"] else datetime.utcnow() + print(end_datetime) + print(vm["created"]) + vm_months = (end_datetime - vm["created"]).days / average_number_of_days_in_a_month + vms_billed.append(dict( + id=vm["id"], + dollars_per_month=vm["dollars_per_month"], + created=vm["created"].strftime("%b %d %Y"), + deleted=vm["deleted"].strftime("%b %d %Y") if vm["deleted"] else "N/A", + months=format(vm_months, '.3f'), + dollars=format(vm_months * float(vm["dollars_per_month"]), '.2f') + )) + return render_template( "account-balance.html", - payments=payments, has_payments=len(payments)>0, account_balance=account_balance + has_vms=len(vms_billed)>0, + vms_billed=vms_billed, + payments=list(map(lambda x: dict(dollars=x["dollars"], created=x["created"].strftime("%b %d %Y")), payments)), + has_payments=len(payments)>0, + account_balance=format(account_balance, '.2f') ) \ No newline at end of file diff --git a/capsulflask/schema_migrations/02_up_accounts_vms_etc.sql b/capsulflask/schema_migrations/02_up_accounts_vms_etc.sql index 2591700..8c344c1 100644 --- a/capsulflask/schema_migrations/02_up_accounts_vms_etc.sql +++ b/capsulflask/schema_migrations/02_up_accounts_vms_etc.sql @@ -83,6 +83,6 @@ INSERT INTO accounts (email) VALUES ('forest.n.johnson@gmail.com'); INSERT INTO payments (email, dollars, created) -VALUES ('forest.n.johnson@gmail.com', 100.00, TO_TIMESTAMP('2020-05-05','YYYY-MM-DD')); +VALUES ('forest.n.johnson@gmail.com', 20.00, TO_TIMESTAMP('2020-05-05','YYYY-MM-DD')); UPDATE schemaversion SET version = 2; \ No newline at end of file diff --git a/capsulflask/static/style.css b/capsulflask/static/style.css index 656fbf9..c07779e 100644 --- a/capsulflask/static/style.css +++ b/capsulflask/static/style.css @@ -170,7 +170,12 @@ thead { background: #bdc7b812; } td, th { - padding: 0.2em 1em; + font: calc(0.40rem + 1vmin) monospace; + padding: 0.1em 1em; +} +table.small td, table.small th { + font: calc(0.35rem + 0.83vmin) monospace; + padding: 0.1em 1em; } th { border-right: 4px solid #241e1e; diff --git a/capsulflask/templates/account-balance.html b/capsulflask/templates/account-balance.html index 5df714e..69779ff 100644 --- a/capsulflask/templates/account-balance.html +++ b/capsulflask/templates/account-balance.html @@ -4,10 +4,82 @@ {% block content %}
-

Account Balance

+

Account Balance: ${{ account_balance }}

+
+ {% if has_payments %} +
+
+

Payments

+
+ + + + + + + + + + {% for payment in payments %} + + + + + {% endfor %} + +
amountdate
${{ payment["dollars"] }}{{ payment["created"] }}
+
+ {% endif %} + + +
+ + + {% if has_vms %} +
+

Capsuls Billed

+
+ + + + + + + + + + + + + {% for vm in vms_billed %} + + + + + + + + + {% endfor %} + +
idcreateddeleted$/monthmonths$ billed
{{ vm["id"] }}{{ vm["created"] }}{{ vm["deleted"] }}${{ vm["dollars_per_month"] }}{{ vm["months"] }}${{ vm["dollars"] }}
+ {% endif %}
{% endblock %} diff --git a/capsulflask/templates/create-capsul.html b/capsulflask/templates/create-capsul.html index dfc69ca..5962bc9 100644 --- a/capsulflask/templates/create-capsul.html +++ b/capsulflask/templates/create-capsul.html @@ -45,6 +45,8 @@ * net is calculated as a per-month average * all VMs come standard with one public IPv4 addr +
+      Your account balance: ${{ account_balance }}