Files
@ e1281796294a
Branch filter:
Location: symposion_app/registrasion/controllers/category.py - annotation
e1281796294a
2.1 KiB
text/x-python
Merge branch 'chrisjrn/fix_64'
875f736d67c5 875f736d67c5 f7289c21019b a79ad3520e60 a79ad3520e60 576dddcaad19 a79ad3520e60 a79ad3520e60 576dddcaad19 efb73e7a682f 2e0144effe14 64ca477cb89a f7289c21019b f7289c21019b f7289c21019b 2e0144effe14 f7289c21019b f7289c21019b 576dddcaad19 576dddcaad19 576dddcaad19 f7289c21019b f7289c21019b f7289c21019b f7289c21019b f7289c21019b f7289c21019b 576dddcaad19 576dddcaad19 576dddcaad19 f7289c21019b 875f736d67c5 875f736d67c5 875f736d67c5 f7289c21019b f7289c21019b f7289c21019b f7289c21019b f7289c21019b f7289c21019b f7289c21019b 576dddcaad19 a79ad3520e60 efb73e7a682f c6fdfa496e84 a79ad3520e60 a79ad3520e60 a79ad3520e60 c6fdfa496e84 c6fdfa496e84 c6fdfa496e84 a79ad3520e60 a79ad3520e60 c6fdfa496e84 a79ad3520e60 a79ad3520e60 a79ad3520e60 a79ad3520e60 a79ad3520e60 576dddcaad19 a79ad3520e60 a79ad3520e60 a79ad3520e60 a79ad3520e60 576dddcaad19 a79ad3520e60 a79ad3520e60 a79ad3520e60 576dddcaad19 576dddcaad19 a79ad3520e60 a79ad3520e60 a79ad3520e60 576dddcaad19 576dddcaad19 a79ad3520e60 a79ad3520e60 c6fdfa496e84 | from registrasion.models import commerce
from registrasion.models import inventory
from django.db.models import Case
from django.db.models import F, Q
from django.db.models import Sum
from django.db.models import When
from django.db.models import Value
from .batch import BatchController
class AllProducts(object):
pass
class CategoryController(object):
def __init__(self, category):
self.category = category
@classmethod
def available_categories(cls, user, products=AllProducts):
''' Returns the categories available to the user. Specify `products` if
you want to restrict to just the categories that hold the specified
products, otherwise it'll do all. '''
# STOPGAP -- this needs to be elsewhere tbqh
from product import ProductController
if products is AllProducts:
products = inventory.Product.objects.all().select_related(
"category",
)
available = ProductController.available_products(
user,
products=products,
)
return set(i.category for i in available)
@classmethod
@BatchController.memoise
def user_remainders(cls, user):
'''
Return:
Mapping[int->int]: A dictionary that maps the category ID to the
user's remainder for that category.
'''
categories = inventory.Category.objects.all()
cart_filter = (
Q(product__productitem__cart__user=user) &
Q(product__productitem__cart__status=commerce.Cart.STATUS_PAID)
)
quantity = When(
cart_filter,
then='product__productitem__quantity'
)
quantity_or_zero = Case(
quantity,
default=Value(0),
)
remainder = Case(
When(limit_per_user=None, then=Value(99999999)),
default=F('limit_per_user') - Sum(quantity_or_zero),
)
categories = categories.annotate(remainder=remainder)
return dict((cat.id, cat.remainder) for cat in categories)
|