Replaces the mandatory/non-mandatory concept with the enabled_if_true/disabled_if_false concept. Closes #4.
This commit is contained in:
parent
c4c8a7ab82
commit
638ec26126
6 changed files with 138 additions and 58 deletions
|
@ -107,11 +107,11 @@ class ConditionController(object):
|
|||
else:
|
||||
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)
|
||||
# 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)
|
||||
|
||||
messages = {}
|
||||
|
||||
|
@ -146,22 +146,23 @@ class ConditionController(object):
|
|||
message = base % {"items": items, "remainder": remainder}
|
||||
|
||||
for product in all_products:
|
||||
if condition.mandatory:
|
||||
mandatory[product] &= met
|
||||
if condition.is_disable_if_false:
|
||||
do_not_disable[product] &= met
|
||||
else:
|
||||
non_mandatory[product] |= met
|
||||
do_enable[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]
|
||||
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]
|
||||
|
||||
error_fields = [
|
||||
(product, messages[product])
|
||||
|
|
|
@ -0,0 +1,37 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
# Generated by Django 1.9.2 on 2016-04-11 10:46
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
replaces = [('registrasion', '0023_auto_20160411_1001'), ('registrasion', '0024_auto_20160411_1002')]
|
||||
|
||||
dependencies = [
|
||||
('registrasion', '0022_auto_20160411_0806'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AlterField(
|
||||
model_name='enablingconditionbase',
|
||||
name='categories',
|
||||
field=models.ManyToManyField(blank=True, help_text="Categories whose products are affected by this flag's condition.", to=b'registrasion.Category'),
|
||||
),
|
||||
migrations.RenameField(
|
||||
model_name='enablingconditionbase',
|
||||
old_name='mandatory',
|
||||
new_name='condition',
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='enablingconditionbase',
|
||||
name='condition',
|
||||
field=models.IntegerField(choices=[(1, 'Disable if false'), (2, 'Enable if true')], default=2, help_text="If there is at least one 'disable if false' flag defined on a product or category, all such flag conditions must be met. If there is at least one 'enable if true' flag, at least one such condition must be met. If both types of conditions exist on a product, both of these rules apply."),
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='enablingconditionbase',
|
||||
name='products',
|
||||
field=models.ManyToManyField(blank=True, help_text="Products affected by this flag's condition.", to=b'registrasion.Product'),
|
||||
),
|
||||
]
|
|
@ -366,16 +366,28 @@ class RoleDiscount(object):
|
|||
pass
|
||||
|
||||
|
||||
class FlagBase(object):
|
||||
''' This will replace EnablingConditionBase once it's ready. '''
|
||||
|
||||
DISABLE_IF_FALSE = 1
|
||||
ENABLE_IF_TRUE = 2
|
||||
|
||||
|
||||
@python_2_unicode_compatible
|
||||
class EnablingConditionBase(models.Model):
|
||||
''' This defines a condition which allows products or categories to
|
||||
be made visible. If there is at least one mandatory enabling condition
|
||||
defined on a Product or Category, it will only be enabled if *all*
|
||||
mandatory conditions are met, otherwise, if there is at least one enabling
|
||||
condition defined on a Product or Category, it will only be enabled if at
|
||||
least one condition is met. '''
|
||||
be made visible, or be prevented from being visible.
|
||||
|
||||
# TODO: rename to EnablingConditionBase once
|
||||
The various subclasses of this can define the conditions that enable
|
||||
or disable products, by the following rules:
|
||||
|
||||
If there is at least one 'disable if false' flag defined on a product or
|
||||
category, all such flag conditions must be met. If there is at least one
|
||||
'enable if true' flag, at least one such condition must be met.
|
||||
|
||||
If both types of conditions exist on a product, both of these rules apply.
|
||||
'''
|
||||
# TODO: rename to FlagBase once
|
||||
# https://code.djangoproject.com/ticket/26488 is solved.
|
||||
|
||||
objects = InheritanceManager()
|
||||
|
@ -384,27 +396,43 @@ class EnablingConditionBase(models.Model):
|
|||
return self.description
|
||||
|
||||
def effects(self):
|
||||
''' Returns all of the items enabled by this condition. '''
|
||||
''' Returns all of the items affected by this condition. '''
|
||||
return itertools.chain(self.products.all(), self.categories.all())
|
||||
|
||||
@property
|
||||
def is_disable_if_false(self):
|
||||
return self.condition == FlagBase.DISABLE_IF_FALSE
|
||||
|
||||
@property
|
||||
def is_enable_if_true(self):
|
||||
return self.condition == FlagBase.ENABLE_IF_TRUE
|
||||
|
||||
description = models.CharField(max_length=255)
|
||||
mandatory = models.BooleanField(
|
||||
default=False,
|
||||
help_text=_("If there is at least one mandatory condition defined on "
|
||||
"a product or category, all such conditions must be met. "
|
||||
"Otherwise, at least one non-mandatory condition must be "
|
||||
"met."),
|
||||
condition = models.IntegerField(
|
||||
default=FlagBase.ENABLE_IF_TRUE,
|
||||
choices=(
|
||||
(FlagBase.DISABLE_IF_FALSE, _("Disable if false")),
|
||||
(FlagBase.ENABLE_IF_TRUE, _("Enable if true")),
|
||||
),
|
||||
help_text=_("If there is at least one 'disable if false' flag "
|
||||
"defined on a product or category, all such flag "
|
||||
" conditions must be met. If there is at least one "
|
||||
"'enable if true' flag, at least one such condition must "
|
||||
"be met. If both types of conditions exist on a product, "
|
||||
"both of these rules apply."
|
||||
),
|
||||
)
|
||||
products = models.ManyToManyField(
|
||||
Product,
|
||||
blank=True,
|
||||
help_text=_("Products that are enabled if this condition is met."),
|
||||
help_text=_("Products affected by this flag's condition."),
|
||||
)
|
||||
categories = models.ManyToManyField(
|
||||
Category,
|
||||
blank=True,
|
||||
help_text=_("Categories whose products are enabled if this condition "
|
||||
"is met."),
|
||||
help_text=_("Categories whose products are affected by this flag's "
|
||||
"condition."
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
|
|
|
@ -97,7 +97,7 @@ class RegistrationCartTestCase(SetTimeMixin, TestCase):
|
|||
def make_ceiling(cls, name, limit=None, start_time=None, end_time=None):
|
||||
limit_ceiling = rego.TimeOrStockLimitFlag.objects.create(
|
||||
description=name,
|
||||
mandatory=True,
|
||||
condition=rego.FlagBase.DISABLE_IF_FALSE,
|
||||
limit=limit,
|
||||
start_time=start_time,
|
||||
end_time=end_time
|
||||
|
@ -111,7 +111,7 @@ class RegistrationCartTestCase(SetTimeMixin, TestCase):
|
|||
cls, name, limit=None, start_time=None, end_time=None):
|
||||
limit_ceiling = rego.TimeOrStockLimitFlag.objects.create(
|
||||
description=name,
|
||||
mandatory=True,
|
||||
condition=rego.FlagBase.DISABLE_IF_FALSE,
|
||||
limit=limit,
|
||||
start_time=start_time,
|
||||
end_time=end_time
|
||||
|
|
|
@ -15,12 +15,12 @@ UTC = pytz.timezone('UTC')
|
|||
class FlagTestCases(RegistrationCartTestCase):
|
||||
|
||||
@classmethod
|
||||
def add_product_flag(cls, mandatory=False):
|
||||
def add_product_flag(cls, condition=rego.FlagBase.ENABLE_IF_TRUE):
|
||||
''' Adds a product enabling condition: adding PROD_1 to a cart is
|
||||
predicated on adding PROD_2 beforehand. '''
|
||||
flag = rego.ProductFlag.objects.create(
|
||||
description="Product condition",
|
||||
mandatory=mandatory,
|
||||
condition=condition,
|
||||
)
|
||||
flag.save()
|
||||
flag.products.add(cls.PROD_1)
|
||||
|
@ -28,24 +28,24 @@ class FlagTestCases(RegistrationCartTestCase):
|
|||
flag.save()
|
||||
|
||||
@classmethod
|
||||
def add_product_flag_on_category(cls, mandatory=False):
|
||||
def add_product_flag_on_category(cls, condition=rego.FlagBase.ENABLE_IF_TRUE):
|
||||
''' Adds a product enabling condition that operates on a category:
|
||||
adding an item from CAT_1 is predicated on adding PROD_3 beforehand '''
|
||||
flag = rego.ProductFlag.objects.create(
|
||||
description="Product condition",
|
||||
mandatory=mandatory,
|
||||
condition=condition,
|
||||
)
|
||||
flag.save()
|
||||
flag.categories.add(cls.CAT_1)
|
||||
flag.enabling_products.add(cls.PROD_3)
|
||||
flag.save()
|
||||
|
||||
def add_category_flag(cls, mandatory=False):
|
||||
def add_category_flag(cls, condition=rego.FlagBase.ENABLE_IF_TRUE):
|
||||
''' Adds a category enabling condition: adding PROD_1 to a cart is
|
||||
predicated on adding an item from CAT_2 beforehand.'''
|
||||
flag = rego.CategoryFlag.objects.create(
|
||||
description="Category condition",
|
||||
mandatory=mandatory,
|
||||
condition=condition,
|
||||
enabling_category=cls.CAT_2,
|
||||
)
|
||||
flag.save()
|
||||
|
@ -110,7 +110,7 @@ class FlagTestCases(RegistrationCartTestCase):
|
|||
current_cart = TestingCartController.for_user(self.USER_1)
|
||||
current_cart.add_to_cart(self.PROD_1, 1)
|
||||
|
||||
def test_multiple_non_mandatory_conditions(self):
|
||||
def test_multiple_eit_conditions(self):
|
||||
self.add_product_flag()
|
||||
self.add_category_flag()
|
||||
|
||||
|
@ -130,9 +130,9 @@ class FlagTestCases(RegistrationCartTestCase):
|
|||
cart_2.add_to_cart(self.PROD_3, 1)
|
||||
cart_2.add_to_cart(self.PROD_1, 1)
|
||||
|
||||
def test_multiple_mandatory_conditions(self):
|
||||
self.add_product_flag(mandatory=True)
|
||||
self.add_category_flag(mandatory=True)
|
||||
def test_multiple_dif_conditions(self):
|
||||
self.add_product_flag(condition=rego.FlagBase.DISABLE_IF_FALSE)
|
||||
self.add_category_flag(condition=rego.FlagBase.DISABLE_IF_FALSE)
|
||||
|
||||
cart_1 = TestingCartController.for_user(self.USER_1)
|
||||
# Cannot add PROD_1 until both conditions are met
|
||||
|
@ -144,18 +144,32 @@ class FlagTestCases(RegistrationCartTestCase):
|
|||
cart_1.add_to_cart(self.PROD_3, 1) # Meets the category condition
|
||||
cart_1.add_to_cart(self.PROD_1, 1)
|
||||
|
||||
def test_mandatory_conditions_are_mandatory(self):
|
||||
self.add_product_flag(mandatory=False)
|
||||
self.add_category_flag(mandatory=True)
|
||||
def test_eit_and_dif_conditions_work_together(self):
|
||||
self.add_product_flag(condition=rego.FlagBase.ENABLE_IF_TRUE)
|
||||
self.add_category_flag(condition=rego.FlagBase.DISABLE_IF_FALSE)
|
||||
|
||||
cart_1 = TestingCartController.for_user(self.USER_1)
|
||||
# Cannot add PROD_1 until both conditions are met
|
||||
with self.assertRaises(ValidationError):
|
||||
cart_1.add_to_cart(self.PROD_1, 1)
|
||||
cart_1.add_to_cart(self.PROD_2, 1) # Meets the product condition
|
||||
|
||||
cart_1.add_to_cart(self.PROD_2, 1) # Meets the EIT condition
|
||||
|
||||
# Need to meet both conditions before you can add
|
||||
with self.assertRaises(ValidationError):
|
||||
cart_1.add_to_cart(self.PROD_1, 1)
|
||||
cart_1.add_to_cart(self.PROD_3, 1) # Meets the category condition
|
||||
|
||||
cart_1.set_quantity(self.PROD_2, 0) # Un-meets the EIT condition
|
||||
|
||||
cart_1.add_to_cart(self.PROD_3, 1) # Meets the DIF condition
|
||||
|
||||
# Need to meet both conditions before you can add
|
||||
with self.assertRaises(ValidationError):
|
||||
cart_1.add_to_cart(self.PROD_1, 1)
|
||||
|
||||
cart_1.add_to_cart(self.PROD_2, 1) # Meets the EIT condition
|
||||
|
||||
# Now that both conditions are met, we can add the product
|
||||
cart_1.add_to_cart(self.PROD_1, 1)
|
||||
|
||||
def test_available_products_works_with_no_conditions_set(self):
|
||||
|
@ -186,7 +200,7 @@ class FlagTestCases(RegistrationCartTestCase):
|
|||
self.assertTrue(self.PROD_4 in prods)
|
||||
|
||||
def test_available_products_on_category_works_when_condition_not_met(self):
|
||||
self.add_product_flag(mandatory=False)
|
||||
self.add_product_flag(condition=rego.FlagBase.ENABLE_IF_TRUE)
|
||||
|
||||
prods = ProductController.available_products(
|
||||
self.USER_1,
|
||||
|
@ -197,7 +211,7 @@ class FlagTestCases(RegistrationCartTestCase):
|
|||
self.assertTrue(self.PROD_2 in prods)
|
||||
|
||||
def test_available_products_on_category_works_when_condition_is_met(self):
|
||||
self.add_product_flag(mandatory=False)
|
||||
self.add_product_flag(condition=rego.FlagBase.ENABLE_IF_TRUE)
|
||||
|
||||
cart_1 = TestingCartController.for_user(self.USER_1)
|
||||
cart_1.add_to_cart(self.PROD_2, 1)
|
||||
|
@ -211,7 +225,7 @@ class FlagTestCases(RegistrationCartTestCase):
|
|||
self.assertTrue(self.PROD_2 in prods)
|
||||
|
||||
def test_available_products_on_products_works_when_condition_not_met(self):
|
||||
self.add_product_flag(mandatory=False)
|
||||
self.add_product_flag(condition=rego.FlagBase.ENABLE_IF_TRUE)
|
||||
|
||||
prods = ProductController.available_products(
|
||||
self.USER_1,
|
||||
|
@ -222,7 +236,7 @@ class FlagTestCases(RegistrationCartTestCase):
|
|||
self.assertTrue(self.PROD_2 in prods)
|
||||
|
||||
def test_available_products_on_products_works_when_condition_is_met(self):
|
||||
self.add_product_flag(mandatory=False)
|
||||
self.add_product_flag(condition=rego.FlagBase.ENABLE_IF_TRUE)
|
||||
|
||||
cart_1 = TestingCartController.for_user(self.USER_1)
|
||||
cart_1.add_to_cart(self.PROD_2, 1)
|
||||
|
@ -236,7 +250,7 @@ class FlagTestCases(RegistrationCartTestCase):
|
|||
self.assertTrue(self.PROD_2 in prods)
|
||||
|
||||
def test_category_flag_fails_if_cart_refunded(self):
|
||||
self.add_category_flag(mandatory=False)
|
||||
self.add_category_flag(condition=rego.FlagBase.ENABLE_IF_TRUE)
|
||||
|
||||
cart = TestingCartController.for_user(self.USER_1)
|
||||
cart.add_to_cart(self.PROD_3, 1)
|
||||
|
@ -254,7 +268,7 @@ class FlagTestCases(RegistrationCartTestCase):
|
|||
cart_2.set_quantity(self.PROD_1, 1)
|
||||
|
||||
def test_product_flag_fails_if_cart_refunded(self):
|
||||
self.add_product_flag(mandatory=False)
|
||||
self.add_product_flag(condition=rego.FlagBase.ENABLE_IF_TRUE)
|
||||
|
||||
cart = TestingCartController.for_user(self.USER_1)
|
||||
cart.add_to_cart(self.PROD_2, 1)
|
||||
|
@ -272,7 +286,7 @@ class FlagTestCases(RegistrationCartTestCase):
|
|||
cart_2.set_quantity(self.PROD_1, 1)
|
||||
|
||||
def test_available_categories(self):
|
||||
self.add_product_flag_on_category(mandatory=False)
|
||||
self.add_product_flag_on_category(condition=rego.FlagBase.ENABLE_IF_TRUE)
|
||||
|
||||
cart_1 = TestingCartController.for_user(self.USER_1)
|
||||
|
||||
|
@ -293,7 +307,7 @@ class FlagTestCases(RegistrationCartTestCase):
|
|||
self.assertTrue(self.CAT_2 in cats)
|
||||
|
||||
def test_validate_cart_when_flags_become_unmet(self):
|
||||
self.add_product_flag(mandatory=False)
|
||||
self.add_product_flag(condition=rego.FlagBase.ENABLE_IF_TRUE)
|
||||
|
||||
cart = TestingCartController.for_user(self.USER_1)
|
||||
cart.add_to_cart(self.PROD_2, 1)
|
||||
|
|
|
@ -61,7 +61,7 @@ class VoucherTestCases(RegistrationCartTestCase):
|
|||
flag = rego.VoucherFlag.objects.create(
|
||||
description="Voucher condition",
|
||||
voucher=voucher,
|
||||
mandatory=False,
|
||||
condition=rego.FlagBase.ENABLE_IF_TRUE,
|
||||
)
|
||||
flag.save()
|
||||
flag.products.add(self.PROD_1)
|
||||
|
|
Loading…
Reference in a new issue