uncloud-mravi/uncloud_pay/selectors.py

27 lines
722 B
Python
Raw Normal View History

2021-01-01 11:41:54 +00:00
from django.utils import timezone
from django.db import transaction
from .models import *
def get_payments_for_user(user):
payments = [ payment.amount for payment in Payment.objects.filter(owner=user) ]
return sum(payments)
def get_spendings_for_user(user):
orders = Order.objects.filter(owner=user)
amount = 0
for order in orders:
amount += order.one_time_price
amount += order.recurring_price * order.count_used(when=timezone.now())
return amount
@transaction.atomic
def get_balance_for_user(user):
return get_payments_for_user(user) - get_spendings_for_user(user)
2021-01-17 14:53:30 +00:00
def get_billing_address_for_user(user):
return BillingAddress.objects.get(owner=user, active=True)