diff --git a/registrasion/migrations/0006_category_required.py b/registrasion/migrations/0006_category_required.py new file mode 100644 index 00000000..46cc6a51 --- /dev/null +++ b/registrasion/migrations/0006_category_required.py @@ -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, + ), + ] diff --git a/registrasion/models.py b/registrasion/models.py index 55d7ee4a..a1101fd9 100644 --- a/registrasion/models.py +++ b/registrasion/models.py @@ -132,6 +132,7 @@ class Category(models.Model): name = models.CharField(max_length=65, verbose_name=_("Name")) description = models.CharField(max_length=255, verbose_name=_("Description")) + required = models.BooleanField(blank=True) order = models.PositiveIntegerField(verbose_name=("Display order")) render_type = models.IntegerField(choices=CATEGORY_RENDER_TYPES, verbose_name=_("Render type")) diff --git a/registrasion/views.py b/registrasion/views.py index da32364e..6288f4b2 100644 --- a/registrasion/views.py +++ b/registrasion/views.py @@ -114,12 +114,30 @@ def product_category(request, category_id): elif cat_form.is_valid(): try: 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: attendee.highest_complete_category = category_id attendee.save() return redirect("dashboard") - except ValidationError as ve: - pass else: # Create initial data for each of products in category