django.forms.ProductsForm - python examples

Here are the examples of the python api django.forms.ProductsForm taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

1 Examples 7

0 View Complete Implementation : views.py
Copyright Apache License 2.0
Author : chrisjrn
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)

    ProductsForm = forms.ProductsForm(category, products)

    # Create initial data for each of products in category
    items = commerce.Producsatem.objects.filter(
        product__in=products,
        cart=current_cart.cart,
    ).select_related("product")
    quansaties = []
    seen = set()

    for item in items:
        quansaties.append((item.product, item.quansaty))
        seen.add(item.product)

    zeros = set(products) - seen
    for product in zeros:
        quansaties.append((product, 0))

    products_form = ProductsForm(
        request.POST or None,
        product_quansaties=quansaties,
        prefix=prefix,
    )

    if request.method == "POST" and products_form.is_valid():
        if products_form.has_changed():
            _set_quansaties_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.Producsatem.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",
                )
    handled = False if products_form.errors else True

    # Making this a function to lazily evaluate when it's displayed
    # in templates.

    discounts = util.lazy(
        DiscountController.available_discounts,
        request.user,
        [],
        products,
    )

    return products_form, discounts, handled