Files
@ 8ad265a65a08
Branch filter:
Location: symposion_app/registrasion/controllers/conditions.py - annotation
8ad265a65a08
9.9 KiB
text/x-python
Fixes tests now that $0 invoices pay themselves
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 | 1e7a2abc7f92 1e7a2abc7f92 1e7a2abc7f92 1e7a2abc7f92 1e7a2abc7f92 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 1e7a2abc7f92 1e7a2abc7f92 1e7a2abc7f92 1e7a2abc7f92 1e7a2abc7f92 1e7a2abc7f92 1e7a2abc7f92 1e7a2abc7f92 1e7a2abc7f92 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 224878a10ce3 224878a10ce3 224878a10ce3 224878a10ce3 4d134e95d70b 224878a10ce3 4d134e95d70b 224878a10ce3 224878a10ce3 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 40bc5985f4b8 40bc5985f4b8 40bc5985f4b8 40bc5985f4b8 40bc5985f4b8 40bc5985f4b8 40bc5985f4b8 40bc5985f4b8 40bc5985f4b8 40bc5985f4b8 40bc5985f4b8 40bc5985f4b8 40bc5985f4b8 40bc5985f4b8 40bc5985f4b8 40bc5985f4b8 40bc5985f4b8 40bc5985f4b8 40bc5985f4b8 40bc5985f4b8 1e7a2abc7f92 1e7a2abc7f92 1e7a2abc7f92 1e7a2abc7f92 1e7a2abc7f92 1e7a2abc7f92 1e7a2abc7f92 1e7a2abc7f92 1e7a2abc7f92 1e7a2abc7f92 1e7a2abc7f92 1e7a2abc7f92 1e7a2abc7f92 1e7a2abc7f92 1e7a2abc7f92 1e7a2abc7f92 1e7a2abc7f92 1e7a2abc7f92 1e7a2abc7f92 760996588372 760996588372 1e7a2abc7f92 1e7a2abc7f92 1e7a2abc7f92 1e7a2abc7f92 1e7a2abc7f92 1e7a2abc7f92 1e7a2abc7f92 1e7a2abc7f92 1e7a2abc7f92 1e7a2abc7f92 1e7a2abc7f92 1e7a2abc7f92 1e7a2abc7f92 1e7a2abc7f92 1e7a2abc7f92 1e7a2abc7f92 1e7a2abc7f92 1e7a2abc7f92 40bc5985f4b8 40bc5985f4b8 1e7a2abc7f92 1e7a2abc7f92 1e7a2abc7f92 1e7a2abc7f92 1e7a2abc7f92 1e7a2abc7f92 1e7a2abc7f92 1e7a2abc7f92 1e7a2abc7f92 1e7a2abc7f92 1e7a2abc7f92 1e7a2abc7f92 1e7a2abc7f92 1e7a2abc7f92 1e7a2abc7f92 1e7a2abc7f92 1e7a2abc7f92 1e7a2abc7f92 1e7a2abc7f92 1e7a2abc7f92 1e7a2abc7f92 40bc5985f4b8 40bc5985f4b8 40bc5985f4b8 40bc5985f4b8 40bc5985f4b8 1e7a2abc7f92 1e7a2abc7f92 1e7a2abc7f92 1e7a2abc7f92 1e7a2abc7f92 1e7a2abc7f92 40bc5985f4b8 40bc5985f4b8 40bc5985f4b8 40bc5985f4b8 1e7a2abc7f92 1e7a2abc7f92 1e7a2abc7f92 1e7a2abc7f92 1e7a2abc7f92 1e7a2abc7f92 1e7a2abc7f92 1e7a2abc7f92 1e7a2abc7f92 40bc5985f4b8 40bc5985f4b8 40bc5985f4b8 40bc5985f4b8 40bc5985f4b8 1e7a2abc7f92 1e7a2abc7f92 1e7a2abc7f92 1e7a2abc7f92 1e7a2abc7f92 1e7a2abc7f92 1e7a2abc7f92 1e7a2abc7f92 1e7a2abc7f92 1e7a2abc7f92 1e7a2abc7f92 1e7a2abc7f92 1e7a2abc7f92 1e7a2abc7f92 1e7a2abc7f92 1e7a2abc7f92 1e7a2abc7f92 1e7a2abc7f92 1e7a2abc7f92 1e7a2abc7f92 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 1e7a2abc7f92 d9e433659d01 d9e433659d01 d9e433659d01 cf85af771983 d9e433659d01 69a65ac3ed90 69a65ac3ed90 5716af0afa77 2cbda9172f1e 69a65ac3ed90 5716af0afa77 5716af0afa77 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 1e7a2abc7f92 d9e433659d01 d9e433659d01 d9e433659d01 cf85af771983 5716af0afa77 2cbda9172f1e 69a65ac3ed90 5716af0afa77 5716af0afa77 d9e433659d01 d9e433659d01 d9e433659d01 4d134e95d70b d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 1e7a2abc7f92 1e7a2abc7f92 1e7a2abc7f92 d9e433659d01 d9e433659d01 1e7a2abc7f92 1e7a2abc7f92 d9e433659d01 1e7a2abc7f92 d9e433659d01 1e7a2abc7f92 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 1e7a2abc7f92 1e7a2abc7f92 1e7a2abc7f92 1e7a2abc7f92 d9e433659d01 1e7a2abc7f92 d9e433659d01 1e7a2abc7f92 d9e433659d01 1e7a2abc7f92 1e7a2abc7f92 1e7a2abc7f92 1e7a2abc7f92 1e7a2abc7f92 4d134e95d70b 4d134e95d70b 4d134e95d70b 4d134e95d70b 4d134e95d70b 4d134e95d70b 760996588372 4d134e95d70b 4d134e95d70b 4d134e95d70b 4d134e95d70b 4d134e95d70b 4d134e95d70b 4d134e95d70b 4d134e95d70b 4d134e95d70b d9e433659d01 4d134e95d70b d9e433659d01 4d134e95d70b d9e433659d01 d9e433659d01 4d134e95d70b 4d134e95d70b 4d134e95d70b 4d134e95d70b 4d134e95d70b 4d134e95d70b 4d134e95d70b d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 d9e433659d01 1e7a2abc7f92 d9e433659d01 5716af0afa77 224878a10ce3 69a65ac3ed90 5716af0afa77 5716af0afa77 | import itertools
from collections import defaultdict
from collections import namedtuple
from django.db.models import Sum
from django.utils import timezone
from registrasion import models as rego
ConditionAndRemainder = namedtuple(
"ConditionAndRemainder",
(
"condition",
"remainder",
),
)
class ConditionController(object):
''' Base class for testing conditions that activate EnablingCondition
or Discount objects. '''
def __init__(self):
pass
@staticmethod
def for_condition(condition):
CONTROLLERS = {
rego.CategoryEnablingCondition: CategoryConditionController,
rego.IncludedProductDiscount: ProductConditionController,
rego.ProductEnablingCondition: ProductConditionController,
rego.TimeOrStockLimitDiscount:
TimeOrStockLimitDiscountController,
rego.TimeOrStockLimitEnablingCondition:
TimeOrStockLimitEnablingConditionController,
rego.VoucherDiscount: VoucherConditionController,
rego.VoucherEnablingCondition: VoucherConditionController,
}
try:
return CONTROLLERS[type(condition)](condition)
except KeyError:
return ConditionController()
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"
},
}
@classmethod
def test_enabling_conditions(
cls, user, products=None, product_quantities=None):
''' Evaluates all of the enabling conditions on the given products.
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.
If all enabling conditions pass, an empty list is returned, otherwise
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)
quantities = dict((product, quantity)
for product, quantity in product_quantities)
elif product_quantities is None:
products = set(products)
quantities = {}
# Get the conditions covered by the products themselves
all_conditions = [
product.enablingconditionbase_set.select_subclasses() |
product.category.enablingconditionbase_set.select_subclasses()
for product in products
]
all_conditions = set(itertools.chain(*all_conditions))
# All mandatory conditions on a product need to be met
mandatory = defaultdict(lambda: True)
# At least one non-mandatory condition on a product must be met
# if there are no mandatory conditions
non_mandatory = defaultdict(lambda: False)
messages = {}
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()
from_category = rego.Product.objects.filter(
category__in=condition.categories.all(),
).all()
all_products = set(itertools.chain(cond_products, from_category))
# Remove the products that we aren't asking about
all_products = all_products & products
if quantities:
consumed = sum(quantities[i] for i in all_products)
else:
consumed = 1
met = consumed <= remainder
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}
for product in all_products:
if condition.mandatory:
mandatory[product] &= met
else:
non_mandatory[product] |= met
if not met and product not in messages:
messages[product] = message
valid = defaultdict(lambda: True)
for product in itertools.chain(mandatory, non_mandatory):
if product in mandatory:
# If there's a mandatory condition, all must be met
valid[product] = mandatory[product]
else:
# Otherwise, we need just one non-mandatory condition met
valid[product] = non_mandatory[product]
error_fields = [
(product, messages[product])
for product in valid if not valid[product]
]
return error_fields
def user_quantity_remaining(self, user):
''' Returns the number of items covered by this enabling condition the
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):
''' Returns True if this enabling condition is met, otherwise returns
False.
Either this method, or user_quantity_remaining() must be overridden
in subclasses.
'''
return self.user_quantity_remaining(user) > 0
class CategoryConditionController(ConditionController):
def __init__(self, condition):
self.condition = condition
def is_met(self, user):
''' returns True if the user has a product from a category that invokes
this condition in one of their carts '''
carts = rego.Cart.objects.filter(user=user, released=False)
enabling_products = rego.Product.objects.filter(
category=self.condition.enabling_category,
)
products_count = rego.ProductItem.objects.filter(
cart__in=carts,
product__in=enabling_products,
).count()
return products_count > 0
class ProductConditionController(ConditionController):
''' Condition tests for ProductEnablingCondition and
IncludedProductDiscount. '''
def __init__(self, condition):
self.condition = condition
def is_met(self, user):
''' returns True if the user has a product that invokes this
condition in one of their carts '''
carts = rego.Cart.objects.filter(user=user, released=False)
products_count = rego.ProductItem.objects.filter(
cart__in=carts,
product__in=self.condition.enabling_products.all(),
).count()
return products_count > 0
class TimeOrStockLimitConditionController(ConditionController):
''' Common condition tests for TimeOrStockLimit EnablingCondition and
Discount.'''
def __init__(self, ceiling):
self.ceiling = ceiling
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. '''
# Test date range
if not self._test_date_range():
return 0
return self._get_remaining_stock(user)
def _test_date_range(self):
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
def _get_remaining_stock(self, user):
''' Returns the stock that remains under this ceiling, excluding the
user's current cart. '''
if self.ceiling.limit is None:
return 99999999
# We care about all reserved carts, but not the user's current cart
reserved_carts = rego.Cart.reserved_carts()
reserved_carts = reserved_carts.exclude(
user=user,
active=True,
)
items = self._items()
items = items.filter(cart__in=reserved_carts)
count = items.aggregate(Sum("quantity"))["quantity__sum"] or 0
return self.ceiling.limit - count
class TimeOrStockLimitEnablingConditionController(
TimeOrStockLimitConditionController):
def _items(self):
category_products = rego.Product.objects.filter(
category__in=self.ceiling.categories.all(),
)
products = self.ceiling.products.all() | category_products
product_items = rego.ProductItem.objects.filter(
product__in=products.all(),
)
return product_items
class TimeOrStockLimitDiscountController(TimeOrStockLimitConditionController):
def _items(self):
discount_items = rego.DiscountItem.objects.filter(
discount=self.ceiling,
)
return discount_items
class VoucherConditionController(ConditionController):
''' Condition test for VoucherEnablingCondition and VoucherDiscount.'''
def __init__(self, condition):
self.condition = condition
def is_met(self, user):
''' returns True if the user has the given voucher attached. '''
carts_count = rego.Cart.objects.filter(
user=user,
vouchers=self.condition.voucher,
).count()
return carts_count > 0
|