symposion_app/registrasion/views.py

606 lines
18 KiB
Python
Raw Normal View History

import sys
from registrasion import forms
from registrasion.models import commerce
from registrasion.models import inventory
from registrasion.models import people
from registrasion.controllers import discount
from registrasion.controllers.cart import CartController
2016-04-11 02:11:14 +00:00
from registrasion.controllers.credit_note import CreditNoteController
from registrasion.controllers.invoice import InvoiceController
from registrasion.controllers.product import ProductController
from registrasion.exceptions import CartValidationError
2016-04-01 00:43:19 +00:00
from collections import namedtuple
from django.conf import settings
from django.contrib.auth.decorators import login_required
2016-04-01 00:43:19 +00:00
from django.contrib import messages
from django.core.exceptions import ObjectDoesNotExist
from django.core.exceptions import ValidationError
from django.http import Http404
2016-04-07 09:19:19 +00:00
from django.shortcuts import get_object_or_404
from django.shortcuts import redirect
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
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
@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, it just goes
through each category one by one
'''
SESSION_KEY = "guided_registration_categories"
ASK_FOR_PROFILE = 777 # Magic number. Meh.
next_step = redirect("guided_registration")
2016-04-01 00:43:19 +00:00
sections = []
attendee = people.Attendee.get_instance(request.user)
2016-04-01 00:43:19 +00:00
if attendee.completed_registration:
2016-04-01 00:43:19 +00:00
return render(
request,
"registrasion/guided_registration_complete.html",
{},
)
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:
profile = attendee.attendeeprofilebase
2016-04-01 05:58:55 +00:00
except ObjectDoesNotExist:
profile = None
# 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
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
starting = attendee.guided_categories_complete.count() == 0
2016-04-01 00:43:19 +00:00
# Get the next category
cats = inventory.Category.objects
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(),
)
2016-04-01 00:43:19 +00:00
cats = cats.order_by("order")
request.session[SESSION_KEY] = []
if starting:
2016-04-01 00:43:19 +00:00
# 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"
all_products = inventory.Product.objects.filter(
category__in=cats,
).select_related("category")
available_products = set(ProductController.available_products(
request.user,
products=all_products,
))
if len(available_products) == 0:
# We've filled in every category
attendee.completed_registration = True
attendee.save()
return next_step
2016-04-01 00:43:19 +00:00
for category in cats:
products = [
i for i in available_products
if i.category == category
]
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,
)
2016-04-02 09:11:40 +00:00
if products:
# This product category has items to show.
2016-04-02 09:11:40 +00:00
sections.append(section)
# Add this to the list of things to show if the form errors.
request.session[SESSION_KEY].append(category.id)
2016-04-01 00:43:19 +00:00
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)
2016-04-01 00:43:19 +00:00
if sections and request.method == "POST":
for section in sections:
if section.form.errors:
break
else:
attendee.save()
if SESSION_KEY in request.session:
del request.session[SESSION_KEY]
2016-04-01 00:43:19 +00:00
# We've successfully processed everything
return next_step
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)
@login_required
def edit_profile(request):
2016-04-01 00:41:59 +00:00
form, handled = handle_profile(request, "profile")
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. '''
attendee = people.Attendee.get_instance(request.user)
try:
profile = attendee.attendeeprofilebase
profile = people.AttendeeProfileBase.objects.get_subclass(
pk=profile.id,
)
except ObjectDoesNotExist:
profile = None
ProfileForm = get_form(settings.ATTENDEE_PROFILE_FORM)
# 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:
initial[name_field] = speaker_name
form = ProfileForm(
2016-04-01 00:41:59 +00:00
request.POST or None,
initial=initial,
2016-04-01 00:41:59 +00:00
instance=profile,
prefix=prefix
)
handled = True if request.POST else False
if request.POST and form.is_valid():
form.instance.attendee = attendee
form.save()
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
@login_required
def product_category(request, category_id):
''' Registration selections form for a specific category of items.
'''
PRODUCTS_FORM_PREFIX = "products"
VOUCHERS_FORM_PREFIX = "vouchers"
# 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
category_id = int(category_id) # Routing is [0-9]+
category = inventory.Category.objects.get(pk=category_id)
products = ProductController.available_products(
request.user,
category=category,
)
if not products:
messages.warning(
request,
2016-04-02 02:29:53 +00:00
"There are no products available from category: " + category.name,
)
return redirect("dashboard")
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
messages.success(
request,
"Your reservations have been updated.",
)
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)
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)
# Create initial data for each of products in category
items = commerce.ProductItem.objects.filter(
product__in=products,
cart=current_cart.cart,
).select_related("product")
quantities = []
seen = set()
for item in items:
quantities.append((item.product, item.quantity))
seen.add(item.product)
zeros = set(products) - seen
for product in zeros:
quantities.append((product, 0))
products_form = ProductsForm(
request.POST or None,
product_quantities=quantities,
prefix=prefix,
)
if request.method == "POST" and products_form.is_valid():
if products_form.has_changed():
set_quantities_from_products_form(products_form, current_cart)
# If category is required, the user must have at least one
# in an active+valid cart
if category.required:
carts = commerce.Cart.objects.filter(user=request.user)
items = commerce.ProductItem.objects.filter(
product__category=category,
cart=carts,
)
if len(items) == 0:
products_form.add_error(
None,
"You must have at least one item from this category",
2016-03-24 03:19:33 +00:00
)
handled = False if products_form.errors else True
discounts = discount.available_discounts(request.user, [], products)
return products_form, discounts, handled
2016-03-25 03:51:39 +00:00
2016-03-27 00:48:17 +00:00
def set_quantities_from_products_form(products_form, current_cart):
quantities = list(products_form.product_quantities())
pks = [i[0] for i in quantities]
products = inventory.Product.objects.filter(
2016-04-07 03:26:25 +00:00
id__in=pks,
).select_related("category")
product_quantities = [
(products.get(pk=i[0]), i[1]) for i in quantities
]
field_names = dict(
(i[0][0], i[1][2]) for i in zip(product_quantities, quantities)
)
try:
current_cart.set_quantities(product_quantities)
except CartValidationError as ve:
for ve_field in ve.error_list:
product, message = ve_field.message
if product in field_names:
field = field_names[product]
elif isinstance(product, inventory.Product):
continue
else:
field = None
products_form.add_error(field, message)
2016-03-27 00:48:17 +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 = inventory.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
@login_required
def checkout(request):
''' Runs checkout for the current cart of items, ideally generating an
invoice. '''
current_cart = CartController.for_user(request.user)
if "fix_errors" in request.GET and request.GET["fix_errors"] == "true":
current_cart.fix_simple_errors()
try:
current_invoice = InvoiceController.for_cart(current_cart.cart)
except ValidationError as ve:
return checkout_errors(request, ve)
return redirect("invoice", current_invoice.invoice.id)
2016-04-07 03:26:25 +00:00
def checkout_errors(request, errors):
error_list = []
for error in errors.error_list:
if isinstance(error, tuple):
error = error[1]
error_list.append(error)
data = {
"error_list": error_list,
}
return render(request, "registrasion/checkout_errors.html", data)
2016-04-07 03:26:25 +00:00
2016-04-08 03:15:24 +00:00
def invoice_access(request, access_code):
''' Redirects to the first unpaid invoice for the attendee that matches
the given access code, if any. '''
invoices = commerce.Invoice.objects.filter(
2016-04-08 03:15:24 +00:00
user__attendee__access_code=access_code,
status=commerce.Invoice.STATUS_UNPAID,
2016-04-08 03:15:24 +00:00
).order_by("issue_time")
2016-04-08 03:15:24 +00:00
if not invoices:
raise Http404()
2016-04-08 03:15:24 +00:00
invoice = invoices[0]
return redirect("invoice", invoice.id, access_code)
def invoice(request, invoice_id, access_code=None):
''' Displays an invoice for a given invoice id.
This view is not authenticated, but it will only allow access to either:
the user the invoice belongs to; staff; or a request made with the correct
access code.
'''
invoice_id = int(invoice_id)
inv = commerce.Invoice.objects.get(pk=invoice_id)
2016-04-08 03:15:24 +00:00
current_invoice = InvoiceController(inv)
2016-04-08 03:15:24 +00:00
if not current_invoice.can_view(
user=request.user,
access_code=access_code,
2016-04-08 09:49:18 +00:00
):
2016-04-08 03:15:24 +00:00
raise Http404()
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
2016-04-07 09:19:19 +00:00
def manual_payment(request, invoice_id):
''' Allows staff to make manual payments or refunds on an invoice.'''
FORM_PREFIX = "manual_payment"
if not request.user.is_staff:
raise Http404()
2016-03-23 03:51:04 +00:00
invoice_id = int(invoice_id)
inv = get_object_or_404(commerce.Invoice, pk=invoice_id)
2016-03-23 03:51:04 +00:00
current_invoice = InvoiceController(inv)
2016-04-07 09:19:19 +00:00
form = forms.ManualPaymentForm(
request.POST or None,
prefix=FORM_PREFIX,
)
if request.POST and form.is_valid():
form.instance.invoice = inv
form.save()
current_invoice.update_status()
form = forms.ManualPaymentForm(prefix=FORM_PREFIX)
data = {
"invoice": inv,
"form": form,
}
return render(request, "registrasion/manual_payment.html", data)
@login_required
def refund(request, invoice_id):
''' Allows staff to refund payments against an invoice and request a
credit note.'''
if not request.user.is_staff:
raise Http404()
invoice_id = int(invoice_id)
inv = get_object_or_404(commerce.Invoice, pk=invoice_id)
current_invoice = InvoiceController(inv)
try:
current_invoice.refund()
messages.success(request, "This invoice has been refunded.")
except ValidationError as ve:
messages.error(request, ve)
return redirect("invoice", invoice_id)
2016-04-11 02:11:14 +00:00
def credit_note(request, note_id, access_code=None):
''' Displays an credit note for a given id.
This view can only be seen by staff.
'''
if not request.user.is_staff:
raise Http404()
note_id = int(note_id)
note = commerce.CreditNote.objects.get(pk=note_id)
2016-04-11 02:11:14 +00:00
current_note = CreditNoteController(note)
apply_form = forms.ApplyCreditNoteForm(
note.invoice.user,
request.POST or None,
prefix="apply_note"
)
refund_form = forms.ManualCreditNoteRefundForm(
request.POST or None,
prefix="refund_note"
)
if request.POST and apply_form.is_valid():
inv_id = apply_form.cleaned_data["invoice"]
invoice = commerce.Invoice.objects.get(pk=inv_id)
current_note.apply_to_invoice(invoice)
messages.success(
request,
"Applied credit note %d to invoice." % note_id,
)
return redirect("invoice", invoice.id)
elif request.POST and refund_form.is_valid():
refund_form.instance.entered_by = request.user
refund_form.instance.parent = note
refund_form.save()
messages.success(
request,
"Applied manual refund to credit note."
)
return redirect("invoice", invoice.id)
2016-04-11 02:11:14 +00:00
data = {
"credit_note": current_note.credit_note,
"apply_form": apply_form,
"refund_form": refund_form,
2016-04-11 02:11:14 +00:00
}
return render(request, "registrasion/credit_note.html", data)