2016-03-26 02:22:47 +00:00
|
|
|
import itertools
|
|
|
|
|
2016-03-27 01:24:48 +00:00
|
|
|
from django.db.models import Sum
|
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-02 03:03:25 +00:00
|
|
|
from category import CategoryController
|
2016-01-22 05:01:30 +00:00
|
|
|
from conditions import ConditionController
|
|
|
|
|
|
|
|
|
|
|
|
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-11 22:30:33 +00:00
|
|
|
flag conditions from the given categories.
|
2016-03-26 02:22:47 +00:00
|
|
|
TODO: refactor so that all conditions are tested here and
|
2016-04-11 22:30:33 +00:00
|
|
|
can_add_with_flags calls this method. '''
|
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))
|
|
|
|
|
|
|
|
cat_quants = dict(
|
|
|
|
(
|
|
|
|
category,
|
|
|
|
CategoryController(category).user_quantity_remaining(user),
|
|
|
|
)
|
|
|
|
for category in set(product.category for product in all_products)
|
|
|
|
)
|
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-06 08:28:33 +00:00
|
|
|
if cat_quants[product.category] > 0
|
2016-04-02 03:20:43 +00:00
|
|
|
if cls(product).user_quantity_remaining(user) > 0
|
2016-04-02 07:03:42 +00:00
|
|
|
)
|
|
|
|
|
2016-04-11 22:30:33 +00:00
|
|
|
failed_and_messages = ConditionController.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-02 03:20:43 +00:00
|
|
|
def user_quantity_remaining(self, user):
|
|
|
|
''' Returns the quantity of this product that the user add in the
|
|
|
|
current cart. '''
|
|
|
|
|
|
|
|
prod_limit = self.product.limit_per_user
|
|
|
|
|
|
|
|
if prod_limit is None:
|
|
|
|
# Don't need to run the remaining queries
|
|
|
|
return 999999 # We can do better
|
2016-01-22 05:01:30 +00:00
|
|
|
|
2016-04-22 05:06:24 +00:00
|
|
|
carts = commerce.Cart.objects.filter(
|
2016-04-02 02:14:34 +00:00
|
|
|
user=user,
|
2016-04-25 04:31:25 +00:00
|
|
|
status=commerce.Cart.STATUS_PAID,
|
2016-04-02 02:14:34 +00:00
|
|
|
)
|
|
|
|
|
2016-04-22 05:06:24 +00:00
|
|
|
items = commerce.ProductItem.objects.filter(
|
2016-04-02 00:02:01 +00:00
|
|
|
cart__in=carts,
|
2016-04-02 03:20:43 +00:00
|
|
|
product=self.product,
|
2016-03-27 01:24:48 +00:00
|
|
|
)
|
2016-01-22 05:01:30 +00:00
|
|
|
|
2016-04-02 03:20:43 +00:00
|
|
|
prod_count = items.aggregate(Sum("quantity"))["quantity__sum"] or 0
|
2016-04-02 00:02:01 +00:00
|
|
|
|
2016-04-02 03:20:43 +00:00
|
|
|
return prod_limit - prod_count
|