Adds logic for required categories

This commit is contained in:
Christopher Neugebauer 2016-03-24 14:19:33 +11:00
parent dcad2d5f7c
commit eff5686dcf
3 changed files with 41 additions and 2 deletions

View file

@ -0,0 +1,20 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('registrasion', '0005_auto_20160323_2141'),
]
operations = [
migrations.AddField(
model_name='category',
name='required',
field=models.BooleanField(default=False),
preserve_default=False,
),
]

View file

@ -132,6 +132,7 @@ class Category(models.Model):
name = models.CharField(max_length=65, verbose_name=_("Name")) name = models.CharField(max_length=65, verbose_name=_("Name"))
description = models.CharField(max_length=255, description = models.CharField(max_length=255,
verbose_name=_("Description")) verbose_name=_("Description"))
required = models.BooleanField(blank=True)
order = models.PositiveIntegerField(verbose_name=("Display order")) order = models.PositiveIntegerField(verbose_name=("Display order"))
render_type = models.IntegerField(choices=CATEGORY_RENDER_TYPES, render_type = models.IntegerField(choices=CATEGORY_RENDER_TYPES,
verbose_name=_("Render type")) verbose_name=_("Render type"))

View file

@ -114,12 +114,30 @@ def product_category(request, category_id):
elif cat_form.is_valid(): elif cat_form.is_valid():
try: try:
handle_valid_cat_form(cat_form, current_cart) handle_valid_cat_form(cat_form, current_cart)
except ValidationError as ve:
pass
# If category is required, the user must have at least one
# in an active+valid cart
if category.required:
carts = rego.Cart.reserved_carts()
carts = carts.filter(user=request.user)
items = rego.ProductItem.objects.filter(
product__category=category,
cart=carts,
)
if len(items) == 0:
cat_form.add_error(
None,
"You must have at least one item from this category",
)
if not cat_form.errors:
if category_id > attendee.highest_complete_category: if category_id > attendee.highest_complete_category:
attendee.highest_complete_category = category_id attendee.highest_complete_category = category_id
attendee.save() attendee.save()
return redirect("dashboard") return redirect("dashboard")
except ValidationError as ve:
pass
else: else:
# Create initial data for each of products in category # Create initial data for each of products in category