Changeset - 0d458bea068e
[Not reviewed]
0 3 1
Christopher Neugebauer - 8 years ago 2016-03-27 02:12:33
chrisjrn@gmail.com
Allows Product.limit_per_user to be blank and null. Adds Category.limit_per_user. Adds functionality and tests to verify that this is legal.
4 files changed with 132 insertions and 14 deletions:
0 comments (0 inline, 0 general)
registrasion/controllers/product.py
Show inline comments
 
import itertools
 

	
 
from django.db.models import Q
 
from django.db.models import Sum
 
from registrasion import models as rego
 

	
 
from conditions import ConditionController
 

	
 

	
 
class ProductController(object):
 

	
 
    def __init__(self, product):
 
        self.product = product
 

	
 
    @classmethod
 
    def available_products(cls, user, category=None, products=None):
 
        ''' Returns a list of all of the products that are available per
 
        enabling conditions from the given categories.
 
        TODO: refactor so that all conditions are tested here and
 
        can_add_with_enabling_conditions calls this method. '''
 
        if category is None and products is None:
 
            raise ValueError("You must provide products or a category")
 

	
 
        if category is not None:
 
            all_products = rego.Product.objects.filter(category=category)
 
        else:
 
            all_products = []
 

	
 
        if products is not None:
 
            all_products = itertools.chain(all_products, products)
 

	
 
        out = [
 
            product
 
            for product in all_products
 
            if cls(product).can_add_with_enabling_conditions(user, 0)
 
        ]
 
        out.sort(key=lambda product: product.order)
 
        return out
 

	
 
    def user_can_add_within_limit(self, user, quantity):
 
        ''' Return true if the user is able to add _quantity_ to their count of
 
        this Product without exceeding _limit_per_user_.'''
 

	
 
        carts = rego.Cart.objects.filter(user=user)
 
        items = rego.ProductItem.objects.filter(
 
            product=self.product,
 
            cart=carts)
 
            cart=carts,
 
        )
 

	
 
        count = 0
 
        for item in items:
 
            count += item.quantity
 
        prod_items = items.filter(product=self.product)
 
        cat_items = items.filter(product__category=self.product.category)
 

	
 
        if quantity + count > self.product.limit_per_user:
 
            return False
 
        else:
 
        prod_count = prod_items.aggregate(Sum("quantity"))["quantity__sum"]
 
        cat_count = cat_items.aggregate(Sum("quantity"))["quantity__sum"]
 

	
 
        prod_limit = self.product.limit_per_user
 
        prod_met = prod_limit is None or quantity + prod_count <= prod_limit
 

	
 
        cat_limit = self.product.category.limit_per_user
 
        cat_met = cat_limit is None or quantity + cat_count <= cat_limit
 

	
 
        if prod_met and cat_met:
 
            return True
 
        else:
 
            return False
 

	
 
    def can_add_with_enabling_conditions(self, user, quantity):
 
        ''' Returns true if the user is able to add _quantity_ to their count
 
        of this Product without exceeding the ceilings the product is attached
 
        to. '''
 

	
 
        conditions = rego.EnablingConditionBase.objects.filter(
 
            Q(products=self.product) | Q(categories=self.product.category)
 
        ).select_subclasses()
 

	
 
        mandatory_violated = False
 
        non_mandatory_met = False
 

	
 
        for condition in conditions:
 
            cond = ConditionController.for_condition(condition)
 
            met = cond.is_met(user, quantity)
 

	
 
            if condition.mandatory and not met:
 
                mandatory_violated = True
 
                break
 
            if met:
 
                non_mandatory_met = True
 

	
 
        if mandatory_violated:
 
            # All mandatory conditions must be met
 
            return False
 

	
 
        if len(conditions) > 0 and not non_mandatory_met:
 
            # If there's any non-mandatory conditions, one must be met
 
            return False
 

	
 
        return True
registrasion/migrations/0007_auto_20160326_2105.py
Show inline comments
 
new file 100644
 
# -*- coding: utf-8 -*-
 
from __future__ import unicode_literals
 

	
 
from django.db import migrations, models
 

	
 

	
 
class Migration(migrations.Migration):
 

	
 
    dependencies = [
 
        ('registrasion', '0006_category_required'),
 
    ]
 

	
 
    operations = [
 
        migrations.AddField(
 
            model_name='category',
 
            name='limit_per_user',
 
            field=models.PositiveIntegerField(null=True, verbose_name='Limit per user', blank=True),
 
        ),
 
        migrations.AlterField(
 
            model_name='product',
 
            name='limit_per_user',
 
            field=models.PositiveIntegerField(null=True, verbose_name='Limit per user', blank=True),
 
        ),
 
    ]
registrasion/models.py
Show inline comments
...
 
@@ -87,117 +87,122 @@ class BadgeAndProfile(models.Model):
 
    # Other important Information
 
    name_per_invoice = models.CharField(
 
        verbose_name="Your legal name (for invoicing purposes)",
 
        max_length=64,
 
        help_text="If your legal name is different to the name on your badge, "
 
                  "fill this in, and we'll put it on your invoice. Otherwise, "
 
                  "leave it blank.",
 
        blank=True,
 
        )
 
    of_legal_age = models.BooleanField(
 
        default=False,
 
        verbose_name="18+?",
 
        blank=True,
 
    )
 
    dietary_requirements = models.CharField(
 
        max_length=256,
 
        blank=True,
 
    )
 
    accessibility_requirements = models.CharField(
 
        max_length=256,
 
        blank=True,
 
    )
 
    gender = models.CharField(
 
        max_length=64,
 
        blank=True,
 
    )
 

	
 

	
 
# Inventory Models
 

	
 
@python_2_unicode_compatible
 
class Category(models.Model):
 
    ''' Registration product categories '''
 

	
 
    def __str__(self):
 
        return self.name
 

	
 
    RENDER_TYPE_RADIO = 1
 
    RENDER_TYPE_QUANTITY = 2
 

	
 
    CATEGORY_RENDER_TYPES = [
 
        (RENDER_TYPE_RADIO, _("Radio button")),
 
        (RENDER_TYPE_QUANTITY, _("Quantity boxes")),
 
    ]
 

	
 
    name = models.CharField(max_length=65, verbose_name=_("Name"))
 
    description = models.CharField(max_length=255,
 
                                   verbose_name=_("Description"))
 
    limit_per_user = models.PositiveIntegerField(
 
        null=True,
 
        blank=True,
 
        verbose_name=_("Limit per user"))
 
    required = models.BooleanField(blank=True)
 
    order = models.PositiveIntegerField(verbose_name=("Display order"))
 
    render_type = models.IntegerField(choices=CATEGORY_RENDER_TYPES,
 
                                      verbose_name=_("Render type"))
 

	
 

	
 
@python_2_unicode_compatible
 
class Product(models.Model):
 
    ''' Registration products '''
 

	
 
    def __str__(self):
 
        return self.name
 

	
 
    name = models.CharField(max_length=65, verbose_name=_("Name"))
 
    description = models.CharField(max_length=255,
 
                                   verbose_name=_("Description"))
 
    category = models.ForeignKey(Category, verbose_name=_("Product category"))
 
    price = models.DecimalField(max_digits=8,
 
                                decimal_places=2,
 
                                verbose_name=_("Price"))
 
    limit_per_user = models.PositiveIntegerField(
 
        null=True,
 
        blank=True,
 
        verbose_name=_("Limit per user"))
 
    reservation_duration = models.DurationField(
 
        default=datetime.timedelta(hours=1),
 
        verbose_name=_("Reservation duration"))
 
    order = models.PositiveIntegerField(verbose_name=("Display order"))
 

	
 

	
 
@python_2_unicode_compatible
 
class Voucher(models.Model):
 
    ''' Registration vouchers '''
 

	
 
    # Vouchers reserve a cart for a fixed amount of time, so that
 
    # items may be added without the voucher being swiped by someone else
 
    RESERVATION_DURATION = datetime.timedelta(hours=1)
 

	
 
    def __str__(self):
 
        return "Voucher for %s" % self.recipient
 

	
 
    @classmethod
 
    def normalise_code(cls, code):
 
        return code.upper()
 

	
 
    def save(self, *a, **k):
 
        ''' Normalise the voucher code to be uppercase '''
 
        self.code = self.normalise_code(self.code)
 
        super(Voucher, self).save(*a, **k)
 

	
 
    recipient = models.CharField(max_length=64, verbose_name=_("Recipient"))
 
    code = models.CharField(max_length=16,
 
                            unique=True,
 
                            verbose_name=_("Voucher code"))
 
    limit = models.PositiveIntegerField(verbose_name=_("Voucher use limit"))
 

	
 

	
 
# Product Modifiers
 

	
 
@python_2_unicode_compatible
 
class DiscountBase(models.Model):
 
    ''' Base class for discounts. Each subclass has controller code that
 
    determines whether or not the given discount is available to be added to
 
    the current cart. '''
 

	
 
    objects = InheritanceManager()
 

	
 
    def __str__(self):
 
        return "Discount: " + self.description
 

	
registrasion/tests/test_cart.py
Show inline comments
 
import datetime
 
import pytz
 

	
 
from decimal import Decimal
 
from django.contrib.auth.models import User
 
from django.core.exceptions import ObjectDoesNotExist
 
from django.core.exceptions import ValidationError
 
from django.test import TestCase
 

	
 
from registrasion import models as rego
 
from registrasion.controllers.cart import CartController
 

	
 
from patch_datetime import SetTimeMixin
 

	
 
UTC = pytz.timezone('UTC')
 

	
 

	
 
class RegistrationCartTestCase(SetTimeMixin, TestCase):
 

	
 
    def setUp(self):
 
        super(RegistrationCartTestCase, self).setUp()
 

	
 
    @classmethod
 
    def setUpTestData(cls):
 
        cls.USER_1 = User.objects.create_user(
 
            username='testuser',
 
            email='test@example.com',
 
            password='top_secret')
 

	
 
        cls.USER_2 = User.objects.create_user(
 
            username='testuser2',
 
            email='test2@example.com',
 
            password='top_secret')
 

	
 
        cls.RESERVATION = datetime.timedelta(hours=1)
 

	
 
        cls.categories = []
 
        for i in xrange(3):
 
        for i in xrange(2):
 
            cat = rego.Category.objects.create(
 
                name="Category " + str(i + 1),
 
                description="This is a test category",
 
                order=i,
 
                render_type=rego.Category.RENDER_TYPE_RADIO,
 
                required=False,
 
            )
 
            cat.save()
 
            cls.categories.append(cat)
 

	
 
        cls.CAT_1 = cls.categories[0]
 
        cls.CAT_2 = cls.categories[1]
 
        cls.CAT_3 = cls.categories[2]
 

	
 
        cls.products = []
 
        for i in xrange(6):
 
        for i in xrange(4):
 
            prod = rego.Product.objects.create(
 
                name="Product 1",
 
                description="This is a test product."
 
                description="This is a test product.",
 
                category=cls.categories[i / 2],  # 2 products per category
 
                price=Decimal("10.00"),
 
                reservation_duration=cls.RESERVATION,
 
                limit_per_user=10,
 
                order=1,
 
            )
 
            prod.save()
 
            cls.products.append(prod)
 

	
 
        cls.PROD_1 = cls.products[0]
 
        cls.PROD_2 = cls.products[1]
 
        cls.PROD_3 = cls.products[2]
 
        cls.PROD_4 = cls.products[3]
 
        cls.PROD_5 = cls.products[4]
 
        cls.PROD_6 = cls.products[5]
 

	
 
        cls.PROD_4.price = Decimal("5.00")
 
        cls.PROD_4.save()
 

	
 
    @classmethod
 
    def make_ceiling(cls, name, limit=None, start_time=None, end_time=None):
 
        limit_ceiling = rego.TimeOrStockLimitEnablingCondition.objects.create(
 
            description=name,
 
            mandatory=True,
 
            limit=limit,
 
            start_time=start_time,
 
            end_time=end_time
 
        )
 
        limit_ceiling.save()
 
        limit_ceiling.products.add(cls.PROD_1, cls.PROD_2)
 
        limit_ceiling.save()
 

	
 
    @classmethod
 
    def make_category_ceiling(
 
            cls, name, limit=None, start_time=None, end_time=None):
 
        limit_ceiling = rego.TimeOrStockLimitEnablingCondition.objects.create(
 
            description=name,
 
            mandatory=True,
 
            limit=limit,
 
            start_time=start_time,
 
            end_time=end_time
 
        )
 
        limit_ceiling.save()
 
        limit_ceiling.categories.add(cls.CAT_1)
 
        limit_ceiling.save()
 

	
 
    @classmethod
 
    def make_discount_ceiling(
 
            cls, name, limit=None, start_time=None, end_time=None):
 
        limit_ceiling = rego.TimeOrStockLimitDiscount.objects.create(
 
            description=name,
 
            start_time=start_time,
 
            end_time=end_time,
 
            limit=limit,
 
        )
 
        limit_ceiling.save()
 
        rego.DiscountForProduct.objects.create(
 
            discount=limit_ceiling,
 
            product=cls.PROD_1,
 
            percentage=100,
 
            quantity=10,
 
        ).save()
 

	
...
 
@@ -163,48 +160,131 @@ class BasicCartTests(RegistrationCartTestCase):
 

	
 
        # Setting the quantity to zero should remove the entry from the cart.
 
        current_cart.set_quantity(self.PROD_1, 0)
 
        with self.assertRaises(ObjectDoesNotExist):
 
            get_item()
 

	
 
        current_cart.set_quantity(self.PROD_1, 9)
 
        self.assertEqual(9, get_item().quantity)
 

	
 
        with self.assertRaises(ValidationError):
 
            current_cart.set_quantity(self.PROD_1, 11)
 

	
 
        self.assertEqual(9, get_item().quantity)
 

	
 
        with self.assertRaises(ValidationError):
 
            current_cart.set_quantity(self.PROD_1, -1)
 

	
 
        self.assertEqual(9, get_item().quantity)
 

	
 
        current_cart.set_quantity(self.PROD_1, 2)
 
        self.assertEqual(2, get_item().quantity)
 

	
 
    def test_add_to_cart_product_per_user_limit(self):
 
        current_cart = CartController.for_user(self.USER_1)
 

	
 
        # User should be able to add 1 of PROD_1 to the current cart.
 
        current_cart.add_to_cart(self.PROD_1, 1)
 

	
 
        # User should be able to add 1 of PROD_1 to the current cart.
 
        current_cart.add_to_cart(self.PROD_1, 1)
 

	
 
        # User should not be able to add 10 of PROD_1 to the current cart now,
 
        # because they have a limit of 10.
 
        with self.assertRaises(ValidationError):
 
            current_cart.add_to_cart(self.PROD_1, 10)
 

	
 
        current_cart.cart.active = False
 
        current_cart.cart.save()
 

	
 
        current_cart = CartController.for_user(self.USER_1)
 
        # User should not be able to add 10 of PROD_1 to the current cart now,
 
        # even though it's a new cart.
 
        with self.assertRaises(ValidationError):
 
            current_cart.add_to_cart(self.PROD_1, 10)
 

	
 
        # Second user should not be affected by first user's limits
 
        second_user_cart = CartController.for_user(self.USER_2)
 
        second_user_cart.add_to_cart(self.PROD_1, 10)
 

	
 
    def set_limits(self):
 
        self.CAT_2.limit_per_user = 10
 
        self.PROD_2.limit_per_user = None
 
        self.PROD_3.limit_per_user = None
 
        self.PROD_4.limit_per_user = 6
 

	
 
        self.CAT_2.save()
 
        self.PROD_2.save()
 
        self.PROD_3.save()
 
        self.PROD_4.save()
 

	
 
    def test_per_user_product_limit_ignored_if_blank(self):
 
        self.set_limits()
 

	
 
        current_cart = CartController.for_user(self.USER_1)
 
        # There is no product limit on PROD_2, and there is no cat limit
 
        current_cart.add_to_cart(self.PROD_2, 1)
 
        # There is no product limit on PROD_3, but there is a cat limit
 
        current_cart.add_to_cart(self.PROD_3, 1)
 

	
 
    def test_per_user_category_limit_ignored_if_blank(self):
 
        self.set_limits()
 
        current_cart = CartController.for_user(self.USER_1)
 
        # There is no product limit on PROD_2, and there is no cat limit
 
        current_cart.add_to_cart(self.PROD_2, 1)
 
        # There is no cat limit on PROD_1, but there is a prod limit
 
        current_cart.add_to_cart(self.PROD_1, 1)
 

	
 
    def test_per_user_category_limit_only(self):
 
        self.set_limits()
 

	
 
        current_cart = CartController.for_user(self.USER_1)
 

	
 
        # Cannot add to cart if category limit is filled by one product.
 
        current_cart.set_quantity(self.PROD_3, 10)
 
        with self.assertRaises(ValidationError):
 
            current_cart.set_quantity(self.PROD_4, 1)
 

	
 
        # Can add to cart if category limit is not filled by one product
 
        current_cart.set_quantity(self.PROD_3, 5)
 
        current_cart.set_quantity(self.PROD_4, 5)
 
        # Cannot add to cart if category limit is filled by two products
 
        with self.assertRaises(ValidationError):
 
            current_cart.add_to_cart(self.PROD_3, 1)
 

	
 
        current_cart.cart.active = False
 
        current_cart.cart.save()
 

	
 
        current_cart = CartController.for_user(self.USER_1)
 
        # The category limit should extend across carts
 
        with self.assertRaises(ValidationError):
 
            current_cart.add_to_cart(self.PROD_3, 10)
 

	
 
    def test_per_user_category_and_product_limits(self):
 
        self.set_limits()
 

	
 
        current_cart = CartController.for_user(self.USER_1)
 

	
 
        # Hit both the product and category edges:
 
        current_cart.set_quantity(self.PROD_3, 4)
 
        current_cart.set_quantity(self.PROD_4, 6)
 
        with self.assertRaises(ValidationError):
 
            # There's unlimited PROD_3, but limited in the category
 
            current_cart.add_to_cart(self.PROD_3, 1)
 

	
 
        current_cart.set_quantity(self.PROD_3, 0)
 
        with self.assertRaises(ValidationError):
 
            # There's only 6 allowed of PROD_4
 
            current_cart.add_to_cart(self.PROD_4, 1)
 

	
 
        # The limits should extend across carts...
 
        current_cart.cart.active = False
 
        current_cart.cart.save()
 

	
 
        current_cart = CartController.for_user(self.USER_1)
 
        current_cart.set_quantity(self.PROD_3, 4)
 

	
 
        with self.assertRaises(ValidationError):
 
            current_cart.set_quantity(self.PROD_3, 5)
 

	
 
        with self.assertRaises(ValidationError):
 
            current_cart.set_quantity(self.PROD_4, 1)
0 comments (0 inline, 0 general)