From 5df06dc1b3cfeb09db93fe172bc8110cc9d580ca Mon Sep 17 00:00:00 2001
From: forest
Date: Mon, 11 May 2020 22:59:36 -0500
Subject: [PATCH] Your account does not have sufficient funds to create a
Capsul
---
capsulflask/console.py | 29 +++---
capsulflask/db_model.py | 4 +-
capsulflask/templates/create-capsul.html | 109 ++++++++++++-----------
3 files changed, 79 insertions(+), 63 deletions(-)
diff --git a/capsulflask/console.py b/capsulflask/console.py
index 7ea2966..bcd40a9 100644
--- a/capsulflask/console.py
+++ b/capsulflask/console.py
@@ -83,6 +83,7 @@ def create():
vm_sizes = get_model().vm_sizes_dict()
operating_systems = get_model().operating_systems_dict()
ssh_public_keys = get_model().list_ssh_public_keys_for_account(session["account"])
+ account_balance = get_account_balance()
errors = list()
created_os = None
@@ -139,7 +140,12 @@ def create():
ssh_public_keys=list(map(lambda x: x["content"], posted_keys))
)
created_os = os
-
+
+ affordable_vm_sizes = dict()
+ for key, vm_size in vm_sizes.items():
+ if vm_size["dollars_per_month"] < account_balance:
+ affordable_vm_sizes[key] = vm_size
+
for error in errors:
flash(error)
@@ -148,9 +154,10 @@ def create():
created_os=created_os,
ssh_public_keys=ssh_public_keys,
ssh_public_key_count=len(ssh_public_keys),
- has_ssh_public_keys=len(ssh_public_keys) > 0,
+ no_ssh_public_keys=len(ssh_public_keys) == 0,
operating_systems=operating_systems,
- vm_sizes=vm_sizes
+ cant_afford=len(affordable_vm_sizes) == 0,
+ vm_sizes=affordable_vm_sizes
)
@bp.route("/ssh", methods=("GET", "POST"))
@@ -208,26 +215,26 @@ def get_payments():
return g.user_payments
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()
vm_months = ( end_datetime - vm["created"] ).days / average_number_of_days_in_a_month
- vm_cost_dollars += vm_months * vm["dollars_per_month"]
-
- payment_total = sum(map(lambda x: x["dollars"], get_payments()))
+ vm_cost_dollars += vm_months * float(vm["dollars_per_month"])
+ payment_dollars_total = sum(map(lambda x: x["dollars"], get_payments()))
+ return payment_dollars_total - vm_cost_dollars
@bp.route("/account-balance")
@account_required
def account_balance():
errors = list()
payments = get_payments()
+ account_balance = get_account_balance()
-
-
- return render_template("account-balance.html")
\ No newline at end of file
+ return render_template(
+ "account-balance.html",
+ payments=payments, has_payments=len(payments)>0, account_balance=account_balance
+ )
\ No newline at end of file
diff --git a/capsulflask/db_model.py b/capsulflask/db_model.py
index a19023a..3c8c6af 100644
--- a/capsulflask/db_model.py
+++ b/capsulflask/db_model.py
@@ -142,12 +142,12 @@ class DBModel:
def list_payments_for_account(self, email):
self.cursor.execute("""
- SELECT payments.id, payments.dollars, payments.created
+ SELECT payments.dollars, payments.created
FROM payments WHERE payments.email = %s""",
(email, )
)
return list(map(
- lambda x: dict(id=x[0], dollars=x[1], created=x[2]),
+ lambda x: dict(dollars=x[0], created=x[1]),
self.cursor.fetchall()
))
diff --git a/capsulflask/templates/create-capsul.html b/capsulflask/templates/create-capsul.html
index f9fa8d3..dfc69ca 100644
--- a/capsulflask/templates/create-capsul.html
+++ b/capsulflask/templates/create-capsul.html
@@ -19,57 +19,66 @@
may take an extra-long time to obtain an IP address, like up to 10 minutes. Be patient.
{% endif %}
-{% else %}
- {% if has_ssh_public_keys %}
-
-CAPSUL SIZES
-============
-type monthly cpus mem ssd net*
------ ------- ---- --- --- ---
-f1-s $5.33 1 512M 25G .5TB
-f1-m $7.16 1 1024M 25G 1TB
-f1-l $8.92 1 2048M 25G 2TB
-f1-x $16.16 2 4096M 25G 4TB
-f1-xx $29.66 4 8096M 25G 8TB
-f1-xxx $57.58 8 16G 25G 16TB
+{% else %}
+ {% if cant_afford %}
+
+ Your account does not have sufficient funds to create a Capsul.
+ (Must be funded enough to last at least one month at creation time).
+
+ You must add funds to your account before you can create a Capsul.
+ {% else %}
+ {% if no_ssh_public_keys %}
+ You don't have any ssh public keys yet.
+ You must upload one before you can create a Capsul.
+ {% else %}
+
+ CAPSUL SIZES
+ ============
+ type monthly cpus mem ssd net*
+ ----- ------- ---- --- --- ---
+ f1-s $5.33 1 512M 25G .5TB
+ f1-m $7.16 1 1024M 25G 1TB
+ f1-l $8.92 1 2048M 25G 2TB
+ f1-x $16.16 2 4096M 25G 4TB
+ f1-xx $29.66 4 8096M 25G 8TB
+ f1-xxx $57.58 8 16G 25G 16TB
+
+ * net is calculated as a per-month average
+ * all VMs come standard with one public IPv4 addr
-* net is calculated as a per-month average
-* all VMs come standard with one public IPv4 addr
-
-