views: Hall of fame...?

feature/cython
informatic 2017-05-05 22:19:14 +02:00
parent 30a705a9a3
commit ca2d83429f
5 changed files with 54 additions and 8 deletions

View File

@ -3,7 +3,7 @@ from flask_sqlalchemy import SQLAlchemy
from datetime import datetime
from sqlalchemy.ext.hybrid import hybrid_property
from sqlalchemy.sql import func, select
from sqlalchemy.sql import func, select, and_
db = SQLAlchemy()
@ -34,6 +34,32 @@ class User(db.Model):
label("balance")
)
@hybrid_property
def purchase_count(self):
return self.transactions.filter(Transaction.type == 'purchase').count()
@purchase_count.expression
def purchase_count(self):
return (select([func.count(Transaction.amount)]).
where(and_(
Transaction.uid == User.uid,
Transaction.type == 'purchase')).
label("purchase_count")
)
@hybrid_property
def purchase_amount(self):
return -sum((tx.amount or 0) for tx in self.transactions.filter(Transaction.type == 'purchase'))
@purchase_amount.expression
def purchase_amount(self):
return (select([-func.sum(Transaction.amount)]).
where(and_(
Transaction.uid == User.uid,
Transaction.type == 'purchase')).
label("purchase_amount")
)
def transfer(self, target, amount):
if amount > self.amount_available:
raise NoFunds()

View File

@ -2,7 +2,7 @@
{%- if amount == None -%}
None
{%- else -%}
<span class="amount{% if amount < 0 and color %} amount-negative{% endif %}" data-original="{{ amount }}">
<span class="amount{% if color %}{% if amount < 0 %} amount-negative{% else %} amount-positive{% endif %}{% endif %}" data-original="{{ amount }}">
{{ format_currency_raw(amount, precision) }}
</span>
{%- endif %}

View File

@ -23,6 +23,7 @@
.well h3 { padding: 0; }
.well h3 small { padding-top: 5px; }
.amount-negative { color: #990000; }
.amount-positive { color: #007700; }
h3.page-header {
margin-top: 10px;
}

View File

@ -51,14 +51,26 @@
<h3 class="page-header">Hall of Shame</h3>
<table class="table table-hover table-striped">
<thead><tr>
<th>Name</th><th>Balance</th>
<th>Name</th><th class="text-right">Balance</th>
</tr></thead>
{% for user in hallofshame %}
<tr><td>{{ user }}</td><td>{{ format_currency(user.balance) }}</td></tr>
<tr><td>{{ user }}</td><td class="text-right">{{ format_currency(user.balance) }}</td></tr>
{% else %}
<tr><td colspan=2 class="placeholder">Wow! Nobody's due!</td></tr>
{% endfor %}
</table>
<h3 class="page-header">Hall of Addicts</h3>
<table class="table table-hover table-striped">
<thead><tr>
<th>Name</th><th class="text-right">Amount</th><th>Purchases</th>
</tr></thead>
{% for user in hallofaddicts %}
<tr><td>{{ user }}</td><td class="text-right">{{ format_currency(user.purchase_amount) }}</td><td>{{ user.purchase_count }}</td></tr>
{% else %}
<tr><td colspan=3 class="placeholder">Huh?</td></tr>
{% endfor %}
</table>
</div>
<div class="col-sm-8">
<h3 class="page-header">Latest transactions</h3>
@ -66,7 +78,7 @@
<thead>
<tr>
<th>Type</th>
<th>Amount</th>
<th class="text-right">Amount</th>
<th>Date</th>
</tr>
</thead>
@ -77,7 +89,7 @@
{{ tx.type }} {% if not tx.finished %}<i>(processing)</i>{% endif %}
<small>
{% if tx.type == 'transfer' and tx.amount > 0 %}
from <b>{{ tx.related_user }}</b>
from <b>{{ tx.related }}</b>
{% elif tx.type == 'transfer' and tx.amount < 0 %}
to <b>{{ tx.related }}</b>
{% elif tx.type == 'purchase' and tx.product_id %}
@ -85,7 +97,7 @@
{% endif %}
</small>
</td>
<td>{{ format_currency(tx.amount) }}</td>
<td class="text-right">{{ format_currency(tx.amount) }}</td>
<td>{{ tx.created }}</td>
</tr>
{% else %}

View File

@ -1,5 +1,6 @@
from flask import Blueprint, render_template, redirect, request, flash, url_for
from flask import current_app as app
from flask_login import login_required, current_user, logout_user
import six
import qrcode
@ -9,7 +10,6 @@ from bitvend import dev, proc
from bitvend.models import db, User, Transaction, NoFunds
from bitvend.auth import try_login, cap_required
from bitvend.forms import TransferForm
from flask_login import login_required, current_user, logout_user
bp = Blueprint('bitvend', __name__, template_folder='templates')
@ -23,6 +23,12 @@ def index():
.limit(5) \
.all()
hallofaddicts = User.query \
.order_by(User.purchase_amount.desc()) \
.filter(User.purchase_amount > 0) \
.limit(5) \
.all()
if current_user.is_authenticated:
transactions = current_user.transactions.order_by(Transaction.created.desc()).limit(10)
@ -32,6 +38,7 @@ def index():
transactions=transactions,
transfer_form=TransferForm(),
hallofshame=hallofshame,
hallofaddicts=hallofaddicts,
)
@bp.route('/transactions/', defaults={'page': 1})