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-28 08:57:55 +00:00
|
|
|
categories = set(product.category for product in all_products)
|
|
|
|
r = CategoryController.attach_user_remainders(user, categories)
|
2016-04-29 01:22:56 +00:00
|
|
|
cat_quants = dict((c, c) for c in r)
|
2016-04-28 08:57:55 +00:00
|
|
|
|
|
|
|
r = ProductController.attach_user_remainders(user, all_products)
|
2016-04-29 01:22:56 +00:00
|
|
|
prod_quants = dict((p, p) for p in r)
|
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-28 08:57:55 +00:00
|
|
|
if cat_quants[product.category].remainder > 0
|
|
|
|
if prod_quants[product].remainder > 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
|
|
|
|
def attach_user_remainders(cls, user, products):
|
|
|
|
'''
|
|
|
|
|
|
|
|
Return:
|
|
|
|
queryset(inventory.Product): A queryset containing items from
|
|
|
|
``product``, with an extra attribute -- remainder = the amount of
|
|
|
|
this item that is remaining.
|
|
|
|
'''
|
|
|
|
|
|
|
|
ids = [product.id for product in products]
|
|
|
|
products = inventory.Product.objects.filter(id__in=ids)
|
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)
|
|
|
|
|
|
|
|
return products
|
|
|
|
|
|
|
|
def user_quantity_remaining(self, user):
|
|
|
|
''' Returns the quantity of this product that the user add in the
|
|
|
|
current cart. '''
|
|
|
|
|
|
|
|
with_remainders = self.attach_user_remainders(user, [self.product])
|
2016-04-02 00:02:01 +00:00
|
|
|
|
2016-04-28 08:57:55 +00:00
|
|
|
return with_remainders[0].remainder
|