Raises limits errors in the right parts of the form
This commit is contained in:
parent
7609965883
commit
a4d684f444
4 changed files with 39 additions and 15 deletions
|
@ -10,6 +10,7 @@ from django.db.models import Max
|
||||||
from django.utils import timezone
|
from django.utils import timezone
|
||||||
|
|
||||||
from registrasion import models as rego
|
from registrasion import models as rego
|
||||||
|
from registrasion.exceptions import CartValidationError
|
||||||
|
|
||||||
from category import CategoryController
|
from category import CategoryController
|
||||||
from conditions import ConditionController
|
from conditions import ConditionController
|
||||||
|
@ -114,22 +115,25 @@ class CartController(object):
|
||||||
''' Tests that the quantity changes we intend to make do not violate
|
''' Tests that the quantity changes we intend to make do not violate
|
||||||
the limits and enabling conditions imposed on the products. '''
|
the limits and enabling conditions imposed on the products. '''
|
||||||
|
|
||||||
|
errors = []
|
||||||
|
|
||||||
# Test each product limit here
|
# Test each product limit here
|
||||||
for product, quantity in product_quantities:
|
for product, quantity in product_quantities:
|
||||||
if quantity < 0:
|
if quantity < 0:
|
||||||
# TODO: batch errors
|
# TODO: batch errors
|
||||||
raise ValidationError("Value must be zero or greater.")
|
errors.append((product, "Value must be zero or greater."))
|
||||||
|
|
||||||
prod = ProductController(product)
|
prod = ProductController(product)
|
||||||
limit = prod.user_quantity_remaining(self.cart.user)
|
limit = prod.user_quantity_remaining(self.cart.user)
|
||||||
|
|
||||||
if quantity > limit:
|
if quantity > limit:
|
||||||
# TODO: batch errors
|
# TODO: batch errors
|
||||||
raise ValidationError(
|
errors.append((
|
||||||
|
product,
|
||||||
"You may only have %d of product: %s" % (
|
"You may only have %d of product: %s" % (
|
||||||
limit, product.name,
|
limit, product,
|
||||||
)
|
|
||||||
)
|
)
|
||||||
|
))
|
||||||
|
|
||||||
# Collect by category
|
# Collect by category
|
||||||
by_cat = collections.defaultdict(list)
|
by_cat = collections.defaultdict(list)
|
||||||
|
@ -137,20 +141,21 @@ class CartController(object):
|
||||||
by_cat[product.category].append((product, quantity))
|
by_cat[product.category].append((product, quantity))
|
||||||
|
|
||||||
# Test each category limit here
|
# Test each category limit here
|
||||||
for cat in by_cat:
|
for category in by_cat:
|
||||||
ctrl = CategoryController(cat)
|
ctrl = CategoryController(category)
|
||||||
limit = ctrl.user_quantity_remaining(self.cart.user)
|
limit = ctrl.user_quantity_remaining(self.cart.user)
|
||||||
|
|
||||||
# Get the amount so far in the cart
|
# Get the amount so far in the cart
|
||||||
to_add = sum(i[1] for i in by_cat[cat])
|
to_add = sum(i[1] for i in by_cat[category])
|
||||||
|
|
||||||
if to_add > limit:
|
if to_add > limit:
|
||||||
# TODO: batch errors
|
# TODO: batch errors
|
||||||
raise ValidationError(
|
errors.append((
|
||||||
|
category,
|
||||||
"You may only have %d items in category: %s" % (
|
"You may only have %d items in category: %s" % (
|
||||||
limit, cat.name,
|
limit, category.name,
|
||||||
)
|
|
||||||
)
|
)
|
||||||
|
))
|
||||||
|
|
||||||
# Test the enabling conditions
|
# Test the enabling conditions
|
||||||
errs = ConditionController.test_enabling_conditions(
|
errs = ConditionController.test_enabling_conditions(
|
||||||
|
@ -160,7 +165,12 @@ class CartController(object):
|
||||||
|
|
||||||
if errs:
|
if errs:
|
||||||
# TODO: batch errors
|
# TODO: batch errors
|
||||||
raise ValidationError("An enabling condition failed")
|
errors.append(
|
||||||
|
("enabling_conditions", "An enabling condition failed")
|
||||||
|
)
|
||||||
|
|
||||||
|
if errors:
|
||||||
|
raise CartValidationError(errors)
|
||||||
|
|
||||||
def apply_voucher(self, voucher_code):
|
def apply_voucher(self, voucher_code):
|
||||||
''' Applies the voucher with the given code to this cart. '''
|
''' Applies the voucher with the given code to this cart. '''
|
||||||
|
|
|
@ -53,4 +53,4 @@ class CategoryController(object):
|
||||||
)
|
)
|
||||||
|
|
||||||
cat_count = items.aggregate(Sum("quantity"))["quantity__sum"] or 0
|
cat_count = items.aggregate(Sum("quantity"))["quantity__sum"] or 0
|
||||||
cat_limit - cat_count
|
return cat_limit - cat_count
|
||||||
|
|
4
registrasion/exceptions.py
Normal file
4
registrasion/exceptions.py
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
from django.core.exceptions import ValidationError
|
||||||
|
|
||||||
|
class CartValidationError(ValidationError):
|
||||||
|
pass
|
|
@ -6,6 +6,7 @@ from registrasion.controllers import discount
|
||||||
from registrasion.controllers.cart import CartController
|
from registrasion.controllers.cart import CartController
|
||||||
from registrasion.controllers.invoice import InvoiceController
|
from registrasion.controllers.invoice import InvoiceController
|
||||||
from registrasion.controllers.product import ProductController
|
from registrasion.controllers.product import ProductController
|
||||||
|
from registrasion.exceptions import CartValidationError
|
||||||
|
|
||||||
from collections import namedtuple
|
from collections import namedtuple
|
||||||
|
|
||||||
|
@ -321,15 +322,24 @@ def handle_products(request, category, products, prefix):
|
||||||
|
|
||||||
def set_quantities_from_products_form(products_form, current_cart):
|
def set_quantities_from_products_form(products_form, current_cart):
|
||||||
|
|
||||||
quantities = products_form.product_quantities()
|
quantities = list(products_form.product_quantities())
|
||||||
product_quantities = [
|
product_quantities = [
|
||||||
(rego.Product.objects.get(pk=i[0]), i[1]) for i in quantities
|
(rego.Product.objects.get(pk=i[0]), i[1]) for i in quantities
|
||||||
]
|
]
|
||||||
|
field_names = dict(
|
||||||
|
(i[0][0], i[1][2]) for i in zip(product_quantities, quantities)
|
||||||
|
)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
current_cart.set_quantities(product_quantities)
|
current_cart.set_quantities(product_quantities)
|
||||||
except ValidationError as ve:
|
except CartValidationError as ve:
|
||||||
products_form.add_error(None, ve)
|
for ve_field in ve.error_list:
|
||||||
|
product, message = ve_field.message
|
||||||
|
if product in field_names:
|
||||||
|
field = field_names[product]
|
||||||
|
else:
|
||||||
|
field = None
|
||||||
|
products_form.add_error(field, message)
|
||||||
|
|
||||||
|
|
||||||
def handle_voucher(request, prefix):
|
def handle_voucher(request, prefix):
|
||||||
|
|
Loading…
Reference in a new issue