Files
@ 84c40a1e1f28
Branch filter:
Location: symposion_app/registrasion/controllers/item.py - annotation
84c40a1e1f28
3.0 KiB
text/x-python
Refactors ItemController, add items_released
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 | 68aa9b067bdb 68aa9b067bdb 68aa9b067bdb 68aa9b067bdb 68aa9b067bdb 68aa9b067bdb 68aa9b067bdb 68aa9b067bdb 68aa9b067bdb 68aa9b067bdb 68aa9b067bdb 68aa9b067bdb 68aa9b067bdb 68aa9b067bdb 68aa9b067bdb 68aa9b067bdb 68aa9b067bdb 68aa9b067bdb 68aa9b067bdb 68aa9b067bdb 68aa9b067bdb 68aa9b067bdb 68aa9b067bdb 68aa9b067bdb 68aa9b067bdb 68aa9b067bdb 68aa9b067bdb 68aa9b067bdb 68aa9b067bdb 68aa9b067bdb 68aa9b067bdb 68aa9b067bdb 84c40a1e1f28 68aa9b067bdb 68aa9b067bdb 68aa9b067bdb 68aa9b067bdb 68aa9b067bdb 68aa9b067bdb 68aa9b067bdb 68aa9b067bdb 68aa9b067bdb 68aa9b067bdb 68aa9b067bdb 68aa9b067bdb 68aa9b067bdb 68aa9b067bdb 84c40a1e1f28 68aa9b067bdb 68aa9b067bdb 68aa9b067bdb 68aa9b067bdb 68aa9b067bdb 68aa9b067bdb 68aa9b067bdb 68aa9b067bdb 68aa9b067bdb 68aa9b067bdb 68aa9b067bdb 68aa9b067bdb 68aa9b067bdb 68aa9b067bdb 68aa9b067bdb 68aa9b067bdb 68aa9b067bdb 68aa9b067bdb 68aa9b067bdb 68aa9b067bdb 68aa9b067bdb 68aa9b067bdb 68aa9b067bdb 68aa9b067bdb 68aa9b067bdb 68aa9b067bdb 84c40a1e1f28 84c40a1e1f28 84c40a1e1f28 84c40a1e1f28 84c40a1e1f28 84c40a1e1f28 84c40a1e1f28 84c40a1e1f28 84c40a1e1f28 84c40a1e1f28 84c40a1e1f28 84c40a1e1f28 84c40a1e1f28 84c40a1e1f28 68aa9b067bdb 68aa9b067bdb 68aa9b067bdb 68aa9b067bdb 68aa9b067bdb 68aa9b067bdb 68aa9b067bdb 68aa9b067bdb 68aa9b067bdb 68aa9b067bdb 84c40a1e1f28 84c40a1e1f28 84c40a1e1f28 84c40a1e1f28 84c40a1e1f28 84c40a1e1f28 84c40a1e1f28 84c40a1e1f28 84c40a1e1f28 84c40a1e1f28 84c40a1e1f28 84c40a1e1f28 84c40a1e1f28 | ''' NEEDS TESTS '''
from registrasion.models import commerce
from registrasion.models import inventory
from collections import namedtuple
from django.db.models import Case
from django.db.models import Q
from django.db.models import Sum
from django.db.models import When
from django.db.models import Value
_ProductAndQuantity = namedtuple("ProductAndQuantity", ["product", "quantity"])
class ProductAndQuantity(_ProductAndQuantity):
''' Class that holds a product and a quantity.
Attributes:
product (models.inventory.Product)
quantity (int)
'''
pass
class ItemController(object):
def __init__(self, user):
self.user = user
def _items(self, cart_status, category=None):
''' Aggregates the items that this user has purchased.
Arguments:
category (Optional[models.inventory.Category]): the category
of items to restrict to.
Returns:
[ProductAndQuantity, ...]: A list of product-quantity pairs,
aggregating like products from across multiple invoices.
'''
in_cart = (
Q(productitem__cart__user=self.user) &
Q(productitem__cart__status=cart_status)
)
quantities_in_cart = When(
in_cart,
then="productitem__quantity",
)
quantities_or_zero = Case(
quantities_in_cart,
default=Value(0),
)
products = inventory.Product.objects
if category:
products = products.filter(category=category)
products = products.select_related("category")
products = products.annotate(quantity=Sum(quantities_or_zero))
products = products.filter(quantity__gt=0)
out = []
for prod in products:
out.append(ProductAndQuantity(prod, prod.quantity))
return out
def items_purchased(self, category=None):
''' Aggregates the items that this user has purchased.
Arguments:
category (Optional[models.inventory.Category]): the category
of items to restrict to.
Returns:
[ProductAndQuantity, ...]: A list of product-quantity pairs,
aggregating like products from across multiple invoices.
'''
return self._items(commerce.Cart.STATUS_PAID)
def items_pending(self):
''' Gets all of the items that the user has reserved, but has not yet
paid for.
Returns:
[ProductAndQuantity, ...]: A list of product-quantity pairs for the
items that the user has not yet paid for.
'''
return self._items(commerce.Cart.STATUS_ACTIVE)
def items_released(self):
''' Gets all of the items that the user previously paid for, but has
since refunded.
Returns:
[ProductAndQuantity, ...]: A list of product-quantity pairs for the
items that the user has not yet paid for.
'''
return self._items(commerce.Cart.STATUS_RELEASED)
|