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
|
|
|
|
|
|
|
from django.contrib.auth.decorators import login_required
|
|
|
|
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-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-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
|
|
|
dashboard = redirect("dashboard")
|
|
|
|
next_step = redirect("guided_registration")
|
|
|
|
|
|
|
|
attendee = rego.Attendee.get_instance(request.user)
|
2016-03-25 01:50:59 +00:00
|
|
|
if attendee.completed_registration:
|
|
|
|
return dashboard
|
|
|
|
|
|
|
|
# Step 1: Fill in a badge
|
2016-03-24 02:43:06 +00:00
|
|
|
profile = rego.BadgeAndProfile.get_instance(attendee)
|
|
|
|
|
|
|
|
if profile is None:
|
|
|
|
ret = edit_profile(request)
|
|
|
|
profile_new = rego.BadgeAndProfile.get_instance(attendee)
|
|
|
|
if profile_new is None:
|
|
|
|
# No new profile was created
|
2016-03-23 08:39:07 +00:00
|
|
|
return ret
|
2016-03-24 02:43:06 +00:00
|
|
|
else:
|
|
|
|
return next_step
|
2016-03-23 08:39:07 +00:00
|
|
|
|
2016-03-24 02:43:06 +00:00
|
|
|
# Step 2: Go through each of the categories in order
|
|
|
|
category = attendee.highest_complete_category
|
|
|
|
|
|
|
|
# Get the next category
|
2016-03-23 08:39:07 +00:00
|
|
|
cats = rego.Category.objects
|
2016-03-24 02:43:06 +00:00
|
|
|
cats = cats.filter(id__gt=category).order_by("order")
|
|
|
|
|
|
|
|
if len(cats) == 0:
|
|
|
|
# We've filled in every category
|
2016-03-25 01:50:59 +00:00
|
|
|
attendee.completed_registration = True
|
|
|
|
attendee.save()
|
2016-03-24 02:43:06 +00:00
|
|
|
return dashboard
|
2016-03-23 08:39:07 +00:00
|
|
|
|
2016-03-24 02:43:06 +00:00
|
|
|
ret = product_category(request, cats[0].id)
|
|
|
|
attendee_new = rego.Attendee.get_instance(request.user)
|
|
|
|
if attendee_new.highest_complete_category == category:
|
|
|
|
# We've not yet completed this category
|
|
|
|
return ret
|
2016-03-23 08:39:07 +00:00
|
|
|
else:
|
2016-03-24 02:43:06 +00:00
|
|
|
return next_step
|
|
|
|
|
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):
|
|
|
|
attendee = rego.Attendee.get_instance(request.user)
|
|
|
|
|
|
|
|
try:
|
|
|
|
profile = rego.BadgeAndProfile.objects.get(attendee=attendee)
|
|
|
|
except ObjectDoesNotExist:
|
|
|
|
profile = None
|
|
|
|
|
|
|
|
form = forms.ProfileForm(request.POST or None, instance=profile)
|
|
|
|
|
|
|
|
if request.POST and form.is_valid():
|
|
|
|
form.instance.attendee = attendee
|
|
|
|
form.save()
|
2016-03-24 00:33:11 +00:00
|
|
|
|
|
|
|
data = {
|
|
|
|
"form": form,
|
|
|
|
}
|
|
|
|
return render(request, "profile_form.html", data)
|
|
|
|
|
2016-03-25 03:51: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-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-23 03:50:52 +00:00
|
|
|
current_cart = CartController.for_user(request.user)
|
2016-03-03 21:40:44 +00:00
|
|
|
|
2016-03-24 02:43:06 +00:00
|
|
|
attendee = rego.Attendee.get_instance(request.user)
|
|
|
|
|
2016-03-05 02:01:16 +00:00
|
|
|
products = rego.Product.objects.filter(category=category)
|
|
|
|
products = products.order_by("order")
|
2016-03-26 02:30:46 +00:00
|
|
|
products = ProductController.available_products(
|
|
|
|
request.user,
|
|
|
|
products=products,
|
|
|
|
)
|
|
|
|
ProductsForm = forms.ProductsForm(products)
|
2016-03-03 21:40:44 +00:00
|
|
|
|
|
|
|
if request.method == "POST":
|
2016-03-26 02:30:46 +00:00
|
|
|
cat_form = ProductsForm(
|
2016-03-25 03:51:39 +00:00
|
|
|
request.POST,
|
|
|
|
request.FILES,
|
|
|
|
prefix=PRODUCTS_FORM_PREFIX)
|
|
|
|
voucher_form = forms.VoucherForm(
|
|
|
|
request.POST,
|
|
|
|
prefix=VOUCHERS_FORM_PREFIX)
|
2016-03-23 02:33:33 +00:00
|
|
|
|
2016-03-25 03:51:39 +00:00
|
|
|
if (voucher_form.is_valid() and
|
|
|
|
voucher_form.cleaned_data["voucher"].strip()):
|
2016-03-23 02:33:33 +00:00
|
|
|
# Apply voucher
|
|
|
|
# leave
|
|
|
|
voucher = voucher_form.cleaned_data["voucher"]
|
|
|
|
try:
|
|
|
|
current_cart.apply_voucher(voucher)
|
|
|
|
except Exception as e:
|
|
|
|
voucher_form.add_error("voucher", e)
|
2016-03-23 08:39:07 +00:00
|
|
|
# Re-visit current page.
|
2016-03-23 02:33:33 +00:00
|
|
|
elif cat_form.is_valid():
|
2016-03-05 02:01:16 +00:00
|
|
|
try:
|
2016-03-23 08:39:07 +00:00
|
|
|
handle_valid_cat_form(cat_form, current_cart)
|
2016-03-25 03:51:39 +00:00
|
|
|
except ValidationError:
|
2016-03-24 03:19:33 +00:00
|
|
|
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:
|
2016-03-24 02:43:06 +00:00
|
|
|
if category_id > attendee.highest_complete_category:
|
|
|
|
attendee.highest_complete_category = category_id
|
|
|
|
attendee.save()
|
|
|
|
return redirect("dashboard")
|
2016-03-05 02:01:16 +00:00
|
|
|
|
2016-03-03 21:40:44 +00:00
|
|
|
else:
|
|
|
|
# Create initial data for each of products in category
|
2016-03-23 03:50:52 +00:00
|
|
|
items = rego.ProductItem.objects.filter(
|
|
|
|
product__category=category,
|
|
|
|
cart=current_cart.cart,
|
|
|
|
)
|
2016-03-05 02:01:16 +00:00
|
|
|
quantities = []
|
2016-03-03 21:40:44 +00:00
|
|
|
for product in products:
|
2016-03-04 22:35:09 +00:00
|
|
|
# Only add items that are enabled.
|
2016-03-03 21:40:44 +00:00
|
|
|
try:
|
|
|
|
quantity = items.get(product=product).quantity
|
|
|
|
except ObjectDoesNotExist:
|
|
|
|
quantity = 0
|
2016-03-05 02:01:16 +00:00
|
|
|
quantities.append((product, quantity))
|
|
|
|
|
2016-03-26 02:30:46 +00:00
|
|
|
cat_form = ProductsForm(
|
|
|
|
prefix=PRODUCTS_FORM_PREFIX,
|
|
|
|
product_quantities=quantities,
|
|
|
|
)
|
2016-03-23 02:33:33 +00:00
|
|
|
|
|
|
|
voucher_form = forms.VoucherForm(prefix=VOUCHERS_FORM_PREFIX)
|
2016-03-03 21:40:44 +00:00
|
|
|
|
2016-03-26 03:03:25 +00:00
|
|
|
discounts = discount.available_discounts(request.user, [], products)
|
2016-03-03 21:40:44 +00:00
|
|
|
data = {
|
|
|
|
"category": category,
|
2016-03-26 03:03:25 +00:00
|
|
|
"discounts": discounts,
|
2016-03-05 02:01:16 +00:00
|
|
|
"form": cat_form,
|
2016-03-23 02:33:33 +00:00
|
|
|
"voucher_form": voucher_form,
|
2016-03-03 21:40:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return render(request, "product_category.html", data)
|
2016-03-04 20:22:01 +00:00
|
|
|
|
2016-03-25 03:51:39 +00:00
|
|
|
|
2016-03-23 08:39:07 +00:00
|
|
|
@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()
|
2016-03-05 02:01:16 +00:00
|
|
|
|
2016-03-25 03:51:39 +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)
|
|
|
|
current_invoice = InvoiceController(inv)
|
|
|
|
|
|
|
|
data = {
|
|
|
|
"invoice": current_invoice.invoice,
|
|
|
|
}
|
|
|
|
|
|
|
|
return render(request, "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-03-24 03:20:29 +00:00
|
|
|
if not inv.paid and current_invoice.is_valid():
|
2016-03-23 03:51:04 +00:00
|
|
|
current_invoice.pay("Demo invoice payment", inv.value)
|
|
|
|
|
|
|
|
return redirect("invoice", current_invoice.invoice.id)
|