2016-04-02 05:26:27 +00:00
|
|
|
import itertools
|
2016-04-06 08:28:33 +00:00
|
|
|
import operator
|
2016-04-02 05:26:27 +00:00
|
|
|
|
|
|
|
from collections import defaultdict
|
|
|
|
from collections import namedtuple
|
|
|
|
|
2016-01-22 05:01:30 +00:00
|
|
|
from django.db.models import Sum
|
|
|
|
from django.utils import timezone
|
|
|
|
|
2016-04-22 05:06:24 +00:00
|
|
|
from registrasion.models import commerce
|
|
|
|
from registrasion.models import conditions
|
|
|
|
from registrasion.models import inventory
|
2016-01-22 05:01:30 +00:00
|
|
|
|
|
|
|
|
2016-04-02 05:26:27 +00:00
|
|
|
ConditionAndRemainder = namedtuple(
|
|
|
|
"ConditionAndRemainder",
|
|
|
|
(
|
|
|
|
"condition",
|
|
|
|
"remainder",
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2016-01-22 05:01:30 +00:00
|
|
|
class ConditionController(object):
|
2016-04-11 07:55:00 +00:00
|
|
|
''' Base class for testing conditions that activate Flag
|
2016-01-22 05:01:30 +00:00
|
|
|
or Discount objects. '''
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def for_condition(condition):
|
|
|
|
CONTROLLERS = {
|
2016-04-22 05:06:24 +00:00
|
|
|
conditions.CategoryFlag: CategoryConditionController,
|
|
|
|
conditions.IncludedProductDiscount: ProductConditionController,
|
|
|
|
conditions.ProductFlag: ProductConditionController,
|
|
|
|
conditions.TimeOrStockLimitDiscount:
|
2016-04-03 02:53:36 +00:00
|
|
|
TimeOrStockLimitDiscountController,
|
2016-04-22 05:06:24 +00:00
|
|
|
conditions.TimeOrStockLimitFlag:
|
2016-04-11 07:55:00 +00:00
|
|
|
TimeOrStockLimitFlagController,
|
2016-04-22 05:06:24 +00:00
|
|
|
conditions.VoucherDiscount: VoucherConditionController,
|
|
|
|
conditions.VoucherFlag: VoucherConditionController,
|
2016-01-22 05:01:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
try:
|
|
|
|
return CONTROLLERS[type(condition)](condition)
|
|
|
|
except KeyError:
|
|
|
|
return ConditionController()
|
|
|
|
|
2016-04-06 05:40:16 +00:00
|
|
|
SINGLE = True
|
|
|
|
PLURAL = False
|
|
|
|
NONE = True
|
|
|
|
SOME = False
|
|
|
|
MESSAGE = {
|
|
|
|
NONE: {
|
|
|
|
SINGLE:
|
|
|
|
"%(items)s is no longer available to you",
|
|
|
|
PLURAL:
|
|
|
|
"%(items)s are no longer available to you",
|
|
|
|
},
|
|
|
|
SOME: {
|
|
|
|
SINGLE:
|
|
|
|
"Only %(remainder)d of the following item remains: %(items)s",
|
|
|
|
PLURAL:
|
|
|
|
"Only %(remainder)d of the following items remain: %(items)s"
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2016-04-02 05:26:27 +00:00
|
|
|
@classmethod
|
2016-04-11 22:30:33 +00:00
|
|
|
def test_flags(
|
2016-04-02 05:26:27 +00:00
|
|
|
cls, user, products=None, product_quantities=None):
|
2016-04-11 22:30:33 +00:00
|
|
|
''' Evaluates all of the flag conditions on the given products.
|
2016-04-02 05:26:27 +00:00
|
|
|
|
|
|
|
If `product_quantities` is supplied, the condition is only met if it
|
|
|
|
will permit the sum of the product quantities for all of the products
|
|
|
|
it covers. Otherwise, it will be met if at least one item can be
|
|
|
|
accepted.
|
|
|
|
|
2016-04-11 22:30:33 +00:00
|
|
|
If all flag conditions pass, an empty list is returned, otherwise
|
2016-04-02 05:26:27 +00:00
|
|
|
a list is returned containing all of the products that are *not
|
|
|
|
enabled*. '''
|
|
|
|
|
|
|
|
if products is not None and product_quantities is not None:
|
|
|
|
raise ValueError("Please specify only products or "
|
|
|
|
"product_quantities")
|
|
|
|
elif products is None:
|
|
|
|
products = set(i[0] for i in product_quantities)
|
2016-04-03 03:21:57 +00:00
|
|
|
quantities = dict((product, quantity)
|
|
|
|
for product, quantity in product_quantities)
|
2016-04-02 05:26:27 +00:00
|
|
|
elif product_quantities is None:
|
|
|
|
products = set(products)
|
|
|
|
quantities = {}
|
|
|
|
|
|
|
|
# Get the conditions covered by the products themselves
|
2016-04-06 08:28:33 +00:00
|
|
|
prods = (
|
2016-04-11 22:30:33 +00:00
|
|
|
product.flagbase_set.select_subclasses()
|
2016-04-02 05:26:27 +00:00
|
|
|
for product in products
|
2016-04-06 08:28:33 +00:00
|
|
|
)
|
|
|
|
# Get the conditions covered by their categories
|
|
|
|
cats = (
|
2016-04-11 22:30:33 +00:00
|
|
|
category.flagbase_set.select_subclasses()
|
2016-04-06 08:28:33 +00:00
|
|
|
for category in set(product.category for product in products)
|
|
|
|
)
|
|
|
|
|
|
|
|
if products:
|
|
|
|
# Simplify the query.
|
|
|
|
all_conditions = reduce(operator.or_, itertools.chain(prods, cats))
|
|
|
|
else:
|
|
|
|
all_conditions = []
|
2016-04-02 05:26:27 +00:00
|
|
|
|
2016-04-11 10:02:16 +00:00
|
|
|
# All disable-if-false conditions on a product need to be met
|
|
|
|
do_not_disable = defaultdict(lambda: True)
|
|
|
|
# At least one enable-if-true condition on a product must be met
|
|
|
|
do_enable = defaultdict(lambda: False)
|
|
|
|
# (if either sort of condition is present)
|
2016-04-02 05:26:27 +00:00
|
|
|
|
2016-04-06 05:40:16 +00:00
|
|
|
messages = {}
|
|
|
|
|
2016-04-02 05:26:27 +00:00
|
|
|
for condition in all_conditions:
|
|
|
|
cond = cls.for_condition(condition)
|
|
|
|
remainder = cond.user_quantity_remaining(user)
|
|
|
|
|
|
|
|
# Get all products covered by this condition, and the products
|
|
|
|
# from the categories covered by this condition
|
|
|
|
cond_products = condition.products.all()
|
2016-04-22 05:06:24 +00:00
|
|
|
from_category = inventory.Product.objects.filter(
|
2016-04-02 05:26:27 +00:00
|
|
|
category__in=condition.categories.all(),
|
|
|
|
).all()
|
2016-04-06 08:28:33 +00:00
|
|
|
all_products = cond_products | from_category
|
|
|
|
all_products = all_products.select_related("category")
|
2016-04-02 05:26:27 +00:00
|
|
|
# Remove the products that we aren't asking about
|
2016-04-06 08:28:33 +00:00
|
|
|
all_products = [
|
|
|
|
product
|
|
|
|
for product in all_products
|
|
|
|
if product in products
|
|
|
|
]
|
2016-04-02 05:26:27 +00:00
|
|
|
|
|
|
|
if quantities:
|
|
|
|
consumed = sum(quantities[i] for i in all_products)
|
|
|
|
else:
|
|
|
|
consumed = 1
|
|
|
|
met = consumed <= remainder
|
|
|
|
|
2016-04-06 05:40:16 +00:00
|
|
|
if not met:
|
|
|
|
items = ", ".join(str(product) for product in all_products)
|
|
|
|
base = cls.MESSAGE[remainder == 0][len(all_products) == 1]
|
|
|
|
message = base % {"items": items, "remainder": remainder}
|
|
|
|
|
2016-04-02 05:26:27 +00:00
|
|
|
for product in all_products:
|
2016-04-11 10:02:16 +00:00
|
|
|
if condition.is_disable_if_false:
|
|
|
|
do_not_disable[product] &= met
|
2016-04-02 05:26:27 +00:00
|
|
|
else:
|
2016-04-11 10:02:16 +00:00
|
|
|
do_enable[product] |= met
|
2016-04-02 05:26:27 +00:00
|
|
|
|
2016-04-06 05:40:16 +00:00
|
|
|
if not met and product not in messages:
|
|
|
|
messages[product] = message
|
|
|
|
|
2016-04-11 10:02:16 +00:00
|
|
|
valid = {}
|
|
|
|
for product in itertools.chain(do_not_disable, do_enable):
|
|
|
|
if product in do_enable:
|
|
|
|
# If there's an enable-if-true, we need need of those met too.
|
|
|
|
# (do_not_disable will default to true otherwise)
|
|
|
|
valid[product] = do_not_disable[product] and do_enable[product]
|
|
|
|
elif product in do_not_disable:
|
|
|
|
# If there's a disable-if-false condition, all must be met
|
|
|
|
valid[product] = do_not_disable[product]
|
2016-04-02 05:26:27 +00:00
|
|
|
|
2016-04-06 05:40:16 +00:00
|
|
|
error_fields = [
|
|
|
|
(product, messages[product])
|
|
|
|
for product in valid if not valid[product]
|
|
|
|
]
|
|
|
|
|
2016-04-02 05:26:27 +00:00
|
|
|
return error_fields
|
|
|
|
|
|
|
|
def user_quantity_remaining(self, user):
|
2016-04-11 22:30:33 +00:00
|
|
|
''' Returns the number of items covered by this flag condition the
|
2016-04-02 05:26:27 +00:00
|
|
|
user can add to the current cart. This default implementation returns
|
|
|
|
a big number if is_met() is true, otherwise 0.
|
|
|
|
|
|
|
|
Either this method, or is_met() must be overridden in subclasses.
|
|
|
|
'''
|
|
|
|
|
|
|
|
return 99999999 if self.is_met(user) else 0
|
|
|
|
|
|
|
|
def is_met(self, user):
|
2016-04-11 22:30:33 +00:00
|
|
|
''' Returns True if this flag condition is met, otherwise returns
|
2016-04-02 05:26:27 +00:00
|
|
|
False.
|
|
|
|
|
|
|
|
Either this method, or user_quantity_remaining() must be overridden
|
|
|
|
in subclasses.
|
|
|
|
'''
|
|
|
|
return self.user_quantity_remaining(user) > 0
|
2016-01-22 05:01:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
class CategoryConditionController(ConditionController):
|
|
|
|
|
|
|
|
def __init__(self, condition):
|
|
|
|
self.condition = condition
|
|
|
|
|
2016-04-02 05:26:27 +00:00
|
|
|
def is_met(self, user):
|
2016-01-22 05:01:30 +00:00
|
|
|
''' returns True if the user has a product from a category that invokes
|
|
|
|
this condition in one of their carts '''
|
|
|
|
|
2016-04-22 05:06:24 +00:00
|
|
|
carts = commerce.Cart.objects.filter(user=user, released=False)
|
|
|
|
enabling_products = inventory.Product.objects.filter(
|
2016-04-02 00:02:01 +00:00
|
|
|
category=self.condition.enabling_category,
|
|
|
|
)
|
2016-04-22 05:06:24 +00:00
|
|
|
products_count = commerce.ProductItem.objects.filter(
|
2016-04-02 09:11:31 +00:00
|
|
|
cart__in=carts,
|
2016-04-02 00:02:01 +00:00
|
|
|
product__in=enabling_products,
|
2016-04-02 04:56:27 +00:00
|
|
|
).count()
|
|
|
|
return products_count > 0
|
2016-01-22 05:01:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ProductConditionController(ConditionController):
|
2016-04-11 07:55:00 +00:00
|
|
|
''' Condition tests for ProductFlag and
|
2016-01-22 05:01:30 +00:00
|
|
|
IncludedProductDiscount. '''
|
|
|
|
|
|
|
|
def __init__(self, condition):
|
|
|
|
self.condition = condition
|
|
|
|
|
2016-04-02 05:26:27 +00:00
|
|
|
def is_met(self, user):
|
2016-01-22 05:01:30 +00:00
|
|
|
''' returns True if the user has a product that invokes this
|
|
|
|
condition in one of their carts '''
|
|
|
|
|
2016-04-22 05:06:24 +00:00
|
|
|
carts = commerce.Cart.objects.filter(user=user, released=False)
|
|
|
|
products_count = commerce.ProductItem.objects.filter(
|
2016-04-02 09:11:31 +00:00
|
|
|
cart__in=carts,
|
2016-04-02 00:02:01 +00:00
|
|
|
product__in=self.condition.enabling_products.all(),
|
2016-04-02 04:56:27 +00:00
|
|
|
).count()
|
|
|
|
return products_count > 0
|
2016-01-22 05:01:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
class TimeOrStockLimitConditionController(ConditionController):
|
2016-04-11 07:55:00 +00:00
|
|
|
''' Common condition tests for TimeOrStockLimit Flag and
|
2016-01-22 05:01:30 +00:00
|
|
|
Discount.'''
|
|
|
|
|
|
|
|
def __init__(self, ceiling):
|
|
|
|
self.ceiling = ceiling
|
|
|
|
|
2016-04-02 05:26:27 +00:00
|
|
|
def user_quantity_remaining(self, user):
|
|
|
|
''' returns 0 if the date range is violated, otherwise, it will return
|
|
|
|
the quantity remaining under the stock limit. '''
|
2016-01-22 05:01:30 +00:00
|
|
|
|
|
|
|
# Test date range
|
2016-04-02 05:26:27 +00:00
|
|
|
if not self._test_date_range():
|
|
|
|
return 0
|
2016-01-22 05:01:30 +00:00
|
|
|
|
2016-04-02 05:26:27 +00:00
|
|
|
return self._get_remaining_stock(user)
|
2016-01-22 05:01:30 +00:00
|
|
|
|
2016-04-02 05:26:27 +00:00
|
|
|
def _test_date_range(self):
|
2016-01-22 05:01:30 +00:00
|
|
|
now = timezone.now()
|
|
|
|
|
|
|
|
if self.ceiling.start_time is not None:
|
|
|
|
if now < self.ceiling.start_time:
|
|
|
|
return False
|
|
|
|
|
|
|
|
if self.ceiling.end_time is not None:
|
|
|
|
if now > self.ceiling.end_time:
|
|
|
|
return False
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
2016-04-02 05:26:27 +00:00
|
|
|
def _get_remaining_stock(self, user):
|
|
|
|
''' Returns the stock that remains under this ceiling, excluding the
|
|
|
|
user's current cart. '''
|
|
|
|
|
2016-01-22 05:01:30 +00:00
|
|
|
if self.ceiling.limit is None:
|
2016-04-02 05:26:27 +00:00
|
|
|
return 99999999
|
2016-01-22 05:01:30 +00:00
|
|
|
|
2016-04-02 05:26:27 +00:00
|
|
|
# We care about all reserved carts, but not the user's current cart
|
2016-04-22 05:06:24 +00:00
|
|
|
reserved_carts = commerce.Cart.reserved_carts()
|
2016-04-02 05:26:27 +00:00
|
|
|
reserved_carts = reserved_carts.exclude(
|
|
|
|
user=user,
|
|
|
|
active=True,
|
|
|
|
)
|
|
|
|
|
2016-04-03 02:53:36 +00:00
|
|
|
items = self._items()
|
|
|
|
items = items.filter(cart__in=reserved_carts)
|
|
|
|
count = items.aggregate(Sum("quantity"))["quantity__sum"] or 0
|
|
|
|
|
|
|
|
return self.ceiling.limit - count
|
|
|
|
|
2016-04-03 03:21:57 +00:00
|
|
|
|
2016-04-11 07:55:00 +00:00
|
|
|
class TimeOrStockLimitFlagController(
|
2016-04-03 02:53:36 +00:00
|
|
|
TimeOrStockLimitConditionController):
|
|
|
|
|
|
|
|
def _items(self):
|
2016-04-22 05:06:24 +00:00
|
|
|
category_products = inventory.Product.objects.filter(
|
2016-04-03 02:53:36 +00:00
|
|
|
category__in=self.ceiling.categories.all(),
|
|
|
|
)
|
|
|
|
products = self.ceiling.products.all() | category_products
|
|
|
|
|
2016-04-22 05:06:24 +00:00
|
|
|
product_items = commerce.ProductItem.objects.filter(
|
2016-04-03 02:53:36 +00:00
|
|
|
product__in=products.all(),
|
2016-01-22 05:01:30 +00:00
|
|
|
)
|
2016-04-03 02:53:36 +00:00
|
|
|
return product_items
|
2016-01-22 05:01:30 +00:00
|
|
|
|
|
|
|
|
2016-04-03 02:53:36 +00:00
|
|
|
class TimeOrStockLimitDiscountController(TimeOrStockLimitConditionController):
|
|
|
|
|
|
|
|
def _items(self):
|
2016-04-22 05:06:24 +00:00
|
|
|
discount_items = commerce.DiscountItem.objects.filter(
|
2016-04-03 02:53:36 +00:00
|
|
|
discount=self.ceiling,
|
|
|
|
)
|
|
|
|
return discount_items
|
2016-01-22 05:01:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
class VoucherConditionController(ConditionController):
|
2016-04-11 07:55:00 +00:00
|
|
|
''' Condition test for VoucherFlag and VoucherDiscount.'''
|
2016-01-22 05:01:30 +00:00
|
|
|
|
|
|
|
def __init__(self, condition):
|
|
|
|
self.condition = condition
|
|
|
|
|
2016-04-02 05:26:27 +00:00
|
|
|
def is_met(self, user):
|
2016-01-22 05:01:30 +00:00
|
|
|
''' returns True if the user has the given voucher attached. '''
|
2016-04-22 05:06:24 +00:00
|
|
|
carts_count = commerce.Cart.objects.filter(
|
2016-01-22 06:02:07 +00:00
|
|
|
user=user,
|
2016-04-02 00:02:01 +00:00
|
|
|
vouchers=self.condition.voucher,
|
2016-04-02 04:56:27 +00:00
|
|
|
).count()
|
|
|
|
return carts_count > 0
|