forked from 3wordchant/capsul-flask
		
	account balance
This commit is contained in:
		@ -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')
 | 
			
		||||
  )
 | 
			
		||||
@ -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;
 | 
			
		||||
@ -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;
 | 
			
		||||
 | 
			
		||||
@ -4,10 +4,82 @@
 | 
			
		||||
 | 
			
		||||
{% block content %}
 | 
			
		||||
<div class="third-margin">
 | 
			
		||||
  <h1>Account Balance</h1>
 | 
			
		||||
  <h1>Account Balance: ${{ account_balance }}</h1>
 | 
			
		||||
</div>
 | 
			
		||||
<div class="half-margin">
 | 
			
		||||
 
 | 
			
		||||
  <div class="row">
 | 
			
		||||
    {% if has_payments %}
 | 
			
		||||
    <div>
 | 
			
		||||
      <div class="third-margin">
 | 
			
		||||
        <h1>Payments</h1>
 | 
			
		||||
      </div>
 | 
			
		||||
    
 | 
			
		||||
      <table>
 | 
			
		||||
        <thead>
 | 
			
		||||
          <tr>
 | 
			
		||||
            <th>amount</th>
 | 
			
		||||
            <th>date</th>
 | 
			
		||||
          </tr>
 | 
			
		||||
        </thead>
 | 
			
		||||
        <tbody>
 | 
			
		||||
          {% for payment in payments %}
 | 
			
		||||
            <tr>
 | 
			
		||||
              <td>${{ payment["dollars"] }}</td>
 | 
			
		||||
              <td>{{ payment["created"] }}</td>
 | 
			
		||||
            </tr>
 | 
			
		||||
          {% endfor %}
 | 
			
		||||
        </tbody>
 | 
			
		||||
      </table>
 | 
			
		||||
    </div>
 | 
			
		||||
    {% endif %}
 | 
			
		||||
    <ul>
 | 
			
		||||
      <li>
 | 
			
		||||
        <h1>PAYMENT OPTIONS</h1>
 | 
			
		||||
        <ul>
 | 
			
		||||
          <li>
 | 
			
		||||
            <a href="/console/stripe">Add funds with Credit/Debit (stripe)</a>
 | 
			
		||||
            <ul><li>notice: stripe will load nonfree javascript </li></ul>
 | 
			
		||||
          </li>
 | 
			
		||||
          <li><a href="/console/btcpay">Add funds with Bitcoin/Litecoin/Monero (btcpay)</a></li>
 | 
			
		||||
    
 | 
			
		||||
          <li>Cash: email treasurer@cyberia.club</li>
 | 
			
		||||
        </ul>
 | 
			
		||||
      </li>
 | 
			
		||||
    </ul>
 | 
			
		||||
 | 
			
		||||
  </div>
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
  {% if has_vms %}
 | 
			
		||||
  <div class="third-margin">
 | 
			
		||||
    <h1>Capsuls Billed</h1>
 | 
			
		||||
  </div>
 | 
			
		||||
  <table class="small">
 | 
			
		||||
    <thead>
 | 
			
		||||
      <tr>
 | 
			
		||||
        <th>id</th>
 | 
			
		||||
        <th>created</th>
 | 
			
		||||
        <th>deleted</th>
 | 
			
		||||
        <th>$/month</th>
 | 
			
		||||
        <th>months</th>
 | 
			
		||||
        <th>$ billed</th>
 | 
			
		||||
      </tr>
 | 
			
		||||
    </thead>
 | 
			
		||||
    <tbody>
 | 
			
		||||
      {% for vm in vms_billed %}
 | 
			
		||||
        <tr>
 | 
			
		||||
          <td>{{ vm["id"] }}</td>
 | 
			
		||||
          <td>{{ vm["created"] }}</td>
 | 
			
		||||
          <td>{{ vm["deleted"] }}</td>
 | 
			
		||||
          <td>${{ vm["dollars_per_month"] }}</td>
 | 
			
		||||
          <td>{{ vm["months"] }}</td>
 | 
			
		||||
          <td>${{ vm["dollars"] }}</td>
 | 
			
		||||
        </tr>
 | 
			
		||||
      {% endfor %}
 | 
			
		||||
    </tbody>
 | 
			
		||||
  </table>
 | 
			
		||||
  {% endif %}
 | 
			
		||||
</div>
 | 
			
		||||
{% endblock %}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -45,6 +45,8 @@
 | 
			
		||||
      
 | 
			
		||||
      * net is calculated as a per-month average
 | 
			
		||||
      * all VMs come standard with one public IPv4 addr</pre>
 | 
			
		||||
      <pre>
 | 
			
		||||
      Your <a href="/console/account-balance">account balance</a>: ${{ account_balance }}</div>
 | 
			
		||||
 | 
			
		||||
      <form method="post">
 | 
			
		||||
        <div class="row justify-start">
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user