2016-04-22 05:06:24 +00:00
|
|
|
from registrasion.models import commerce
|
|
|
|
from registrasion.models import inventory
|
2016-04-02 00:56:03 +00:00
|
|
|
|
2016-04-28 08:57:55 +00:00
|
|
|
from django.db.models import Case
|
|
|
|
from django.db.models import F, Q
|
2016-04-02 03:03:25 +00:00
|
|
|
from django.db.models import Sum
|
2016-04-28 08:57:55 +00:00
|
|
|
from django.db.models import When
|
|
|
|
from django.db.models import Value
|
2016-04-02 03:03:25 +00:00
|
|
|
|
2016-05-01 02:42:06 +00:00
|
|
|
from .batch import BatchController
|
2016-04-02 02:29:53 +00:00
|
|
|
|
2016-09-02 01:43:27 +00:00
|
|
|
|
2016-04-02 00:56:03 +00:00
|
|
|
class AllProducts(object):
|
|
|
|
pass
|
|
|
|
|
2016-04-02 02:29:53 +00:00
|
|
|
|
2016-04-02 00:56:03 +00:00
|
|
|
class CategoryController(object):
|
|
|
|
|
2016-04-02 03:03:25 +00:00
|
|
|
def __init__(self, category):
|
|
|
|
self.category = category
|
|
|
|
|
2016-04-02 00:56:03 +00:00
|
|
|
@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. '''
|
|
|
|
|
2016-04-02 03:03:25 +00:00
|
|
|
# STOPGAP -- this needs to be elsewhere tbqh
|
|
|
|
from product import ProductController
|
|
|
|
|
2016-04-02 00:56:03 +00:00
|
|
|
if products is AllProducts:
|
2016-04-22 05:06:24 +00:00
|
|
|
products = inventory.Product.objects.all().select_related(
|
|
|
|
"category",
|
|
|
|
)
|
2016-04-02 00:56:03 +00:00
|
|
|
|
|
|
|
available = ProductController.available_products(
|
|
|
|
user,
|
|
|
|
products=products,
|
|
|
|
)
|
|
|
|
|
|
|
|
return set(i.category for i in available)
|
2016-04-02 03:03:25 +00:00
|
|
|
|
2016-04-28 08:57:55 +00:00
|
|
|
@classmethod
|
2016-05-01 02:42:06 +00:00
|
|
|
@BatchController.memoise
|
2016-04-30 10:30:44 +00:00
|
|
|
def user_remainders(cls, user):
|
2016-04-28 08:57:55 +00:00
|
|
|
'''
|
|
|
|
|
|
|
|
Return:
|
2016-04-30 10:30:44 +00:00
|
|
|
Mapping[int->int]: A dictionary that maps the category ID to the
|
|
|
|
user's remainder for that category.
|
|
|
|
|
2016-04-28 08:57:55 +00:00
|
|
|
'''
|
|
|
|
|
2016-04-30 10:30:44 +00:00
|
|
|
categories = inventory.Category.objects.all()
|
2016-04-28 08:57:55 +00:00
|
|
|
|
|
|
|
cart_filter = (
|
|
|
|
Q(product__productitem__cart__user=user) &
|
|
|
|
Q(product__productitem__cart__status=commerce.Cart.STATUS_PAID)
|
|
|
|
)
|
2016-04-02 03:03:25 +00:00
|
|
|
|
2016-04-28 08:57:55 +00:00
|
|
|
quantity = When(
|
|
|
|
cart_filter,
|
|
|
|
then='product__productitem__quantity'
|
|
|
|
)
|
2016-04-02 03:03:25 +00:00
|
|
|
|
2016-04-28 08:57:55 +00:00
|
|
|
quantity_or_zero = Case(
|
|
|
|
quantity,
|
|
|
|
default=Value(0),
|
2016-04-02 03:03:25 +00:00
|
|
|
)
|
|
|
|
|
2016-04-28 08:57:55 +00:00
|
|
|
remainder = Case(
|
|
|
|
When(limit_per_user=None, then=Value(99999999)),
|
|
|
|
default=F('limit_per_user') - Sum(quantity_or_zero),
|
2016-04-02 03:03:25 +00:00
|
|
|
)
|
|
|
|
|
2016-04-28 08:57:55 +00:00
|
|
|
categories = categories.annotate(remainder=remainder)
|
|
|
|
|
2016-04-30 10:30:44 +00:00
|
|
|
return dict((cat.id, cat.remainder) for cat in categories)
|