2016-04-01 10:21:09 +00:00
|
|
|
import sys
|
|
|
|
|
2016-03-03 21:40:44 +00:00
|
|
|
from registrasion import forms
|
|
|
|
from registrasion import models as rego
|
2016-03-26 03:03:25 +00:00
|
|
|
from registrasion.controllers import discount
|
2016-03-03 21:40:44 +00:00
|
|
|
from registrasion.controllers.cart import CartController
|
2016-03-04 20:22:01 +00:00
|
|
|
from registrasion.controllers.invoice import InvoiceController
|
2016-03-26 02:30:46 +00:00
|
|
|
from registrasion.controllers.product import ProductController
|
2016-03-03 21:40:44 +00:00
|
|
|
|
2016-04-01 00:43:19 +00:00
|
|
|
from collections import namedtuple
|
|
|
|
|
2016-04-01 10:21:09 +00:00
|
|
|
from django.conf import settings
|
2016-03-03 21:40:44 +00:00
|
|
|
from django.contrib.auth.decorators import login_required
|
2016-04-01 00:43:19 +00:00
|
|
|
from django.contrib import messages
|
2016-03-03 21:40:44 +00:00
|
|
|
from django.core.exceptions import ObjectDoesNotExist
|
2016-03-05 02:01:16 +00:00
|
|
|
from django.core.exceptions import ValidationError
|
2016-03-03 21:40:44 +00:00
|
|
|
from django.db import transaction
|
2016-04-01 05:17:46 +00:00
|
|
|
from django.http import Http404
|
2016-03-04 20:22:01 +00:00
|
|
|
from django.shortcuts import redirect
|
2016-03-03 21:40:44 +00:00
|
|
|
from django.shortcuts import render
|
|
|
|
|
|
|
|
|
2016-04-01 00:43:19 +00:00
|
|
|
GuidedRegistrationSection = namedtuple(
|
|
|
|
"GuidedRegistrationSection",
|
|
|
|
(
|
|
|
|
"title",
|
|
|
|
"discounts",
|
|
|
|
"description",
|
|
|
|
"form",
|
|
|
|
)
|
|
|
|
)
|
|
|
|
GuidedRegistrationSection.__new__.__defaults__ = (
|
|
|
|
(None,) * len(GuidedRegistrationSection._fields)
|
|
|
|
)
|
|
|
|
|
2016-04-01 11:14:39 +00:00
|
|
|
|
2016-04-01 10:21:09 +00:00
|
|
|
def get_form(name):
|
|
|
|
dot = name.rindex(".")
|
|
|
|
mod_name, form_name = name[:dot], name[dot + 1:]
|
|
|
|
__import__(mod_name)
|
|
|
|
return getattr(sys.modules[mod_name], form_name)
|
|
|
|
|
2016-04-01 11:14:39 +00:00
|
|
|
|
2016-03-23 08:39:07 +00:00
|
|
|
@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
|
2016-03-24 02:43:06 +00:00
|
|
|
grouping of categories into a specific page. Currently, it just goes
|
|
|
|
through each category one by one
|
2016-03-23 08:39:07 +00:00
|
|
|
'''
|
|
|
|
|
2016-03-24 02:43:06 +00:00
|
|
|
next_step = redirect("guided_registration")
|
|
|
|
|
2016-04-01 00:43:19 +00:00
|
|
|
sections = []
|
|
|
|
|
2016-03-24 02:43:06 +00:00
|
|
|
attendee = rego.Attendee.get_instance(request.user)
|
2016-04-01 00:43:19 +00:00
|
|
|
|
2016-03-25 01:50:59 +00:00
|
|
|
if attendee.completed_registration:
|
2016-04-01 00:43:19 +00:00
|
|
|
return render(
|
|
|
|
request,
|
|
|
|
"registrasion/guided_registration_complete.html",
|
|
|
|
{},
|
|
|
|
)
|
2016-03-25 01:50:59 +00:00
|
|
|
|
2016-04-01 00:43:19 +00:00
|
|
|
# Step 1: Fill in a badge and collect a voucher code
|
2016-04-01 05:58:55 +00:00
|
|
|
try:
|
2016-04-01 10:21:09 +00:00
|
|
|
profile = attendee.attendeeprofilebase
|
2016-04-01 05:58:55 +00:00
|
|
|
except ObjectDoesNotExist:
|
|
|
|
profile = None
|
2016-03-24 02:43:06 +00:00
|
|
|
|
2016-04-01 05:58:55 +00:00
|
|
|
if not profile:
|
2016-04-01 11:14:39 +00:00
|
|
|
# TODO: if voucherform is invalid, make sure
|
|
|
|
# that profileform does not save
|
2016-04-01 00:43:19 +00:00
|
|
|
voucher_form, voucher_handled = handle_voucher(request, "voucher")
|
|
|
|
profile_form, profile_handled = handle_profile(request, "profile")
|
|
|
|
|
|
|
|
voucher_section = GuidedRegistrationSection(
|
|
|
|
title="Voucher Code",
|
|
|
|
form=voucher_form,
|
|
|
|
)
|
|
|
|
|
|
|
|
profile_section = GuidedRegistrationSection(
|
|
|
|
title="Profile and Personal Information",
|
|
|
|
form=profile_form,
|
|
|
|
)
|
|
|
|
|
|
|
|
title = "Attendee information"
|
|
|
|
current_step = 1
|
|
|
|
sections.append(voucher_section)
|
|
|
|
sections.append(profile_section)
|
|
|
|
else:
|
|
|
|
# We're selling products
|
2016-03-23 08:39:07 +00:00
|
|
|
|
2016-04-01 00:43:19 +00:00
|
|
|
last_category = attendee.highest_complete_category
|
2016-03-24 02:43:06 +00:00
|
|
|
|
2016-04-01 00:43:19 +00:00
|
|
|
# Get the next category
|
|
|
|
cats = rego.Category.objects
|
|
|
|
cats = cats.filter(id__gt=last_category).order_by("order")
|
|
|
|
|
|
|
|
if cats.count() == 0:
|
|
|
|
# We've filled in every category
|
|
|
|
attendee.completed_registration = True
|
|
|
|
attendee.save()
|
|
|
|
return next_step
|
2016-03-24 02:43:06 +00:00
|
|
|
|
2016-04-01 00:43:19 +00:00
|
|
|
if last_category == 0:
|
|
|
|
# Only display the first Category
|
|
|
|
title = "Select ticket type"
|
|
|
|
current_step = 2
|
|
|
|
cats = [cats[0]]
|
|
|
|
else:
|
|
|
|
# Set title appropriately for remaining categories
|
|
|
|
current_step = 3
|
|
|
|
title = "Additional items"
|
|
|
|
|
|
|
|
for category in cats:
|
|
|
|
products = ProductController.available_products(
|
|
|
|
request.user,
|
|
|
|
category=category,
|
|
|
|
)
|
|
|
|
|
2016-04-02 00:33:20 +00:00
|
|
|
if not products:
|
|
|
|
# This product category does not exist for this user
|
|
|
|
continue
|
|
|
|
|
2016-04-01 00:43:19 +00:00
|
|
|
prefix = "category_" + str(category.id)
|
|
|
|
p = handle_products(request, category, products, prefix)
|
|
|
|
products_form, discounts, products_handled = p
|
|
|
|
|
|
|
|
section = GuidedRegistrationSection(
|
|
|
|
title=category.name,
|
|
|
|
description=category.description,
|
|
|
|
discounts=discounts,
|
|
|
|
form=products_form,
|
|
|
|
)
|
|
|
|
sections.append(section)
|
|
|
|
|
|
|
|
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 sections and request.method == "POST":
|
|
|
|
for section in sections:
|
|
|
|
if section.form.errors:
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
attendee.save()
|
|
|
|
# We've successfully processed everything
|
|
|
|
return next_step
|
2016-03-23 08:39:07 +00:00
|
|
|
|
2016-04-01 00:43:19 +00:00
|
|
|
data = {
|
|
|
|
"current_step": current_step,
|
|
|
|
"sections": sections,
|
2016-04-01 11:14:39 +00:00
|
|
|
"title": title,
|
2016-04-01 00:43:19 +00:00
|
|
|
"total_steps": 3,
|
|
|
|
}
|
|
|
|
return render(request, "registrasion/guided_registration.html", data)
|
2016-03-24 02:43:06 +00:00
|
|
|
|
2016-03-23 08:39:07 +00:00
|
|
|
|
2016-03-24 00:33:11 +00:00
|
|
|
@login_required
|
2016-03-24 01:58:23 +00:00
|
|
|
def edit_profile(request):
|
2016-04-01 00:41:59 +00:00
|
|
|
form, handled = handle_profile(request, "profile")
|
|
|
|
|
2016-04-02 00:33:20 +00:00
|
|
|
if handled and not form.errors:
|
|
|
|
messages.success(
|
|
|
|
request,
|
|
|
|
"Your attendee profile was updated.",
|
|
|
|
)
|
|
|
|
return redirect("dashboard")
|
|
|
|
|
2016-04-01 00:41:59 +00:00
|
|
|
data = {
|
|
|
|
"form": form,
|
|
|
|
}
|
|
|
|
return render(request, "registrasion/profile_form.html", data)
|
|
|
|
|
2016-04-01 11:14:39 +00:00
|
|
|
|
2016-04-01 00:41:59 +00:00
|
|
|
def handle_profile(request, prefix):
|
|
|
|
''' Returns a profile form instance, and a boolean which is true if the
|
|
|
|
form was handled. '''
|
2016-03-24 01:58:23 +00:00
|
|
|
attendee = rego.Attendee.get_instance(request.user)
|
|
|
|
|
|
|
|
try:
|
2016-04-01 10:21:09 +00:00
|
|
|
profile = attendee.attendeeprofilebase
|
2016-04-01 11:34:06 +00:00
|
|
|
profile = rego.AttendeeProfileBase.objects.get_subclass(pk=profile.id)
|
2016-03-24 01:58:23 +00:00
|
|
|
except ObjectDoesNotExist:
|
|
|
|
profile = None
|
|
|
|
|
2016-04-01 10:21:09 +00:00
|
|
|
ProfileForm = get_form(settings.ATTENDEE_PROFILE_FORM)
|
|
|
|
|
2016-04-01 10:39:54 +00:00
|
|
|
# Load a pre-entered name from the speaker's profile,
|
|
|
|
# if they have one.
|
|
|
|
try:
|
|
|
|
speaker_profile = request.user.speaker_profile
|
|
|
|
speaker_name = speaker_profile.name
|
|
|
|
except ObjectDoesNotExist:
|
|
|
|
speaker_name = None
|
|
|
|
|
|
|
|
name_field = ProfileForm.Meta.model.name_field()
|
|
|
|
initial = {}
|
2016-04-01 11:34:06 +00:00
|
|
|
if profile is None and name_field is not None:
|
2016-04-01 10:39:54 +00:00
|
|
|
initial[name_field] = speaker_name
|
|
|
|
|
2016-04-01 10:21:09 +00:00
|
|
|
form = ProfileForm(
|
2016-04-01 00:41:59 +00:00
|
|
|
request.POST or None,
|
2016-04-01 10:39:54 +00:00
|
|
|
initial=initial,
|
2016-04-01 00:41:59 +00:00
|
|
|
instance=profile,
|
|
|
|
prefix=prefix
|
|
|
|
)
|
|
|
|
|
|
|
|
handled = True if request.POST else False
|
2016-03-24 01:58:23 +00:00
|
|
|
|
|
|
|
if request.POST and form.is_valid():
|
|
|
|
form.instance.attendee = attendee
|
|
|
|
form.save()
|
2016-03-24 00:33:11 +00:00
|
|
|
|
2016-04-01 00:41:59 +00:00
|
|
|
return form, handled
|
2016-03-25 03:51:39 +00:00
|
|
|
|
2016-04-01 11:14:39 +00:00
|
|
|
|
2016-03-03 21:40:44 +00:00
|
|
|
@login_required
|
|
|
|
def product_category(request, category_id):
|
2016-03-23 08:39:07 +00:00
|
|
|
''' Registration selections form for a specific category of items.
|
|
|
|
'''
|
2016-03-03 21:40:44 +00:00
|
|
|
|
2016-03-23 02:33:33 +00:00
|
|
|
PRODUCTS_FORM_PREFIX = "products"
|
|
|
|
VOUCHERS_FORM_PREFIX = "vouchers"
|
|
|
|
|
2016-03-26 09:21:54 +00:00
|
|
|
# Handle the voucher form *before* listing products.
|
|
|
|
# Products can change as vouchers are entered.
|
|
|
|
v = handle_voucher(request, VOUCHERS_FORM_PREFIX)
|
|
|
|
voucher_form, voucher_handled = v
|
|
|
|
|
2016-03-03 21:40:44 +00:00
|
|
|
category_id = int(category_id) # Routing is [0-9]+
|
|
|
|
category = rego.Category.objects.get(pk=category_id)
|
2016-03-24 02:43:06 +00:00
|
|
|
|
2016-03-26 02:30:46 +00:00
|
|
|
products = ProductController.available_products(
|
|
|
|
request.user,
|
2016-03-26 09:43:20 +00:00
|
|
|
category=category,
|
2016-03-26 02:30:46 +00:00
|
|
|
)
|
2016-03-26 09:01:46 +00:00
|
|
|
|
2016-04-02 00:33:20 +00:00
|
|
|
if not products:
|
|
|
|
messages.warning(
|
|
|
|
request,
|
2016-04-02 02:29:53 +00:00
|
|
|
"There are no products available from category: " + category.name,
|
2016-04-02 00:33:20 +00:00
|
|
|
)
|
|
|
|
return redirect("dashboard")
|
|
|
|
|
2016-03-26 09:43:20 +00:00
|
|
|
p = handle_products(request, category, products, PRODUCTS_FORM_PREFIX)
|
|
|
|
products_form, discounts, products_handled = p
|
|
|
|
|
|
|
|
if request.POST and not voucher_handled and not products_form.errors:
|
|
|
|
# Only return to the dashboard if we didn't add a voucher code
|
|
|
|
# and if there's no errors in the products form
|
2016-04-02 00:33:20 +00:00
|
|
|
messages.success(
|
|
|
|
request,
|
|
|
|
"Your reservations have been updated.",
|
|
|
|
)
|
2016-03-26 09:43:20 +00:00
|
|
|
return redirect("dashboard")
|
|
|
|
|
|
|
|
data = {
|
|
|
|
"category": category,
|
|
|
|
"discounts": discounts,
|
|
|
|
"form": products_form,
|
|
|
|
"voucher_form": voucher_form,
|
|
|
|
}
|
|
|
|
|
2016-03-31 04:44:20 +00:00
|
|
|
return render(request, "registrasion/product_category.html", data)
|
2016-03-26 09:43:20 +00:00
|
|
|
|
|
|
|
|
|
|
|
def handle_products(request, category, products, prefix):
|
|
|
|
''' Handles a products list form in the given request. Returns the
|
|
|
|
form instance, the discounts applicable to this form, and whether the
|
|
|
|
contents were handled. '''
|
|
|
|
|
|
|
|
current_cart = CartController.for_user(request.user)
|
|
|
|
|
2016-03-27 00:18:26 +00:00
|
|
|
ProductsForm = forms.ProductsForm(category, products)
|
2016-03-03 21:40:44 +00:00
|
|
|
|
2016-03-26 09:21:54 +00:00
|
|
|
# Create initial data for each of products in category
|
|
|
|
items = rego.ProductItem.objects.filter(
|
2016-03-26 09:43:20 +00:00
|
|
|
product__in=products,
|
2016-03-26 09:21:54 +00:00
|
|
|
cart=current_cart.cart,
|
|
|
|
)
|
|
|
|
quantities = []
|
|
|
|
for product in products:
|
|
|
|
# Only add items that are enabled.
|
|
|
|
try:
|
|
|
|
quantity = items.get(product=product).quantity
|
|
|
|
except ObjectDoesNotExist:
|
|
|
|
quantity = 0
|
|
|
|
quantities.append((product, quantity))
|
|
|
|
|
2016-03-26 09:43:20 +00:00
|
|
|
products_form = ProductsForm(
|
2016-03-26 09:21:54 +00:00
|
|
|
request.POST or None,
|
|
|
|
product_quantities=quantities,
|
2016-03-26 09:43:20 +00:00
|
|
|
prefix=prefix,
|
2016-03-26 09:21:54 +00:00
|
|
|
)
|
|
|
|
|
2016-03-26 09:43:20 +00:00
|
|
|
if request.method == "POST" and products_form.is_valid():
|
2016-04-02 08:57:17 +00:00
|
|
|
if products_form.has_changed():
|
|
|
|
set_quantities_from_products_form(products_form, current_cart)
|
2016-03-26 09:21:54 +00:00
|
|
|
|
|
|
|
# If category is required, the user must have at least one
|
|
|
|
# in an active+valid cart
|
|
|
|
if category.required:
|
2016-03-26 09:43:20 +00:00
|
|
|
carts = rego.Cart.objects.filter(user=request.user)
|
2016-03-26 09:21:54 +00:00
|
|
|
items = rego.ProductItem.objects.filter(
|
|
|
|
product__category=category,
|
|
|
|
cart=carts,
|
|
|
|
)
|
|
|
|
if len(items) == 0:
|
2016-03-26 09:43:20 +00:00
|
|
|
products_form.add_error(
|
2016-03-26 09:21:54 +00:00
|
|
|
None,
|
|
|
|
"You must have at least one item from this category",
|
2016-03-24 03:19:33 +00:00
|
|
|
)
|
2016-03-26 09:43:20 +00:00
|
|
|
handled = False if products_form.errors else True
|
2016-03-23 02:33:33 +00:00
|
|
|
|
2016-03-26 03:03:25 +00:00
|
|
|
discounts = discount.available_discounts(request.user, [], products)
|
2016-03-26 09:21:54 +00:00
|
|
|
|
2016-03-26 09:43:20 +00:00
|
|
|
return products_form, discounts, handled
|
2016-03-25 03:51:39 +00:00
|
|
|
|
2016-03-27 00:48:17 +00:00
|
|
|
|
2016-03-26 09:43:20 +00:00
|
|
|
def set_quantities_from_products_form(products_form, current_cart):
|
2016-03-31 08:13:31 +00:00
|
|
|
# TODO: issue #8 is a problem here.
|
|
|
|
quantities = list(products_form.product_quantities())
|
|
|
|
quantities.sort(key=lambda item: item[1])
|
2016-04-02 08:57:17 +00:00
|
|
|
|
|
|
|
product_quantities = [
|
|
|
|
(rego.Product.objects.get(pk=i[0]), i[1]) for i in quantities
|
|
|
|
]
|
|
|
|
|
|
|
|
try:
|
|
|
|
current_cart.set_quantities(product_quantities)
|
|
|
|
except ValidationError as ve:
|
|
|
|
products_form.add_error(None, ve)
|
2016-03-05 02:01:16 +00:00
|
|
|
|
2016-03-27 00:48:17 +00:00
|
|
|
|
2016-03-26 09:01:46 +00:00
|
|
|
def handle_voucher(request, prefix):
|
|
|
|
''' Handles a voucher form in the given request. Returns the voucher
|
|
|
|
form instance, and whether the voucher code was handled. '''
|
|
|
|
|
|
|
|
voucher_form = forms.VoucherForm(request.POST or None, prefix=prefix)
|
|
|
|
current_cart = CartController.for_user(request.user)
|
|
|
|
|
|
|
|
if (voucher_form.is_valid() and
|
|
|
|
voucher_form.cleaned_data["voucher"].strip()):
|
|
|
|
|
|
|
|
voucher = voucher_form.cleaned_data["voucher"]
|
|
|
|
voucher = rego.Voucher.normalise_code(voucher)
|
|
|
|
|
|
|
|
if len(current_cart.cart.vouchers.filter(code=voucher)) > 0:
|
|
|
|
# This voucher has already been applied to this cart.
|
|
|
|
# Do not apply code
|
|
|
|
handled = False
|
|
|
|
else:
|
|
|
|
try:
|
|
|
|
current_cart.apply_voucher(voucher)
|
|
|
|
except Exception as e:
|
|
|
|
voucher_form.add_error("voucher", e)
|
|
|
|
handled = True
|
|
|
|
else:
|
|
|
|
handled = False
|
|
|
|
|
|
|
|
return (voucher_form, handled)
|
2016-03-25 03:51:39 +00:00
|
|
|
|
2016-03-27 00:48:17 +00:00
|
|
|
|
2016-03-04 20:22:01 +00:00
|
|
|
@login_required
|
|
|
|
def checkout(request):
|
|
|
|
''' Runs checkout for the current cart of items, ideally generating an
|
|
|
|
invoice. '''
|
|
|
|
|
|
|
|
current_cart = CartController.for_user(request.user)
|
|
|
|
current_invoice = InvoiceController.for_cart(current_cart.cart)
|
|
|
|
|
|
|
|
return redirect("invoice", current_invoice.invoice.id)
|
|
|
|
|
|
|
|
|
|
|
|
@login_required
|
|
|
|
def invoice(request, invoice_id):
|
|
|
|
''' Displays an invoice for a given invoice id. '''
|
|
|
|
|
|
|
|
invoice_id = int(invoice_id)
|
|
|
|
inv = rego.Invoice.objects.get(pk=invoice_id)
|
2016-04-01 05:17:46 +00:00
|
|
|
|
|
|
|
if request.user != inv.cart.user and not request.user.is_staff:
|
|
|
|
raise Http404()
|
|
|
|
|
2016-03-04 20:22:01 +00:00
|
|
|
current_invoice = InvoiceController(inv)
|
|
|
|
|
|
|
|
data = {
|
|
|
|
"invoice": current_invoice.invoice,
|
|
|
|
}
|
|
|
|
|
2016-03-31 04:44:20 +00:00
|
|
|
return render(request, "registrasion/invoice.html", data)
|
2016-03-23 03:51:04 +00:00
|
|
|
|
2016-03-25 03:51:39 +00:00
|
|
|
|
2016-03-23 03:51:04 +00:00
|
|
|
@login_required
|
|
|
|
def pay_invoice(request, invoice_id):
|
|
|
|
''' Marks the invoice with the given invoice id as paid.
|
|
|
|
WORK IN PROGRESS FUNCTION. Must be replaced with real payment workflow.
|
|
|
|
|
|
|
|
'''
|
|
|
|
invoice_id = int(invoice_id)
|
|
|
|
inv = rego.Invoice.objects.get(pk=invoice_id)
|
|
|
|
current_invoice = InvoiceController(inv)
|
2016-04-01 05:17:46 +00:00
|
|
|
if not current_invoice.invoice.paid and not current_invoice.invoice.void:
|
2016-03-23 03:51:04 +00:00
|
|
|
current_invoice.pay("Demo invoice payment", inv.value)
|
|
|
|
|
|
|
|
return redirect("invoice", current_invoice.invoice.id)
|