Changeset - 8d66ed57150d
[Not reviewed]
0 6 0
Christopher Neugebauer - 8 years ago 2016-03-25 03:51:39
chrisjrn@gmail.com
Fix flake8 warnings
6 files changed with 22 insertions and 14 deletions:
0 comments (0 inline, 0 general)
registrasion/forms.py
Show inline comments
...
 
@@ -34,25 +34,24 @@ def CategoryForm(category):
 

	
 
        def disable_product(self, product):
 
            ''' Removes a given product from this form. '''
 
            del self.fields[field_name(product)]
 

	
 
        def disable_products_for_user(self, user):
 
            for product in products:
 
                # Remove fields that do not have an enabling condition.
 
                prod = ProductController(product)
 
                if not prod.can_add_with_enabling_conditions(user, 0):
 
                    self.disable_product(product)
 

	
 

	
 
    products = rego.Product.objects.filter(category=category).order_by("order")
 
    for product in products:
 

	
 
        help_text = "$%d -- %s" % (product.price, product.description)
 

	
 
        field = forms.IntegerField(
 
            label=product.name,
 
            help_text=help_text,
 
        )
 
        _CategoryForm.base_fields[field_name(product)] = field
 

	
 
    return _CategoryForm
registrasion/templatetags/registrasion_tags.py
Show inline comments
 
from registrasion import models as rego
 

	
 
from django import template
 

	
 
register = template.Library()
 

	
 

	
 
@register.assignment_tag(takes_context=True)
 
def available_categories(context):
 
    ''' Returns all of the available product categories '''
 
    return rego.Category.objects.all()
registrasion/tests/test_discount.py
Show inline comments
...
 
@@ -199,18 +199,18 @@ class DiscountTestCase(RegistrationCartTestCase):
 
        discount_items = list(cart.cart.discountitem_set.all())
 
        # There is one discount, and it should apply to the more expensive.
 
        self.assertEqual(1, len(discount_items))
 
        self.assertEqual(self.PROD_3, discount_items[0].product)
 

	
 
    def test_discount_quantity_is_per_user(self):
 
        self.add_discount_prod_1_includes_cat_2(quantity=1)
 

	
 
        # Both users should be able to apply the same discount
 
        # in the same way
 
        for user in (self.USER_1, self.USER_2):
 
            cart = CartController.for_user(user)
 
            cart.add_to_cart(self.PROD_1, 1) # Enable the discount
 
            cart.add_to_cart(self.PROD_1, 1)  # Enable the discount
 
            cart.add_to_cart(self.PROD_3, 1)
 

	
 
            discount_items = list(cart.cart.discountitem_set.all())
 
            # The discount is applied.
 
            self.assertEqual(1, len(discount_items))
registrasion/tests/test_voucher.py
Show inline comments
...
 
@@ -86,24 +86,24 @@ class VoucherTestCases(RegistrationCartTestCase):
 
            product=self.PROD_1,
 
            percentage=Decimal(100),
 
            quantity=1
 
        ).save()
 

	
 
        # Having PROD_1 in place should add a discount
 
        current_cart = CartController.for_user(self.USER_1)
 
        current_cart.apply_voucher(voucher.code)
 
        current_cart.add_to_cart(self.PROD_1, 1)
 
        self.assertEqual(1, len(current_cart.cart.discountitem_set.all()))
 

	
 
    def test_voucher_codes_unique(self):
 
        voucher1 = self.new_voucher(code="VOUCHER")
 
        self.new_voucher(code="VOUCHER")
 
        with self.assertRaises(IntegrityError):
 
            voucher2 = self.new_voucher(code="VOUCHER")
 
            self.new_voucher(code="VOUCHER")
 

	
 
    def test_multiple_vouchers_work(self):
 
        voucher1 = self.new_voucher(code="VOUCHER1")
 
        voucher2 = self.new_voucher(code="VOUCHER2")
 
        self.new_voucher(code="VOUCHER1")
 
        self.new_voucher(code="VOUCHER2")
 

	
 
    def test_vouchers_case_insensitive(self):
 
        voucher = self.new_voucher(code="VOUCHeR")
 
        current_cart = CartController.for_user(self.USER_1)
 
        current_cart.apply_voucher(voucher.code.lower())
registrasion/urls.py
Show inline comments
 
from django.conf.urls import url, patterns
 

	
 
urlpatterns = patterns(
 
    "registrasion.views",
 
    url(r"^category/([0-9]+)$", "product_category", name="product_category"),
 
    url(r"^checkout$", "checkout", name="checkout"),
 
    url(r"^invoice/([0-9]+)$", "invoice", name="invoice"),
 
    url(r"^invoice/([0-9]+)/pay$", "pay_invoice", name="pay_invoice"),
 
    url(r"^profile$", "edit_profile", name="profile"),
 
    url(r"^register$", "guided_registration", name="guided_registration"),
 
    url(r"^register/([0-9]+)$", "guided_registration", name="guided_registration"),
 
    url(r"^register/([0-9]+)$", "guided_registration",
 
        name="guided_registration"),
 
)
registrasion/views.py
Show inline comments
 
from registrasion import forms
 
from registrasion import models as rego
 
from registrasion.controllers.cart import CartController
 
from registrasion.controllers.invoice import InvoiceController
 
from registrasion.controllers.product import ProductController
 

	
 
from django.contrib.auth.decorators import login_required
 
from django.core.exceptions import ObjectDoesNotExist
 
from django.core.exceptions import ValidationError
 
from django.db import transaction
 
from django.shortcuts import redirect
 
from django.shortcuts import render
 

	
 

	
 
@login_required
 
def guided_registration(request, page_id=0):
 
    ''' Goes through the registration process in order,
...
 
@@ -74,61 +73,68 @@ def edit_profile(request):
 

	
 
    form = forms.ProfileForm(request.POST or None, instance=profile)
 

	
 
    if request.POST and form.is_valid():
 
        form.instance.attendee = attendee
 
        form.save()
 

	
 
    data = {
 
        "form": form,
 
    }
 
    return render(request, "profile_form.html", data)
 

	
 

	
 
@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"
 

	
 
    category_id = int(category_id)  # Routing is [0-9]+
 
    category = rego.Category.objects.get(pk=category_id)
 
    current_cart = CartController.for_user(request.user)
 

	
 
    CategoryForm = forms.CategoryForm(category)
 

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

	
 
    products = rego.Product.objects.filter(category=category)
 
    products = products.order_by("order")
 

	
 
    if request.method == "POST":
 
        cat_form = CategoryForm(request.POST, request.FILES, prefix=PRODUCTS_FORM_PREFIX)
 
        cat_form = CategoryForm(
 
            request.POST,
 
            request.FILES,
 
            prefix=PRODUCTS_FORM_PREFIX)
 
        cat_form.disable_products_for_user(request.user)
 
        voucher_form = forms.VoucherForm(request.POST, prefix=VOUCHERS_FORM_PREFIX)
 
        voucher_form = forms.VoucherForm(
 
            request.POST,
 
            prefix=VOUCHERS_FORM_PREFIX)
 

	
 
        if voucher_form.is_valid() and voucher_form.cleaned_data["voucher"].strip():
 
        if (voucher_form.is_valid() and
 
                voucher_form.cleaned_data["voucher"].strip()):
 
            # 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)
 
            # Re-visit current page.
 
        elif cat_form.is_valid():
 
            try:
 
                handle_valid_cat_form(cat_form, current_cart)
 
            except ValidationError as ve:
 
            except ValidationError:
 
                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,
 
                )
...
 
@@ -144,58 +150,58 @@ def product_category(request, category_id):
 
                    attendee.save()
 
                return redirect("dashboard")
 

	
 
    else:
 
        # Create initial data for each of products in category
 
        items = rego.ProductItem.objects.filter(
 
            product__category=category,
 
            cart=current_cart.cart,
 
        )
 
        quantities = []
 
        for product in products:
 
            # Only add items that are enabled.
 
            prod = ProductController(product)
 
            try:
 
                quantity = items.get(product=product).quantity
 
            except ObjectDoesNotExist:
 
                quantity = 0
 
            quantities.append((product, quantity))
 

	
 
        initial = CategoryForm.initial_data(quantities)
 
        cat_form = CategoryForm(prefix=PRODUCTS_FORM_PREFIX, initial=initial)
 
        cat_form.disable_products_for_user(request.user)
 

	
 
        voucher_form = forms.VoucherForm(prefix=VOUCHERS_FORM_PREFIX)
 

	
 

	
 
    data = {
 
        "category": category,
 
        "form": cat_form,
 
        "voucher_form": voucher_form,
 
    }
 

	
 
    return render(request, "product_category.html", data)
 

	
 

	
 
@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()
 

	
 

	
 
@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
...
 
@@ -203,24 +209,25 @@ 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)
 

	
 

	
 
@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)
 
    if not inv.paid and current_invoice.is_valid():
 
        current_invoice.pay("Demo invoice payment", inv.value)
0 comments (0 inline, 0 general)