Changeset - a4d684f444e7
[Not reviewed]
0 3 1
Christopher Neugebauer - 8 years ago 2016-04-03 05:25:39
chrisjrn@gmail.com
Raises limits errors in the right parts of the form
4 files changed with 39 insertions and 15 deletions:
0 comments (0 inline, 0 general)
registrasion/controllers/cart.py
Show inline comments
...
 
@@ -12,2 +12,3 @@ from django.utils import timezone
 
from registrasion import models as rego
 
from registrasion.exceptions import CartValidationError
 

	
...
 
@@ -116,2 +117,4 @@ class CartController(object):
 

	
 
        errors = []
 

	
 
        # Test each product limit here
...
 
@@ -120,3 +123,3 @@ class CartController(object):
 
                # TODO: batch errors
 
                raise ValidationError("Value must be zero or greater.")
 
                errors.append((product, "Value must be zero or greater."))
 

	
...
 
@@ -127,7 +130,8 @@ class CartController(object):
 
                # TODO: batch errors
 
                raise ValidationError(
 
                errors.append((
 
                    product,
 
                    "You may only have %d of product: %s" % (
 
                        limit, product.name,
 
                        limit, product,
 
                    )
 
                )
 
                ))
 

	
...
 
@@ -139,4 +143,4 @@ class CartController(object):
 
        # Test each category limit here
 
        for cat in by_cat:
 
            ctrl = CategoryController(cat)
 
        for category in by_cat:
 
            ctrl = CategoryController(category)
 
            limit = ctrl.user_quantity_remaining(self.cart.user)
...
 
@@ -144,3 +148,3 @@ class CartController(object):
 
            # 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])
 

	
...
 
@@ -148,7 +152,8 @@ class CartController(object):
 
                # TODO: batch errors
 
                raise ValidationError(
 
                errors.append((
 
                    category,
 
                    "You may only have %d items in category: %s" % (
 
                        limit, cat.name,
 
                        limit, category.name,
 
                    )
 
                )
 
                ))
 

	
...
 
@@ -162,3 +167,8 @@ class CartController(object):
 
            # TODO: batch errors
 
            raise ValidationError("An enabling condition failed")
 
            errors.append(
 
                ("enabling_conditions", "An enabling condition failed")
 
            )
 

	
 
        if errors:
 
            raise CartValidationError(errors)
 

	
registrasion/controllers/category.py
Show inline comments
...
 
@@ -55,2 +55,2 @@ class CategoryController(object):
 
        cat_count = items.aggregate(Sum("quantity"))["quantity__sum"] or 0
 
        cat_limit - cat_count
 
        return cat_limit - cat_count
registrasion/exceptions.py
Show inline comments
 
new file 100644
 
from django.core.exceptions import ValidationError
 

	
 
class CartValidationError(ValidationError):
 
    pass
registrasion/views.py
Show inline comments
...
 
@@ -8,2 +8,3 @@ from registrasion.controllers.invoice import InvoiceController
 
from registrasion.controllers.product import ProductController
 
from registrasion.exceptions import CartValidationError
 

	
...
 
@@ -323,3 +324,3 @@ def set_quantities_from_products_form(products_form, current_cart):
 

	
 
    quantities = products_form.product_quantities()
 
    quantities = list(products_form.product_quantities())
 
    product_quantities = [
...
 
@@ -327,2 +328,5 @@ def set_quantities_from_products_form(products_form, current_cart):
 
    ]
 
    field_names = dict(
 
        (i[0][0], i[1][2]) for i in zip(product_quantities, quantities)
 
    )
 

	
...
 
@@ -330,4 +334,10 @@ def set_quantities_from_products_form(products_form, current_cart):
 
        current_cart.set_quantities(product_quantities)
 
    except ValidationError as ve:
 
        products_form.add_error(None, ve)
 
    except CartValidationError as 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)
 

	
0 comments (0 inline, 0 general)