Coverage for jacc/helpers.py: 100%

# Coverage for jacc/helpers.py : 100%

## 5 statements 5 run 0 missing 0 excluded

Hot-keys on this page

r m x p toggle line displays

j k next/prev highlighted chunk

0 (zero) top of page

1 (one) first highlighted chunk

1 from decimal import Decimal

2 from django.db.models import QuerySet, Sum

3

4

5 def sum_queryset(qs: QuerySet, key: str = 'amount', default: Decimal = Decimal(0)) -> Decimal:

6 """

7 Returns aggregate sum of queryset 'amount' field.

8 :param qs: QuerySet

9 :param key: Field to sum (default: 'amount')

10 :param default: Default value if no results

11 :return: Sum of 'amount' field values (coalesced 0 if None)

12 """

13 res = qs.aggregate(b=Sum(key))['b']

14 return default if res is None else res