diff --git a/registrasion/views.py b/registrasion/views.py index 251f0fbad679137638d04808f578bdde4d74e4fa..1b0fe64655b38b3c65ef8eaecfa5e0215af34d54 100644 --- a/registrasion/views.py +++ b/registrasion/views.py @@ -52,6 +52,9 @@ def guided_registration(request, page_id=0): through each category one by one ''' + SESSION_KEY = "guided_registration_categories" + ASK_FOR_PROFILE = 777 # Magic number. Meh. + next_step = redirect("guided_registration") sections = [] @@ -71,9 +74,19 @@ def guided_registration(request, page_id=0): except ObjectDoesNotExist: profile = None - if not profile: - # TODO: if voucherform is invalid, make sure - # that profileform does not save + # Figure out if we need to show the profile form and the voucher form + show_profile_and_voucher = False + if SESSION_KEY not in request.session: + if not profile: + show_profile_and_voucher = True + else: + if request.session[SESSION_KEY] == ASK_FOR_PROFILE: + show_profile_and_voucher = True + + if show_profile_and_voucher: + # Keep asking for the profile until everything passes. + request.session[SESSION_KEY] = ASK_FOR_PROFILE + voucher_form, voucher_handled = handle_voucher(request, "voucher") profile_form, profile_handled = handle_profile(request, "profile") @@ -94,19 +107,23 @@ def guided_registration(request, page_id=0): else: # We're selling products - last_category = attendee.highest_complete_category + starting = attendee.guided_categories_complete.count() == 0 # Get the next category cats = rego.Category.objects - cats = cats.filter(id__gt=last_category).order_by("order") + if SESSION_KEY in request.session: + _cats = request.session[SESSION_KEY] + cats = cats.filter(id__in=_cats) + else: + cats = cats.exclude( + id__in=attendee.guided_categories_complete.all(), + ) - if cats.count() == 0: - # We've filled in every category - attendee.completed_registration = True - attendee.save() - return next_step + cats = cats.order_by("order") + + request.session[SESSION_KEY] = [] - if last_category == 0: + if starting: # Only display the first Category title = "Select ticket type" current_step = 2 @@ -125,6 +142,12 @@ def guided_registration(request, page_id=0): products=all_products, )) + if len(available_products) == 0: + # We've filled in every category + attendee.completed_registration = True + attendee.save() + return next_step + for category in cats: products = [ i for i in available_products @@ -141,14 +164,17 @@ def guided_registration(request, page_id=0): discounts=discounts, form=products_form, ) + if products: - # This product category does not exist for this user + # This product category has items to show. sections.append(section) + # Add this to the list of things to show if the form errors. + request.session[SESSION_KEY].append(category.id) - if request.method == "POST" and not products_form.errors: - if category.id > attendee.highest_complete_category: - # This is only saved if we pass each form with no errors. - attendee.highest_complete_category = category.id + if request.method == "POST" and not products_form.errors: + # This is only saved if we pass each form with no errors, + # and if the form actually has products. + attendee.guided_categories_complete.add(category) if sections and request.method == "POST": for section in sections: @@ -156,6 +182,8 @@ def guided_registration(request, page_id=0): break else: attendee.save() + if SESSION_KEY in request.session: + del request.session[SESSION_KEY] # We've successfully processed everything return next_step