2016-03-26 02:22:47 +00:00
|
|
|
import itertools
|
|
|
|
|
2016-01-22 06:02:07 +00:00
|
|
|
from django.db.models import Q
|
2016-03-27 01:24:48 +00:00
|
|
|
from django.db.models import Sum
|
2016-01-22 05:01:30 +00:00
|
|
|
from registrasion import models as rego
|
|
|
|
|
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
|
|
|
|
enabling conditions from the given categories.
|
|
|
|
TODO: refactor so that all conditions are tested here and
|
|
|
|
can_add_with_enabling_conditions calls this method. '''
|
|
|
|
if category is None and products is None:
|
|
|
|
raise ValueError("You must provide products or a category")
|
|
|
|
|
|
|
|
if category is not None:
|
|
|
|
all_products = rego.Product.objects.filter(category=category)
|
|
|
|
else:
|
|
|
|
all_products = []
|
|
|
|
|
|
|
|
if products is not None:
|
|
|
|
all_products = itertools.chain(all_products, products)
|
|
|
|
|
2016-03-26 09:43:20 +00:00
|
|
|
out = [
|
2016-03-26 02:22:47 +00:00
|
|
|
product
|
|
|
|
for product in all_products
|
2016-04-02 03:03:25 +00:00
|
|
|
if CategoryController(product.category).user_quantity_remaining(user) > 0
|
2016-04-02 03:20:43 +00:00
|
|
|
if cls(product).user_quantity_remaining(user) > 0
|
2016-03-26 02:22:47 +00:00
|
|
|
if cls(product).can_add_with_enabling_conditions(user, 0)
|
|
|
|
]
|
2016-03-26 09:43:20 +00:00
|
|
|
out.sort(key=lambda product: product.order)
|
|
|
|
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-02 02:14:34 +00:00
|
|
|
carts = rego.Cart.objects.filter(
|
|
|
|
user=user,
|
2016-04-02 03:20:43 +00:00
|
|
|
active=False,
|
2016-04-02 02:14:34 +00:00
|
|
|
released=False,
|
|
|
|
)
|
|
|
|
|
2016-01-22 06:02:07 +00:00
|
|
|
items = rego.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
|
2016-01-22 05:01:30 +00:00
|
|
|
|
|
|
|
def can_add_with_enabling_conditions(self, user, quantity):
|
|
|
|
''' Returns true if the user is able to add _quantity_ to their count
|
|
|
|
of this Product without exceeding the ceilings the product is attached
|
|
|
|
to. '''
|
|
|
|
|
|
|
|
conditions = rego.EnablingConditionBase.objects.filter(
|
|
|
|
Q(products=self.product) | Q(categories=self.product.category)
|
|
|
|
).select_subclasses()
|
|
|
|
|
|
|
|
mandatory_violated = False
|
|
|
|
non_mandatory_met = False
|
|
|
|
|
|
|
|
for condition in conditions:
|
|
|
|
cond = ConditionController.for_condition(condition)
|
|
|
|
met = cond.is_met(user, quantity)
|
|
|
|
|
|
|
|
if condition.mandatory and not met:
|
|
|
|
mandatory_violated = True
|
|
|
|
break
|
|
|
|
if met:
|
|
|
|
non_mandatory_met = True
|
|
|
|
|
|
|
|
if mandatory_violated:
|
|
|
|
# All mandatory conditions must be met
|
|
|
|
return False
|
|
|
|
|
|
|
|
if len(conditions) > 0 and not non_mandatory_met:
|
|
|
|
# If there's any non-mandatory conditions, one must be met
|
|
|
|
return False
|
|
|
|
|
|
|
|
return True
|