@@ -182,74 +182,99 @@ class CartController(object):
voucher = rego.Voucher.objects.get(code=voucher_code.upper())
# It's invalid for a user to enter a voucher that's exhausted
carts_with_voucher = active_carts.filter(vouchers=voucher)
if len(carts_with_voucher) >= voucher.limit:
raise ValidationError("This voucher is no longer available")
# It's not valid for users to re-enter a voucher they already have
user_carts_with_voucher = rego.Cart.objects.filter(
user=self.cart.user,
released=False,
vouchers=voucher,
)
if len(user_carts_with_voucher) > 0:
raise ValidationError("You have already entered this voucher.")
# If successful...
self.cart.vouchers.add(voucher)
self.end_batch()
def validate_cart(self):
''' Determines whether the status of the current cart is valid;
this is normally called before generating or paying an invoice '''
cart = self.cart
user = self.cart.user
errors = []
# TODO: validate vouchers
items = rego.ProductItem.objects.filter(cart=self.cart)
items = rego.ProductItem.objects.filter(cart=cart)
products = set(i.product for i in items)
available = set(ProductController.available_products(
user,
products=products,
))
if products != available:
# Then we have products that aren't available any more.
for product in products:
if product not in available:
message = "%s is no longer available to you." % product
errors.append(ValidationError(message))
product_quantities = list((i.product, i.quantity) for i in items)
self._test_limits(product_quantities)
try:
except ValidationError as ve:
errors.append(ve)
# Validate the discounts
discount_items = rego.DiscountItem.objects.filter(cart=self.cart)
discount_items = rego.DiscountItem.objects.filter(cart=cart)
seen_discounts = set()
for discount_item in discount_items:
discount = discount_item.discount
if discount in seen_discounts:
continue
seen_discounts.add(discount)
real_discount = rego.DiscountBase.objects.get_subclass(
pk=discount.pk)
cond = ConditionController.for_condition(real_discount)
if not cond.is_met(self.cart.user):
raise ValidationError("Discounts are no longer available")
if not cond.is_met(user):
errors.append(
ValidationError("Discounts are no longer available")
if errors:
raise ValidationError(errors)
@transaction.atomic
def recalculate_discounts(self):
''' Calculates all of the discounts available for this product.
NB should be transactional, and it's terribly inefficient.
'''
# Delete the existing entries.
rego.DiscountItem.objects.filter(cart=self.cart).delete()
product_items = self.cart.productitem_set.all()
products = [i.product for i in product_items]
discounts = discount.available_discounts(self.cart.user, [], products)
# The highest-value discounts will apply to the highest-value
# products first.
product_items = product_items.order_by('product__price')
product_items = reversed(product_items)
for item in product_items:
self._add_discount(item.product, item.quantity, discounts)
def _add_discount(self, product, quantity, discounts):
''' Applies the best discounts on the given product, from the given
discounts.'''
def matches(discount):
''' Returns True if and only if the given discount apples to