Replaces the mandatory/non-mandatory concept with the enabled_if_true/disabled_if_false concept. Closes #4.

This commit is contained in:
Christopher Neugebauer 2016-04-11 20:02:16 +10:00
parent c4c8a7ab82
commit 638ec26126
6 changed files with 138 additions and 58 deletions

View file

@ -107,11 +107,11 @@ class ConditionController(object):
else: else:
all_conditions = [] all_conditions = []
# All mandatory conditions on a product need to be met # All disable-if-false conditions on a product need to be met
mandatory = defaultdict(lambda: True) do_not_disable = defaultdict(lambda: True)
# At least one non-mandatory condition on a product must be met # At least one enable-if-true condition on a product must be met
# if there are no mandatory conditions do_enable = defaultdict(lambda: False)
non_mandatory = defaultdict(lambda: False) # (if either sort of condition is present)
messages = {} messages = {}
@ -146,22 +146,23 @@ class ConditionController(object):
message = base % {"items": items, "remainder": remainder} message = base % {"items": items, "remainder": remainder}
for product in all_products: for product in all_products:
if condition.mandatory: if condition.is_disable_if_false:
mandatory[product] &= met do_not_disable[product] &= met
else: else:
non_mandatory[product] |= met do_enable[product] |= met
if not met and product not in messages: if not met and product not in messages:
messages[product] = message messages[product] = message
valid = defaultdict(lambda: True) valid = {}
for product in itertools.chain(mandatory, non_mandatory): for product in itertools.chain(do_not_disable, do_enable):
if product in mandatory: if product in do_enable:
# If there's a mandatory condition, all must be met # If there's an enable-if-true, we need need of those met too.
valid[product] = mandatory[product] # (do_not_disable will default to true otherwise)
else: valid[product] = do_not_disable[product] and do_enable[product]
# Otherwise, we need just one non-mandatory condition met elif product in do_not_disable:
valid[product] = non_mandatory[product] # If there's a disable-if-false condition, all must be met
valid[product] = do_not_disable[product]
error_fields = [ error_fields = [
(product, messages[product]) (product, messages[product])

View file

@ -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'),
),
]

View file

@ -366,16 +366,28 @@ class RoleDiscount(object):
pass pass
class FlagBase(object):
''' This will replace EnablingConditionBase once it's ready. '''
DISABLE_IF_FALSE = 1
ENABLE_IF_TRUE = 2
@python_2_unicode_compatible @python_2_unicode_compatible
class EnablingConditionBase(models.Model): class EnablingConditionBase(models.Model):
''' This defines a condition which allows products or categories to ''' This defines a condition which allows products or categories to
be made visible. If there is at least one mandatory enabling condition be made visible, or be prevented from being visible.
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. '''
# 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. # https://code.djangoproject.com/ticket/26488 is solved.
objects = InheritanceManager() objects = InheritanceManager()
@ -384,27 +396,43 @@ class EnablingConditionBase(models.Model):
return self.description return self.description
def effects(self): 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()) 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) description = models.CharField(max_length=255)
mandatory = models.BooleanField( condition = models.IntegerField(
default=False, default=FlagBase.ENABLE_IF_TRUE,
help_text=_("If there is at least one mandatory condition defined on " choices=(
"a product or category, all such conditions must be met. " (FlagBase.DISABLE_IF_FALSE, _("Disable if false")),
"Otherwise, at least one non-mandatory condition must be " (FlagBase.ENABLE_IF_TRUE, _("Enable if true")),
"met."), ),
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( products = models.ManyToManyField(
Product, Product,
blank=True, 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( categories = models.ManyToManyField(
Category, Category,
blank=True, blank=True,
help_text=_("Categories whose products are enabled if this condition " help_text=_("Categories whose products are affected by this flag's "
"is met."), "condition."
),
) )

View file

@ -97,7 +97,7 @@ class RegistrationCartTestCase(SetTimeMixin, TestCase):
def make_ceiling(cls, name, limit=None, start_time=None, end_time=None): def make_ceiling(cls, name, limit=None, start_time=None, end_time=None):
limit_ceiling = rego.TimeOrStockLimitFlag.objects.create( limit_ceiling = rego.TimeOrStockLimitFlag.objects.create(
description=name, description=name,
mandatory=True, condition=rego.FlagBase.DISABLE_IF_FALSE,
limit=limit, limit=limit,
start_time=start_time, start_time=start_time,
end_time=end_time end_time=end_time
@ -111,7 +111,7 @@ class RegistrationCartTestCase(SetTimeMixin, TestCase):
cls, name, limit=None, start_time=None, end_time=None): cls, name, limit=None, start_time=None, end_time=None):
limit_ceiling = rego.TimeOrStockLimitFlag.objects.create( limit_ceiling = rego.TimeOrStockLimitFlag.objects.create(
description=name, description=name,
mandatory=True, condition=rego.FlagBase.DISABLE_IF_FALSE,
limit=limit, limit=limit,
start_time=start_time, start_time=start_time,
end_time=end_time end_time=end_time

View file

@ -15,12 +15,12 @@ UTC = pytz.timezone('UTC')
class FlagTestCases(RegistrationCartTestCase): class FlagTestCases(RegistrationCartTestCase):
@classmethod @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 ''' Adds a product enabling condition: adding PROD_1 to a cart is
predicated on adding PROD_2 beforehand. ''' predicated on adding PROD_2 beforehand. '''
flag = rego.ProductFlag.objects.create( flag = rego.ProductFlag.objects.create(
description="Product condition", description="Product condition",
mandatory=mandatory, condition=condition,
) )
flag.save() flag.save()
flag.products.add(cls.PROD_1) flag.products.add(cls.PROD_1)
@ -28,24 +28,24 @@ class FlagTestCases(RegistrationCartTestCase):
flag.save() flag.save()
@classmethod @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: ''' Adds a product enabling condition that operates on a category:
adding an item from CAT_1 is predicated on adding PROD_3 beforehand ''' adding an item from CAT_1 is predicated on adding PROD_3 beforehand '''
flag = rego.ProductFlag.objects.create( flag = rego.ProductFlag.objects.create(
description="Product condition", description="Product condition",
mandatory=mandatory, condition=condition,
) )
flag.save() flag.save()
flag.categories.add(cls.CAT_1) flag.categories.add(cls.CAT_1)
flag.enabling_products.add(cls.PROD_3) flag.enabling_products.add(cls.PROD_3)
flag.save() 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 ''' Adds a category enabling condition: adding PROD_1 to a cart is
predicated on adding an item from CAT_2 beforehand.''' predicated on adding an item from CAT_2 beforehand.'''
flag = rego.CategoryFlag.objects.create( flag = rego.CategoryFlag.objects.create(
description="Category condition", description="Category condition",
mandatory=mandatory, condition=condition,
enabling_category=cls.CAT_2, enabling_category=cls.CAT_2,
) )
flag.save() flag.save()
@ -110,7 +110,7 @@ class FlagTestCases(RegistrationCartTestCase):
current_cart = TestingCartController.for_user(self.USER_1) current_cart = TestingCartController.for_user(self.USER_1)
current_cart.add_to_cart(self.PROD_1, 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_product_flag()
self.add_category_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_3, 1)
cart_2.add_to_cart(self.PROD_1, 1) cart_2.add_to_cart(self.PROD_1, 1)
def test_multiple_mandatory_conditions(self): def test_multiple_dif_conditions(self):
self.add_product_flag(mandatory=True) self.add_product_flag(condition=rego.FlagBase.DISABLE_IF_FALSE)
self.add_category_flag(mandatory=True) self.add_category_flag(condition=rego.FlagBase.DISABLE_IF_FALSE)
cart_1 = TestingCartController.for_user(self.USER_1) cart_1 = TestingCartController.for_user(self.USER_1)
# Cannot add PROD_1 until both conditions are met # 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_3, 1) # Meets the category condition
cart_1.add_to_cart(self.PROD_1, 1) cart_1.add_to_cart(self.PROD_1, 1)
def test_mandatory_conditions_are_mandatory(self): def test_eit_and_dif_conditions_work_together(self):
self.add_product_flag(mandatory=False) self.add_product_flag(condition=rego.FlagBase.ENABLE_IF_TRUE)
self.add_category_flag(mandatory=True) self.add_category_flag(condition=rego.FlagBase.DISABLE_IF_FALSE)
cart_1 = TestingCartController.for_user(self.USER_1) cart_1 = TestingCartController.for_user(self.USER_1)
# Cannot add PROD_1 until both conditions are met # Cannot add PROD_1 until both conditions are met
with self.assertRaises(ValidationError): with self.assertRaises(ValidationError):
cart_1.add_to_cart(self.PROD_1, 1) 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): with self.assertRaises(ValidationError):
cart_1.add_to_cart(self.PROD_1, 1) 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) cart_1.add_to_cart(self.PROD_1, 1)
def test_available_products_works_with_no_conditions_set(self): def test_available_products_works_with_no_conditions_set(self):
@ -186,7 +200,7 @@ class FlagTestCases(RegistrationCartTestCase):
self.assertTrue(self.PROD_4 in prods) self.assertTrue(self.PROD_4 in prods)
def test_available_products_on_category_works_when_condition_not_met(self): 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( prods = ProductController.available_products(
self.USER_1, self.USER_1,
@ -197,7 +211,7 @@ class FlagTestCases(RegistrationCartTestCase):
self.assertTrue(self.PROD_2 in prods) self.assertTrue(self.PROD_2 in prods)
def test_available_products_on_category_works_when_condition_is_met(self): 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 = TestingCartController.for_user(self.USER_1)
cart_1.add_to_cart(self.PROD_2, 1) cart_1.add_to_cart(self.PROD_2, 1)
@ -211,7 +225,7 @@ class FlagTestCases(RegistrationCartTestCase):
self.assertTrue(self.PROD_2 in prods) self.assertTrue(self.PROD_2 in prods)
def test_available_products_on_products_works_when_condition_not_met(self): 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( prods = ProductController.available_products(
self.USER_1, self.USER_1,
@ -222,7 +236,7 @@ class FlagTestCases(RegistrationCartTestCase):
self.assertTrue(self.PROD_2 in prods) self.assertTrue(self.PROD_2 in prods)
def test_available_products_on_products_works_when_condition_is_met(self): 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 = TestingCartController.for_user(self.USER_1)
cart_1.add_to_cart(self.PROD_2, 1) cart_1.add_to_cart(self.PROD_2, 1)
@ -236,7 +250,7 @@ class FlagTestCases(RegistrationCartTestCase):
self.assertTrue(self.PROD_2 in prods) self.assertTrue(self.PROD_2 in prods)
def test_category_flag_fails_if_cart_refunded(self): 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 = TestingCartController.for_user(self.USER_1)
cart.add_to_cart(self.PROD_3, 1) cart.add_to_cart(self.PROD_3, 1)
@ -254,7 +268,7 @@ class FlagTestCases(RegistrationCartTestCase):
cart_2.set_quantity(self.PROD_1, 1) cart_2.set_quantity(self.PROD_1, 1)
def test_product_flag_fails_if_cart_refunded(self): 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 = TestingCartController.for_user(self.USER_1)
cart.add_to_cart(self.PROD_2, 1) cart.add_to_cart(self.PROD_2, 1)
@ -272,7 +286,7 @@ class FlagTestCases(RegistrationCartTestCase):
cart_2.set_quantity(self.PROD_1, 1) cart_2.set_quantity(self.PROD_1, 1)
def test_available_categories(self): 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) cart_1 = TestingCartController.for_user(self.USER_1)
@ -293,7 +307,7 @@ class FlagTestCases(RegistrationCartTestCase):
self.assertTrue(self.CAT_2 in cats) self.assertTrue(self.CAT_2 in cats)
def test_validate_cart_when_flags_become_unmet(self): 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 = TestingCartController.for_user(self.USER_1)
cart.add_to_cart(self.PROD_2, 1) cart.add_to_cart(self.PROD_2, 1)

View file

@ -61,7 +61,7 @@ class VoucherTestCases(RegistrationCartTestCase):
flag = rego.VoucherFlag.objects.create( flag = rego.VoucherFlag.objects.create(
description="Voucher condition", description="Voucher condition",
voucher=voucher, voucher=voucher,
mandatory=False, condition=rego.FlagBase.ENABLE_IF_TRUE,
) )
flag.save() flag.save()
flag.products.add(self.PROD_1) flag.products.add(self.PROD_1)