From eb530bd4855ee14344ebd238dcd4a2e89ebb73bb Mon Sep 17 00:00:00 2001 From: Christopher Neugebauer Date: Wed, 23 Mar 2016 19:39:07 +1100 Subject: [PATCH] =?UTF-8?q?Adds=20the=20first=20pass=20at=20a=20=E2=80=9Cg?= =?UTF-8?q?uided=E2=80=9D=20registration=20form?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- registrasion/urls.py | 2 ++ registrasion/views.py | 64 ++++++++++++++++++++++++++++++++++--------- 2 files changed, 53 insertions(+), 13 deletions(-) diff --git a/registrasion/urls.py b/registrasion/urls.py index 746d163d..c19b6ab8 100644 --- a/registrasion/urls.py +++ b/registrasion/urls.py @@ -2,6 +2,8 @@ from django.conf.urls import url, patterns urlpatterns = patterns( "registrasion.views", + url(r"^register$", "guided_registration", name="guided_registration"), + url(r"^register/([0-9]+)$", "guided_registration", name="guided_registration"), url(r"^category/([0-9]+)$", "product_category", name="product_category"), url(r"^checkout$", "checkout", name="checkout"), url(r"^invoice/([0-9]+)$", "invoice", name="invoice"), diff --git a/registrasion/views.py b/registrasion/views.py index 5aefbfeb..21e41970 100644 --- a/registrasion/views.py +++ b/registrasion/views.py @@ -12,9 +12,45 @@ from django.shortcuts import redirect from django.shortcuts import render +@login_required +def guided_registration(request, page_id=0): + ''' Goes through the registration process in order, + making sure user sees all valid categories. + + WORK IN PROGRESS: the finalised version of this view will allow + grouping of categories into a specific page. Currently, page_id simply + refers to the category_id. Future versions will have pages containing + categories. + ''' + + page_id = int(page_id) + if page_id != 0: + ret = product_category_inner(request, page_id) + if ret is not True: + return ret + + # Go to next page in the guided registration + cats = rego.Category.objects + cats = cats.filter(id__gt=page_id).order_by("order") + + if len(cats) > 0: + return redirect("guided_registration", cats[0].id) + else: + return redirect("dashboard") + @login_required def product_category(request, category_id): - ''' Registration selections form for a specific category of items ''' + ret = product_category_inner(request, category_id) + if ret is not True: + return ret + else: + return redirect("dashboard") + +def product_category_inner(request, category_id): + ''' Registration selections form for a specific category of items. + It returns a rendered template if this page needs to display stuff, + otherwise it returns True. + ''' PRODUCTS_FORM_PREFIX = "products" VOUCHERS_FORM_PREFIX = "vouchers" @@ -41,20 +77,11 @@ def product_category(request, category_id): current_cart.apply_voucher(voucher) except Exception as e: voucher_form.add_error("voucher", e) + # Re-visit current page. elif cat_form.is_valid(): try: - with transaction.atomic(): - for product_id, quantity, field_name \ - in cat_form.product_quantities(): - product = rego.Product.objects.get(pk=product_id) - try: - current_cart.set_quantity( - product, quantity, batched=True) - except ValidationError as ve: - cat_form.add_error(field_name, ve) - if cat_form.errors: - raise ValidationError("Cannot add that stuff") - current_cart.end_batch() + handle_valid_cat_form(cat_form, current_cart) + return True except ValidationError as ve: pass @@ -89,6 +116,17 @@ def product_category(request, category_id): return render(request, "product_category.html", data) +@transaction.atomic +def handle_valid_cat_form(cat_form, current_cart): + for product_id, quantity, field_name in cat_form.product_quantities(): + product = rego.Product.objects.get(pk=product_id) + try: + current_cart.set_quantity(product, quantity, batched=True) + except ValidationError as ve: + cat_form.add_error(field_name, ve) + if cat_form.errors: + raise ValidationError("Cannot add that stuff") + current_cart.end_batch() @login_required def checkout(request):