Your account does not have sufficient funds to create a Capsul

This commit is contained in:
forest 2020-05-11 22:59:36 -05:00
parent 2e6291d87c
commit 5df06dc1b3
3 changed files with 79 additions and 63 deletions

View File

@ -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
@ -140,6 +141,11 @@ def create():
)
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")
return render_template(
"account-balance.html",
payments=payments, has_payments=len(payments)>0, account_balance=account_balance
)

View File

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

View File

@ -20,21 +20,31 @@
</p>
{% endif %}
{% else %}
{% if has_ssh_public_keys %}
{% if cant_afford %}
<p>
Your account does not have sufficient funds to create a Capsul.
(Must be funded enough to last at least one month at creation time).
</p>
<p>You must <a href="/console/account-balance">add funds to your account</a> before you can create a Capsul.</p>
{% else %}
{% if no_ssh_public_keys %}
<p>You don't have any ssh public keys yet.</p>
<p>You must <a href="/console/ssh">upload one</a> before you can create a Capsul.</p>
{% else %}
<pre>
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
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</pre>
* net is calculated as a per-month average
* all VMs come standard with one public IPv4 addr</pre>
<form method="post">
<div class="row justify-start">
@ -67,9 +77,8 @@ f1-xxx $57.58 8 16G 25G 16TB
<input type="submit" value="Create">
</div>
</form>
{% else %}
<p>You don't have any ssh public keys yet.</p>
<p>You must <a href="/console/ssh">upload one</a> before you can create a Capsul.</p>
{% endif %}
{% endif %}
{% endif %}
@ -77,7 +86,7 @@ f1-xxx $57.58 8 16G 25G 16TB
{% endblock %}
{% block subcontent %}
{% if ssh_public_keys[0] is defined %}
{% if not cant_afford and not no_ssh_public_keys %}
<p>
Using our services implies that you agree to our terms of service & privacy policy.
</p>