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() vm_sizes = get_model().vm_sizes_dict()
operating_systems = get_model().operating_systems_dict() operating_systems = get_model().operating_systems_dict()
ssh_public_keys = get_model().list_ssh_public_keys_for_account(session["account"]) ssh_public_keys = get_model().list_ssh_public_keys_for_account(session["account"])
account_balance = get_account_balance()
errors = list() errors = list()
created_os = None created_os = None
@ -140,6 +141,11 @@ def create():
) )
created_os = os 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: for error in errors:
flash(error) flash(error)
@ -148,9 +154,10 @@ def create():
created_os=created_os, created_os=created_os,
ssh_public_keys=ssh_public_keys, ssh_public_keys=ssh_public_keys,
ssh_public_key_count=len(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, 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")) @bp.route("/ssh", methods=("GET", "POST"))
@ -208,26 +215,26 @@ def get_payments():
return g.user_payments return g.user_payments
def get_account_balance(): def get_account_balance():
average_number_of_days_in_a_month = 30.44 average_number_of_days_in_a_month = 30.44
vm_cost_dollars = 0.0 vm_cost_dollars = 0.0
for vm in get_vms(): for vm in get_vms():
end_datetime = vm["deleted"] if vm["deleted"] else datetime.now() 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_months = ( end_datetime - vm["created"] ).days / average_number_of_days_in_a_month
vm_cost_dollars += vm_months * vm["dollars_per_month"] vm_cost_dollars += vm_months * float(vm["dollars_per_month"])
payment_total = sum(map(lambda x: x["dollars"], get_payments()))
payment_dollars_total = sum(map(lambda x: x["dollars"], get_payments()))
return payment_dollars_total - vm_cost_dollars
@bp.route("/account-balance") @bp.route("/account-balance")
@account_required @account_required
def account_balance(): def account_balance():
errors = list() errors = list()
payments = get_payments() 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): def list_payments_for_account(self, email):
self.cursor.execute(""" self.cursor.execute("""
SELECT payments.id, payments.dollars, payments.created SELECT payments.dollars, payments.created
FROM payments WHERE payments.email = %s""", FROM payments WHERE payments.email = %s""",
(email, ) (email, )
) )
return list(map( 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() self.cursor.fetchall()
)) ))

View File

@ -20,7 +20,17 @@
</p> </p>
{% endif %} {% endif %}
{% else %} {% 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> <pre>
CAPSUL SIZES CAPSUL SIZES
============ ============
@ -67,9 +77,8 @@ f1-xxx $57.58 8 16G 25G 16TB
<input type="submit" value="Create"> <input type="submit" value="Create">
</div> </div>
</form> </form>
{% else %} {% endif %}
<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 %} {% endif %}
@ -77,7 +86,7 @@ f1-xxx $57.58 8 16G 25G 16TB
{% endblock %} {% endblock %}
{% block subcontent %} {% block subcontent %}
{% if ssh_public_keys[0] is defined %} {% if not cant_afford and not no_ssh_public_keys %}
<p> <p>
Using our services implies that you agree to our terms of service & privacy policy. Using our services implies that you agree to our terms of service & privacy policy.
</p> </p>