本文整理匯總了Python中cartridge.shop.forms.AddProductForm類的典型用法代碼示例。如果您正苦於以下問題:Python AddProductForm類的具體用法?Python AddProductForm怎麽用?Python AddProductForm使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了AddProductForm類的7個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: product
def product(request, slug, template="shop/product.html"):
"""
Display a product - convert the product variations to JSON as well as
handling adding the product to either the cart or the wishlist.
"""
published_products = Product.objects.published(for_user=request.user)\
.select_subclasses()
product = get_object_or_404(published_products, slug=slug)
fields = [f.name for f in ProductVariation.option_fields()]
variations = product.variations.all()
variations_json = simplejson.dumps([dict([(f, getattr(v, f))
for f in fields + ["sku", "image_id"]])
for v in variations])
to_cart = (request.method == "POST" and
request.POST.get("add_wishlist") is None)
initial_data = {}
if variations:
initial_data = dict([(f, getattr(variations[0], f)) for f in fields])
initial_data["quantity"] = 1
add_product_form = AddProductForm(request.POST or None, product=product,
initial=initial_data, to_cart=to_cart)
if request.method == "POST":
if add_product_form.is_valid():
if to_cart:
quantity = add_product_form.cleaned_data["quantity"]
request.cart.add_item(add_product_form.variation, quantity)
recalculate_discount(request)
info(request, _("Item added to cart"))
return redirect("shop_cart")
else:
skus = request.wishlist
sku = add_product_form.variation.sku
if sku not in skus:
skus.append(sku)
info(request, _("Item added to wishlist"))
response = redirect("shop_wishlist")
set_cookie(response, "wishlist", ",".join(skus))
return response
context = {
"product": product,
"editable_obj": product,
"images": product.images.all(),
"variations": variations,
"variations_json": variations_json,
"has_available_variations": any([v.has_price() for v in variations]),
"related_products": product.related_products.published(
for_user=request.user),
"add_product_form": add_product_form
}
templates = []
# Check for a template matching the page's content model.
if product.content_model is not None:
templates.append(u"shop/products/%s.html" % product.content_model)
templates.append(template)
return render(request, templates, context)
示例2: wishlist
def wishlist(request, template="shop/wishlist.html"):
"""
Display the wishlist and handle removing items from the wishlist and
adding them to the cart.
"""
if not settings.SHOP_USE_WISHLIST:
raise Http404
skus = request.wishlist
error = None
if request.method == "POST":
to_cart = request.POST.get("add_cart")
add_product_form = AddProductForm(request.POST or None,
to_cart=to_cart)
if to_cart:
if add_product_form.is_valid():
request.cart.add_item(add_product_form.variation, 1)
recalculate_discount(request)
recalculate_billship_tax(request)
message = _("Item added to cart")
url = "shop_cart"
else:
error = add_product_form.errors.values()[0]
else:
message = _("Item removed from wishlist")
url = "shop_wishlist"
sku = request.POST.get("sku")
if sku in skus:
skus.remove(sku)
if not error:
info(request, message)
response = redirect(url)
set_cookie(response, "wishlist", ",".join(skus))
return response
# Remove skus from the cookie that no longer exist.
published_products = Product.objects.published(for_user=request.user)
f = {"product__in": published_products, "sku__in": skus}
wishlist = ProductVariation.objects.filter(**f).select_related(depth=1)
wishlist = sorted(wishlist, key=lambda v: skus.index(v.sku))
context = {"wishlist_items": wishlist, "error": error}
response = render(request, template, context)
if len(wishlist) < len(skus):
skus = [variation.sku for variation in wishlist]
set_cookie(response, "wishlist", ",".join(skus))
return response
示例3: product
def product(request, slug, template="shop/product.html"):
"""
Display a product - convert the product variations to JSON as well as
handling adding the product to either the cart or the wishlist.
"""
published_products = Product.objects.published(for_user=request.user)
product = get_object_or_404(published_products, slug=slug)
to_cart = (request.method == "POST" and
request.POST.get("add_wishlist") is None)
add_product_form = AddProductForm(request.POST or None, product=product,
initial={"quantity": 1}, to_cart=to_cart)
if request.method == "POST":
if add_product_form.is_valid():
if to_cart:
quantity = add_product_form.cleaned_data["quantity"]
request.cart.add_item(add_product_form.variation, quantity)
recalculate_discount(request)
info(request, _("Item added to cart"))
return HttpResponseRedirect(reverse("shop_cart"))
else:
skus = request.wishlist
sku = add_product_form.variation.sku
if sku not in skus:
skus.append(sku)
info(request, _("Item added to wishlist"))
response = HttpResponseRedirect(reverse("shop_wishlist"))
set_cookie(response, "wishlist", ",".join(skus))
return response
fields = [f.name for f in ProductVariation.option_fields()]
fields += ["sku", "image_id"]
variations = product.variations.all()
variations_json = simplejson.dumps([dict([(f, getattr(v, f))
for f in fields])
for v in variations])
context = {
"product": product,
"images": product.images.all(),
"variations": variations,
"variations_json": variations_json,
"has_available_variations": any([v.has_price() for v in variations]),
"related": product.related_products.published(for_user=request.user),
"add_product_form": add_product_form
}
return render_to_response(template, context, RequestContext(request))
示例4: product
def product(request, slug, template="shop/product.html"):
"""
Display a product - convert the product variations to JSON as well as
handling adding the product to either the cart or the wishlist.
"""
published_products = Product.objects.published(for_user=request.user)
product = get_object_or_404(published_products, slug=slug)
if not request.is_ajax():
page = Category.objects.get(slug__exact='shop')
products = Product.objects.published(for_user=request.user
).filter(page.category.filters()).distinct()
sort_options = [(slugify(option[0]), option[1])
for option in settings.SHOP_PRODUCT_SORT_OPTIONS]
sort_by = request.GET.get("sort", sort_options[0][1])
products = paginate(products.order_by(sort_by),
request.GET.get("page", 1),
settings.SHOP_PER_PAGE_CATEGORY,
settings.MAX_PAGING_LINKS)
products.sort_by = sort_by
sub_categories = page.category.children.published()
child_categories = Category.objects.filter(id__in=sub_categories)
context = RequestContext(request, locals())
return render_to_response('pages/category.html', {}, context_instance=context)
else:
fields = [f.name for f in ProductVariation.option_fields()]
variations = product.variations.all()
variations_json = simplejson.dumps([dict([(f, getattr(v, f))
for f in fields + ["sku", "image_id"]])
for v in variations])
to_cart = (request.method == "POST" and
request.POST.get("add_wishlist") is None)
initial_data = {}
if variations:
initial_data = dict([(f, getattr(variations[0], f)) for f in fields])
initial_data["quantity"] = 1
add_product_form = AddProductForm(request.POST or None, product=product,
initial=initial_data, to_cart=to_cart)
if request.method == "POST":
if add_product_form.is_valid():
if to_cart:
quantity = add_product_form.cleaned_data["quantity"]
request.cart.add_item(add_product_form.variation, quantity)
recalculate_cart(request)
return HttpResponse(request.cart.total_quantity())
else:
skus = request.wishlist
sku = add_product_form.variation.sku
if sku not in skus:
skus.append(sku)
info(request, _("Item added to wishlist"))
response = redirect("shop_wishlist")
set_cookie(response, "wishlist", ",".join(skus))
return response
context = {
"product": product,
"editable_obj": product,
"images": product.images.all(),
"variations": variations,
"variations_json": variations_json,
"has_available_variations": any([v.has_price() for v in variations]),
"related_products": product.related_products.published(
for_user=request.user),
"add_product_form": add_product_form
}
templates = [u"shop/%s.html" % unicode(product.slug), template]
return render(request, templates, context)
示例5: wishlist
def wishlist(request, template="shop/wishlist1.html"):
"""
Display the wishlist and handle removing items from the wishlist and
adding them to the cart.
"""
if not settings.SHOP_USE_WISHLIST:
raise Http404
skus = request.wishlist
error = None
if request.method == "POST":
print request.POST
print "nihao"
to_cart = request.POST.get("add_cart")
print len(to_cart)
print "to_cart:"+to_cart
#{u'sku': [u'28'], u'csrfmiddlewaretoken': [u'P5IBTacZNNoP6QzaVPMScIdje3cGBEbv'], u'add_cart': [u'Buy'], u'quantity': [u'1']}>
# test={u'sku': [u'28'], u'csrfmiddlewaretoken': [u'P5IBTacZNNoP6QzaVPMScIdje3cGBEbv'], u'add_cart': [u'Buy'], u'quantity': [u'1']}
# add_product_form = AddProductForm(test or None,
# to_cart=to_cart)
# # print request.POST
# print add_product_form
if len(to_cart)>10:
pass
tmp_post=request.POST.copy()
wish_skus=tmp_post.getlist("sku")
if wish_skus=="none":
message = _("error in wishlist")
# print "wish_skus:" + wish_skus
for i in wish_skus:
tmp_post.__setitem__("sku",i)
add_product_form = AddProductForm(tmp_post or None,
to_cart=to_cart)
if add_product_form.is_valid():
request.cart.add_item(add_product_form.variation, 1)
recalculate_cart(request)
else:
error = list(add_product_form.errors.values())[0]
# print "error:"+str(skus)
skus.remove(i)
message = _("Items added to cart")
url = "shop_cart"
elif to_cart:
add_product_form = AddProductForm(request.POST or None,
to_cart=to_cart)
if add_product_form.is_valid():
request.cart.add_item(add_product_form.variation, 1)
print add_product_form.variation
recalculate_cart(request)
message = _("Item added to cart")
url = "shop_cart"
else:
error = list(add_product_form.errors.values())[0]
else:
add_product_form = AddProductForm(request.POST or None,
to_cart=to_cart)
message = _("Item removed from wishlist")
url = "shop_wishlist"
sku = request.POST.get("sku")
print request.POST.get("sku")
print skus
if sku in skus:
skus.remove(sku)
if not error:
info(request, message)
response = redirect(url)
set_cookie(response, "wishlist", ",".join(skus))
return response
# Remove skus from the cookie that no longer exist.
published_products = Product.objects.published(for_user=request.user)
f = {"product__in": published_products, "sku__in": skus}
wishlist = ProductVariation.objects.filter(**f).select_related(depth=1)
wishlist = sorted(wishlist, key=lambda v: skus.index(v.sku))
context = {"wishlist_items": wishlist, "error": error}
response = render(request, template, context)
if len(wishlist) < len(skus):
skus = [variation.sku for variation in wishlist]
set_cookie(response, "wishlist", ",".join(skus))
return response
示例6: product
def product(request, slug, template="shop/product.html"):
"""
Display a product - convert the product variations to JSON as well as
handling adding the product to either the cart or the wishlist.
"""
address = request.session['address']
if 'cart loaded' in request.session:
current_store = request.session['stores'][0]
else:
current_store = []
published_products = Product.objects.published(for_user=request.user)
product = get_object_or_404(published_products, slug=slug)
fields = [f.name for f in ProductVariation.option_fields()]
variations = product.variations.all()
variations_json = simplejson.dumps([dict([(f, getattr(v, f))
for f in fields + ["sku", "image_id"]])
for v in variations])
to_cart = (request.method == "POST" and
request.POST.get("add_wishlist") is None)
initial_data = {}
if variations:
initial_data = dict([(f, getattr(variations[0], f)) for f in fields])
initial_data["quantity"] = 1
add_product_form = AddProductForm(request.POST or None, product=product,
initial=initial_data, to_cart=to_cart)
if request.method == "POST":
if add_product_form.is_valid():
if to_cart:
if 'cart loaded' in request.session:
current_store = Store.objects.filter(name__exact=product.store)[0]
if current_store != request.session['stores'][0]:
info(request, _("Sorry, the item cannot be added to your cart. "
"By NY law you cannot shop from two different liquor stores simultaneously."))
return HttpResponseRedirect('/shop/')
else:
request.session['cart loaded'] = 'cart loaded'
store = Store.objects.filter(name__exact=product.store)
request.session['stores'] = store
request.session['delivery min'] = store[0].delivery_min
current_store = store[0]
name = unicodedata.normalize('NFKD', current_store.name).encode('ascii','ignore')
store_slug = '/shop/'+slugify(name)+'/'
request.session['store slug'] = store_slug
quantity = add_product_form.cleaned_data["quantity"]
request.cart.add_item(add_product_form.variation, quantity)
recalculate_cart(request)
info(request, _("Item added to cart. Anything else from %s?" % name ))
# return redirect("shop_cart")
return redirect(store_slug)
else:
skus = request.wishlist
sku = add_product_form.variation.sku
if sku not in skus:
skus.append(sku)
info(request, _("Item added to wishlist"))
response = redirect("shop_wishlist")
set_cookie(response, "wishlist", ",".join(skus))
return response
context = {
"product": product,
"editable_obj": product,
"images": product.images.all(),
"variations": variations,
"variations_json": variations_json,
"has_available_variations": any([v.has_price() for v in variations]),
"related_products": product.related_products.published(
for_user=request.user),
"add_product_form": add_product_form,
"address": address,
"stores": current_store,
}
templates = [u"shop/%s.html" % unicode(product.slug), template] # new
return render(request, templates, context)
示例7: wishlist
def wishlist(request, template="shop/wishlist1.html"):
"""
Display the wishlist and handle removing items from the wishlist and
adding them to the cart.
"""
if not settings.SHOP_USE_WISHLIST:
raise Http404
skus = request.wishlist
error = None
# print "hehe"
if request.method == "POST":
# print request.POST
print "nihao"
# to_cart = request.POST.get("add_cart").decode('utf-8').encode('GBK')
to_cart = request.POST.get("add_cart")
if to_cart:
tmp_post=request.POST.copy()
wish_skus=tmp_post.getlist("sku")
if wish_skus=="none":
message = _("error in wishlist")
# print "wish_skus:" + wish_skus
for i in wish_skus:
tmp_post.__setitem__("sku",i)
add_product_form = AddProductForm(tmp_post or None,
to_cart=to_cart)
if add_product_form.is_valid():
request.cart.add_item(add_product_form.variation, 1)
recalculate_cart(request)
else:
error = list(add_product_form.errors.values())[0]
# print "error:"+str(skus)
skus.remove(i)
message = _("Items added to cart")
url = "shop_cart"
else:
add_product_form = AddProductForm(request.POST or None,
to_cart=to_cart)
message = _("Item removed from wishlist")
url = "shop_wishlist"
sku = request.POST.get("sku")
print request.POST.get("sku")
print skus
if sku in skus:
skus.remove(sku)
if not error:
info(request, message)
response = redirect(url)
set_cookie(response, "wishlist", ",".join(skus))
return response
# Remove skus from the cookie that no longer exist.
published_products = Product.objects.published(for_user=request.user)
f = {"product__in": published_products, "sku__in": skus}
wishlist = ProductVariation.objects.filter(**f).select_related(depth=1)
wishlist = sorted(wishlist, key=lambda v: skus.index(v.sku))
context = {"wishlist_items": wishlist, "error": error}
response = render(request, template, context)
if len(wishlist) < len(skus):
skus = [variation.sku for variation in wishlist]
set_cookie(response, "wishlist", ",".join(skus))
return response