Changeset - 897915f1217d
[Not reviewed]
0 3 0
Christopher Neugebauer - 8 years ago 2016-09-03 04:22:32
chrisjrn@gmail.com
Adds the amend_registration view, which currently can display all of the products that the user has added to their current cart, and not much else.
3 files changed with 45 insertions and 0 deletions:
0 comments (0 inline, 0 general)
registrasion/forms.py
Show inline comments
...
 
@@ -254,96 +254,111 @@ class _ItemQuantityProductsForm(_ProductsForm):
 
    def initial_data(cls, product_quantities):
 
        initial = {}
 

	
 
        for product, quantity in product_quantities:
 
            if quantity > 0:
 
                initial[cls.CHOICE_FIELD] = product.id
 
                initial[cls.QUANTITY_FIELD] = quantity
 
                break
 

	
 
        return initial
 

	
 
    def product_quantities(self):
 
        our_choice = self.cleaned_data[self.CHOICE_FIELD]
 
        our_quantity = self.cleaned_data[self.QUANTITY_FIELD]
 
        choices = self.fields[self.CHOICE_FIELD].choices
 
        for choice_value, choice_display in choices:
 
            if choice_value == 0:
 
                continue
 
            yield (
 
                choice_value,
 
                our_quantity if our_choice == choice_value else 0,
 
            )
 

	
 
    def add_product_error(self, product, error):
 
        if self.CHOICE_FIELD not in self.cleaned_data:
 
            return
 

	
 
        if product.id == self.cleaned_data[self.CHOICE_FIELD]:
 
            self.add_error(self.CHOICE_FIELD, error)
 
            self.add_error(self.QUANTITY_FIELD, error)
 

	
 

	
 
class _ItemQuantityProductsFormSet(_HasProductsFields, forms.BaseFormSet):
 

	
 
    @classmethod
 
    def set_fields(cls, category, products):
 
        raise ValueError("set_fields must be called on the underlying Form")
 

	
 
    @classmethod
 
    def initial_data(cls, product_quantities):
 
        ''' Prepares initial data for an instance of this form.
 
        product_quantities is a sequence of (product,quantity) tuples '''
 

	
 
        f = [
 
            {
 
                _ItemQuantityProductsForm.CHOICE_FIELD: product.id,
 
                _ItemQuantityProductsForm.QUANTITY_FIELD: quantity,
 
            }
 
            for product, quantity in product_quantities
 
            if quantity > 0
 
        ]
 
        return f
 

	
 
    def product_quantities(self):
 
        ''' Yields a sequence of (product, quantity) tuples from the
 
        cleaned form data. '''
 

	
 
        products = set()
 
        # Track everything so that we can yield some zeroes
 
        all_products = set()
 

	
 
        for form in self:
 
            if form.empty_permitted and not form.cleaned_data:
 
                # This is the magical empty form at the end of the list.
 
                continue
 

	
 
            for product, quantity in form.product_quantities():
 
                all_products.add(product)
 
                if quantity == 0:
 
                    continue
 
                if product in products:
 
                    form.add_error(
 
                        _ItemQuantityProductsForm.CHOICE_FIELD,
 
                        "You may only choose each product type once.",
 
                    )
 
                    form.add_error(
 
                        _ItemQuantityProductsForm.QUANTITY_FIELD,
 
                        "You may only choose each product type once.",
 
                    )
 
                products.add(product)
 
                yield product, quantity
 

	
 
        for product in (all_products - products):
 
            yield product, 0
 

	
 
    def add_product_error(self, product, error):
 
        for form in self.forms:
 
            form.add_product_error(product, error)
 

	
 

	
 
class VoucherForm(forms.Form):
 
    voucher = forms.CharField(
 
        label="Voucher code",
 
        help_text="If you have a voucher code, enter it here",
 
        required=False,
 
    )
 

	
 

	
 
class StaffProductsForm(forms.Form):
 
    ''' Form for allowing staff to add an item to a user's cart. '''
 

	
 
    product = forms.ModelChoiceField(
 
        widget=forms.Select,
 
        queryset=inventory.Product.objects.all(),
 
    )
 

	
 
    quantity = forms.IntegerField(
 
        min_value=0,
 
    )
 

	
 
StaffProductsFormSet = forms.formset_factory(StaffProductsForm)
registrasion/urls.py
Show inline comments
 
from reporting import views as rv
 

	
 
from django.conf.urls import include
 
from django.conf.urls import url
 

	
 
from .views import (
 
    product_category,
 
    checkout,
 
    credit_note,
 
    invoice,
 
    manual_payment,
 
    refund,
 
    invoice_access,
 
    edit_profile,
 
    guided_registration,
 
    amend_registration,
 
)
 

	
 

	
 
public = [
 
    url(r"^amend/([0-9]+)$", amend_registration, name="amend_registration"),
 
    url(r"^category/([0-9]+)$", product_category, name="product_category"),
 
    url(r"^checkout$", checkout, name="checkout"),
 
    url(r"^credit_note/([0-9]+)$", credit_note, name="credit_note"),
 
    url(r"^invoice/([0-9]+)$", invoice, name="invoice"),
 
    url(r"^invoice/([0-9]+)/([A-Z0-9]+)$", invoice, name="invoice"),
 
    url(r"^invoice/([0-9]+)/manual_payment$",
 
        manual_payment, name="manual_payment"),
 
    url(r"^invoice/([0-9]+)/refund$",
 
        refund, name="refund"),
 
    url(r"^invoice_access/([A-Z0-9]+)$", invoice_access,
 
        name="invoice_access"),
 
    url(r"^profile$", edit_profile, name="attendee_edit"),
 
    url(r"^register$", guided_registration, name="guided_registration"),
 
    url(r"^register/([0-9]+)$", guided_registration,
 
        name="guided_registration"),
 
]
 

	
 

	
 
reports = [
 
    url(r"^$", rv.reports_list, name="reports_list"),
 
    url(r"^attendee/?$", rv.attendee, name="attendee"),
 
    url(r"^attendee/([0-9]*)$", rv.attendee, name="attendee"),
 
    url(r"^credit_notes/?$", rv.credit_notes, name="credit_notes"),
 
    url(r"^items_sold/?$", rv.items_sold, name="items_sold"),
 
    url(r"^product_status/?$", rv.product_status, name="product_status"),
 
    url(r"^reconciliation/?$", rv.reconciliation, name="reconciliation"),
 
]
 

	
 

	
 
urlpatterns = [
 
    url(r"^reports/", include(reports)),
 
    url(r"^", include(public))  # This one must go last.
 
]
registrasion/views.py
Show inline comments
 
import sys
 

	
 
from registrasion import forms
 
from registrasion import util
 
from registrasion.models import commerce
 
from registrasion.models import inventory
 
from registrasion.models import people
 
from registrasion.controllers.batch import BatchController
 
from registrasion.controllers.cart import CartController
 
from registrasion.controllers.credit_note import CreditNoteController
 
from registrasion.controllers.discount import DiscountController
 
from registrasion.controllers.invoice import InvoiceController
 
from registrasion.controllers.product import ProductController
 
from registrasion.exceptions import CartValidationError
 

	
 
from collections import namedtuple
 

	
 
from django.conf import settings
 
from django.contrib.auth.decorators import login_required
 
from django.contrib.auth.decorators import user_passes_test
 
from django.contrib.auth.models import User
 
from django.contrib import messages
 
from django.core.exceptions import ObjectDoesNotExist
 
from django.core.exceptions import ValidationError
 
from django.http import Http404
 
from django.shortcuts import redirect
 
from django.shortcuts import render
 

	
 

	
 
_GuidedRegistrationSection = namedtuple(
 
    "GuidedRegistrationSection",
 
    (
 
        "title",
 
        "discounts",
 
        "description",
 
        "form",
 
    )
 
)
 

	
 

	
 
@util.all_arguments_optional
 
class GuidedRegistrationSection(_GuidedRegistrationSection):
 
    ''' Represents a section of a guided registration page.
 

	
 
    Attributes:
 
       title (str): The title of the section.
 

	
 
       discounts ([registrasion.contollers.discount.DiscountAndQuantity, ...]):
 
            A list of discount objects that are available in the section. You
 
            can display ``.clause`` to show what the discount applies to, and
 
            ``.quantity`` to display the number of times that discount can be
 
            applied.
 

	
 
       description (str): A description of the section.
 

	
 
       form (forms.Form): A form to display.
 
    '''
 
    pass
 

	
 

	
 
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)
 

	
 

	
 
@login_required
 
def guided_registration(request):
 
    ''' Goes through the registration process in order, making sure user sees
 
    all valid categories.
 

	
 
    The user must be logged in to see this view.
 

	
 
    Returns:
 
        render: Renders ``registrasion/guided_registration.html``,
 
            with the following data::
 

	
 
                {
 
                    "current_step": int(),  # The current step in the
 
                                            # registration
 
                    "sections": sections,   # A list of
 
                                            # GuidedRegistrationSections
 
                    "title": str(),         # The title of the page
 
                    "total_steps": int(),   # The total number of steps
 
                }
 

	
 
    '''
 

	
 
    SESSION_KEY = "guided_registration_categories"
 
    ASK_FOR_PROFILE = 777  # Magic number. Meh.
 

	
 
    next_step = redirect("guided_registration")
 

	
 
    sections = []
 

	
 
    attendee = people.Attendee.get_instance(request.user)
 

	
 
    if attendee.completed_registration:
 
        return render(
 
            request,
 
            "registrasion/guided_registration_complete.html",
 
            {},
 
        )
 

	
 
    # Step 1: Fill in a badge and collect a voucher code
 
    try:
 
        profile = attendee.attendeeprofilebase
 
    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:
...
 
@@ -697,96 +698,123 @@ def refund(request, invoice_id):
 
    ''' Marks an invoice as refunded and requests a credit note for the
 
    full amount paid against the invoice.
 

	
 
    This view requires a login, and the logged in user must be staff.
 

	
 
    Arguments:
 
        invoice_id (castable to int): The ID of the invoice to refund.
 

	
 
    Returns:
 
        redirect:
 
            Redirects to ``invoice``.
 

	
 
    '''
 

	
 
    current_invoice = InvoiceController.for_id_or_404(invoice_id)
 

	
 
    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)
 

	
 

	
 
@user_passes_test(_staff_only)
 
def credit_note(request, note_id, access_code=None):
 
    ''' Displays a credit note.
 

	
 
    If ``request`` is a ``POST`` request, forms for applying or refunding
 
    a credit note will be processed.
 

	
 
    This view requires a login, and the logged in user must be staff.
 

	
 
    Arguments:
 
        note_id (castable to int): The ID of the credit note to view.
 

	
 
    Returns:
 
        render or redirect:
 
            If the "apply to invoice" form is correctly processed, redirect to
 
            that invoice, otherwise, render ``registration/credit_note.html``
 
            with the following data::
 

	
 
                {
 
                    "credit_note": models.commerce.CreditNote(),
 
                    "apply_form": form,  # A form for applying credit note
 
                                         # to an invoice.
 
                    "refund_form": form, # A form for applying a *manual*
 
                                         # refund of the credit note.
 
                }
 

	
 
    '''
 

	
 
    note_id = int(note_id)
 
    current_note = CreditNoteController.for_id_or_404(note_id)
 

	
 
    apply_form = forms.ApplyCreditNoteForm(
 
        current_note.credit_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 = current_note.credit_note
 
        refund_form.save()
 
        messages.success(
 
            request,
 
            "Applied manual refund to credit note."
 
        )
 
        refund_form = forms.ManualCreditNoteRefundForm(
 
            prefix="refund_note",
 
        )
 

	
 
    data = {
 
        "credit_note": current_note.credit_note,
 
        "apply_form": apply_form,
 
        "refund_form": refund_form,
 
    }
 

	
 
    return render(request, "registrasion/credit_note.html", data)
 

	
 

	
 
@user_passes_test(_staff_only)
 
def amend_registration(request, user_id):
 
    ''' Allows staff to amend a user's current registration cart, and etc etc.
 
    '''
 

	
 
    user = User.objects.get(id=int(user_id))
 
    current_cart = CartController.for_user(user)
 

	
 
    items = commerce.ProductItem.objects.filter(
 
        cart=current_cart.cart,
 
    ).select_related("product")
 

	
 
    initial = [{"product": i.product, "quantity": i.quantity} for i in items]
 

	
 
    form = forms.StaffProductsFormSet(
 
        request.POST or None,
 
        initial=initial,
 
        prefix="products",
 
    )
 

	
 
    data = {
 
        "form": form,
 
    }
 

	
 
    return render(request, "registrasion/amend_registration.html", data)
0 comments (0 inline, 0 general)