2016-03-26 02:22:47 +00:00
|
|
|
import itertools
|
|
|
|
|
2016-04-28 08:57:55 +00:00
|
|
|
from django.db.models import Case
|
|
|
|
from django.db.models import F, Q
|
2016-03-27 01:24:48 +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-22 05:06:24 +00:00
|
|
|
from registrasion.models import commerce
|
|
|
|
from registrasion.models import inventory
|
2016-01-22 05:01:30 +00:00
|
|
|
|
2016-04-28 02:13:42 +00:00
|
|
|
from .category import CategoryController
|
|
|
|
from .flag import FlagController
|
2016-01-22 05:01:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ProductController(object):
|
|
|
|
|
|
|
|
def __init__(self, product):
|
|
|
|
self.product = product
|
|
|
|
|
2016-03-26 02:22:47 +00:00
|
|
|
@classmethod
|
|
|
|
def available_products(cls, user, category=None, products=None):
|
|
|
|
''' Returns a list of all of the products that are available per
|
2016-04-28 08:57:55 +00:00
|
|
|
flag conditions from the given categories. '''
|
2016-03-26 02:22:47 +00:00
|
|
|
if category is None and products is None:
|
|
|
|
raise ValueError("You must provide products or a category")
|
|
|
|
|
|
|
|
if category is not None:
|
2016-04-22 05:06:24 +00:00
|
|
|
all_products = inventory.Product.objects.filter(category=category)
|
2016-04-06 08:28:33 +00:00
|
|
|
all_products = all_products.select_related("category")
|
2016-03-26 02:22:47 +00:00
|
|
|
else:
|
|
|
|
all_products = []
|
|
|
|
|
|
|
|
if products is not None:
|
2016-04-06 08:28:33 +00:00
|
|
|
all_products = set(itertools.chain(all_products, products))
|
|
|
|
|
2016-04-30 10:30:44 +00:00
|
|
|
category_remainders = CategoryController.user_remainders(user)
|
2016-04-30 10:30:21 +00:00
|
|
|
product_remainders = ProductController.user_remainders(user)
|
2016-03-26 02:22:47 +00:00
|
|
|
|
2016-04-02 07:03:42 +00:00
|
|
|
passed_limits = set(
|
2016-03-26 02:22:47 +00:00
|
|
|
product
|
|
|
|
for product in all_products
|
2016-04-30 10:30:44 +00:00
|
|
|
if category_remainders[product.category.id] > 0
|
2016-04-30 10:30:21 +00:00
|
|
|
if product_remainders[product.id] > 0
|
2016-04-02 07:03:42 +00:00
|
|
|
)
|
|
|
|
|
2016-04-28 02:13:42 +00:00
|
|
|
failed_and_messages = FlagController.test_flags(
|
2016-04-02 07:03:42 +00:00
|
|
|
user, products=passed_limits
|
2016-04-06 06:09:57 +00:00
|
|
|
)
|
|
|
|
failed_conditions = set(i[0] for i in failed_and_messages)
|
2016-04-02 07:03:42 +00:00
|
|
|
|
|
|
|
out = list(passed_limits - failed_conditions)
|
2016-03-26 09:43:20 +00:00
|
|
|
out.sort(key=lambda product: product.order)
|
2016-04-02 07:10:33 +00:00
|
|
|
|
2016-03-26 09:43:20 +00:00
|
|
|
return out
|
2016-03-26 02:22:47 +00:00
|
|
|
|
2016-04-28 08:57:55 +00:00
|
|
|
@classmethod
|
2016-04-30 10:30:21 +00:00
|
|
|
def user_remainders(cls, user):
|
2016-04-28 08:57:55 +00:00
|
|
|
'''
|
|
|
|
|
|
|
|
Return:
|
2016-04-30 10:30:21 +00:00
|
|
|
Mapping[int->int]: A dictionary that maps the product ID to the
|
|
|
|
user's remainder for that product.
|
2016-04-28 08:57:55 +00:00
|
|
|
'''
|
|
|
|
|
2016-04-30 10:30:21 +00:00
|
|
|
products = inventory.Product.objects.all()
|
2016-04-02 03:20:43 +00:00
|
|
|
|
2016-04-28 08:57:55 +00:00
|
|
|
cart_filter = (
|
|
|
|
Q(productitem__cart__user=user) &
|
|
|
|
Q(productitem__cart__status=commerce.Cart.STATUS_PAID)
|
|
|
|
)
|
|
|
|
|
|
|
|
quantity = When(
|
|
|
|
cart_filter,
|
|
|
|
then='productitem__quantity'
|
|
|
|
)
|
2016-01-22 05:01:30 +00:00
|
|
|
|
2016-04-28 08:57:55 +00:00
|
|
|
quantity_or_zero = Case(
|
|
|
|
quantity,
|
|
|
|
default=Value(0),
|
2016-04-02 02:14:34 +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-03-27 01:24:48 +00:00
|
|
|
)
|
2016-01-22 05:01:30 +00:00
|
|
|
|
2016-04-28 08:57:55 +00:00
|
|
|
products = products.annotate(remainder=remainder)
|
|
|
|
|
2016-04-30 10:30:21 +00:00
|
|
|
return dict((product.id, product.remainder) for product in products)
|