forked from 3wordchant/capsul-flask
Your account does not have sufficient funds to create a Capsul
This commit is contained in:
@ -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")
|
||||
return render_template(
|
||||
"account-balance.html",
|
||||
payments=payments, has_payments=len(payments)>0, account_balance=account_balance
|
||||
)
|
Reference in New Issue
Block a user