bitvend/models: rewrite purchase_count/purchase_amount

master
informatic 2023-07-15 22:42:41 +02:00
parent ad24d2684d
commit 12dd471725
1 changed files with 10 additions and 18 deletions

View File

@ -70,17 +70,11 @@ class User(db.Model):
.correlate_except(Transaction)
)
@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")
)
purchase_count = column_property(
select([func.count(Transaction.amount)])
.where(and_(Transaction.uid == uid, Transaction.type == "purchase"))
.correlate_except(Transaction)
)
@hybrid_property
def purchase_amount(self):
@ -89,13 +83,11 @@ class User(db.Model):
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")
)
purchase_amount = column_property(
select([-func.sum(Transaction.amount)])
.where(and_(Transaction.uid == uid, Transaction.type == "purchase"))
.correlate_except(Transaction)
)
def transfer(self, target, amount):
if amount > self.amount_available: