Changeset - 43649002cb03
[Not reviewed]
0 2 0
Christopher Neugebauer - 8 years ago 2016-09-03 06:18:27
chrisjrn@gmail.com
Makes ProductCondition work if you have both valid and cancelled instances of a product. Fixes #68
2 files changed with 16 insertions and 2 deletions:
0 comments (0 inline, 0 general)
registrasion/controllers/conditions.py
Show inline comments
...
 
@@ -111,133 +111,142 @@ class IsMetByFilter(object):
 
        with a queryset containing only self.condition. '''
 

	
 
        if filtered:
 
            return True  # Why query again?
 

	
 
        return self.passes_filter(user)
 

	
 

	
 
class RemainderSetByFilter(object):
 

	
 
    def user_quantity_remaining(self, user, filtered=True):
 
        ''' returns 0 if the date range is violated, otherwise, it will return
 
        the quantity remaining under the stock limit.
 

	
 
        The filter for this condition must add an annotation called "remainder"
 
        in order for this to work.
 
        '''
 

	
 
        if filtered:
 
            if hasattr(self.condition, "remainder"):
 
                return self.condition.remainder
 

	
 
        # Mark self.condition with a remainder
 
        qs = type(self.condition).objects.filter(pk=self.condition.id)
 
        qs = self.pre_filter(qs, user)
 

	
 
        if len(qs) > 0:
 
            return qs[0].remainder
 
        else:
 
            return 0
 

	
 

	
 
class CategoryConditionController(IsMetByFilter, ConditionController):
 

	
 
    @classmethod
 
    def pre_filter(self, queryset, user):
 
        ''' Returns all of the items from queryset where the user has a
 
        product from a category invoking that item's condition in one of their
 
        carts. '''
 

	
 
        in_user_carts = Q(
 
            enabling_category__product__productitem__cart__user=user
 
        )
 
        released = commerce.Cart.STATUS_RELEASED
 
        in_released_carts = Q(
 
            enabling_category__product__productitem__cart__status=released
 
        )
 
        queryset = queryset.filter(in_user_carts)
 
        queryset = queryset.exclude(in_released_carts)
 

	
 
        return queryset
 

	
 

	
 
class ProductConditionController(IsMetByFilter, ConditionController):
 
    ''' Condition tests for ProductFlag and
 
    IncludedProductDiscount. '''
 

	
 
    @classmethod
 
    def pre_filter(self, queryset, user):
 
        ''' Returns all of the items from queryset where the user has a
 
        product invoking that item's condition in one of their carts. '''
 

	
 
        in_user_carts = Q(enabling_products__productitem__cart__user=user)
 
        released = commerce.Cart.STATUS_RELEASED
 
        paid = commerce.Cart.STATUS_PAID
 
        active = commerce.Cart.STATUS_ACTIVE
 
        in_released_carts = Q(
 
            enabling_products__productitem__cart__status=released
 
        )
 
        not_in_paid_or_active_carts = ~(
 
            Q(enabling_products__productitem__cart__status=paid) |
 
            Q(enabling_products__productitem__cart__status=active)
 
        )
 

	
 
        queryset = queryset.filter(in_user_carts)
 
        queryset = queryset.exclude(in_released_carts)
 
        queryset = queryset.exclude(
 
            in_released_carts & not_in_paid_or_active_carts
 
        )
 

	
 
        return queryset
 

	
 

	
 
class TimeOrStockLimitConditionController(
 
            RemainderSetByFilter,
 
            ConditionController,
 
        ):
 
    ''' Common condition tests for TimeOrStockLimit Flag and
 
    Discount.'''
 

	
 
    @classmethod
 
    def pre_filter(self, queryset, user):
 
        ''' Returns all of the items from queryset where the date falls into
 
        any specified range, but not yet where the stock limit is not yet
 
        reached.'''
 

	
 
        now = timezone.now()
 

	
 
        # Keep items with no start time, or start time not yet met.
 
        queryset = queryset.filter(Q(start_time=None) | Q(start_time__lte=now))
 
        queryset = queryset.filter(Q(end_time=None) | Q(end_time__gte=now))
 

	
 
        # Filter out items that have been reserved beyond the limits
 
        quantity_or_zero = self._calculate_quantities(user)
 

	
 
        remainder = Case(
 
            When(limit=None, then=Value(_BIG_QUANTITY)),
 
            default=F("limit") - Sum(quantity_or_zero),
 
        )
 

	
 
        queryset = queryset.annotate(remainder=remainder)
 
        queryset = queryset.filter(remainder__gt=0)
 

	
 
        return queryset
 

	
 
    @classmethod
 
    def _relevant_carts(cls, user):
 
        reserved_carts = commerce.Cart.reserved_carts()
 
        reserved_carts = reserved_carts.exclude(
 
            user=user,
 
            status=commerce.Cart.STATUS_ACTIVE,
 
        )
 
        return reserved_carts
 

	
 

	
 
class TimeOrStockLimitFlagController(
 
        TimeOrStockLimitConditionController):
 

	
 
    @classmethod
 
    def _calculate_quantities(cls, user):
 
        reserved_carts = cls._relevant_carts(user)
 

	
 
        # Calculate category lines
 
        item_cats = F('categories__product__productitem__product__category')
 
        reserved_category_products = (
 
            Q(categories=item_cats) &
 
            Q(categories__product__productitem__cart__in=reserved_carts)
 
        )
 

	
 
        # Calculate product lines
 
        reserved_products = (
 
            Q(products=F('products__productitem__product')) &
 
            Q(products__productitem__cart__in=reserved_carts)
registrasion/tests/test_flag.py
Show inline comments
...
 
@@ -291,89 +291,94 @@ class FlagTestCases(RegistrationCartTestCase):
 

	
 
        cart_1 = TestingCartController.for_user(self.USER_1)
 

	
 
        cats = CategoryController.available_categories(
 
            self.USER_1,
 
        )
 

	
 
        self.assertFalse(self.CAT_1 in cats)
 
        self.assertTrue(self.CAT_2 in cats)
 

	
 
        cart_1.add_to_cart(self.PROD_3, 1)
 

	
 
        cats = CategoryController.available_categories(
 
            self.USER_1,
 
        )
 

	
 
        self.assertTrue(self.CAT_1 in cats)
 
        self.assertTrue(self.CAT_2 in cats)
 

	
 
    def test_validate_cart_when_flags_become_unmet(self):
 
        self.add_product_flag(condition=conditions.FlagBase.ENABLE_IF_TRUE)
 

	
 
        cart = TestingCartController.for_user(self.USER_1)
 
        cart.add_to_cart(self.PROD_2, 1)
 
        cart.add_to_cart(self.PROD_1, 1)
 

	
 
        # Should pass
 
        cart.validate_cart()
 

	
 
        cart.set_quantity(self.PROD_2, 0)
 

	
 
        # Should fail
 
        with self.assertRaises(ValidationError):
 
            cart.validate_cart()
 

	
 
    def test_fix_simple_errors_resolves_unavailable_products(self):
 
        self.test_validate_cart_when_flags_become_unmet()
 
        cart = TestingCartController.for_user(self.USER_1)
 

	
 
        # Should just remove all of the unavailable products
 
        cart.fix_simple_errors()
 
        # Should now succeed
 
        cart.validate_cart()
 

	
 
        # Should keep PROD_2 in the cart
 
        items = commerce.ProductItem.objects.filter(cart=cart.cart)
 
        self.assertFalse([i for i in items if i.product == self.PROD_1])
 

	
 
    def test_fix_simple_errors_does_not_remove_limited_items(self):
 
        cart = TestingCartController.for_user(self.USER_1)
 

	
 
        cart.add_to_cart(self.PROD_2, 1)
 
        cart.add_to_cart(self.PROD_1, 10)
 

	
 
        # Should just remove all of the unavailable products
 
        cart.fix_simple_errors()
 
        # Should now succeed
 
        cart.validate_cart()
 

	
 
        # Should keep PROD_2 in the cart
 
        # and also PROD_1, which is now exhausted for user.
 
        items = commerce.ProductItem.objects.filter(cart=cart.cart)
 
        self.assertTrue([i for i in items if i.product == self.PROD_1])
 

	
 
    def test_oops(self):
 
    def test_product_stays_enabled_even_if_some_are_cancelled(self):
 
        ''' Flags should be enabled, even if *some* enabling products are cnx.
 
        Tests issue #68.
 
        '''
 

	
 
        self.add_product_flag()
 
        cart1 = TestingCartController.for_user(self.USER_1)
 

	
 
        with self.assertRaises(ValidationError):
 
            # Can't do this without PROD_2
 
            cart1.add_to_cart(self.PROD_1, 1)
 

	
 
        cart1.add_to_cart(self.PROD_2, 1)
 

	
 
        inv = TestingInvoiceController.for_cart(cart1.cart)
 
        inv.pay("Lol", inv.invoice.value)
 

	
 
        cart2 = TestingCartController.for_user(self.USER_1)
 
        cart2.add_to_cart(self.PROD_2, 1)
 

	
 
        inv.refund()
 

	
 
        # Even though cart1 has been cancelled, we have the item in cart2.
 
        # So we should be able to add PROD_1, which depends on PROD_2
 
        cart2.add_to_cart(self.PROD_1, 1)
 

	
 
        cart2.set_quantity(self.PROD_2, 0)
 

	
 
        with self.assertRaises(ValidationError):
 
            cart2.add_to_cart(self.PROD_1, 1)
0 comments (0 inline, 0 general)