Beispiel #1
0
def show_product(request, product_slug, template_name="catalog/product.html"):
    p = get_object_or_404(Product, slug=product_slug)
    categories = p.categories.all()
    page_title = p.name
    meta_keywords = p.meta_keywords
    meta_description = p.meta_description
    # evaluate the HTTP method, change as needed
    if request.method == 'POST':
        #create the bound form
        postdata = request.POST.copy()
        form = ProductAddToCartForm(request, postdata)
        #check if posted data is valid
        if form.is_valid():
            #add to cart and redirect to cart page
            cart.add_to_cart(request)
            # if test cookie worked, get rid of it
            if request.session.test_cookie_worked():
                request.session.delete_test_cookie()
            url = urlresolvers.reverse('show_cart')
            return HttpResponseRedirect(url)
    else:
        #create the unbound form. Notice the request as a keyword argument
        form = ProductAddToCartForm(request=request, label_suffix=':')
    # assign the hidden input the product slug
    form.fields['product_slug'].widget.attrs['value'] = product_slug
    # set test cookie to make sure cookies are enabled
    request.session.set_test_cookie()
    return render_to_response(template_name, locals(), 
                              context_instance=RequestContext(request))
Beispiel #2
0
def show_product(request, product_slug, template_name="catalog/product.html"):
    p = get_object_or_404(Product, slug=product_slug)
    categories = p.categories.all()
    page_title = p.name
    meta_keywords = p.meta_keywords
    meta_description = p.meta_description
    # need to evaluate the HTTP method
    if request.method == 'POST':
    # add to cart create the bound form
        postdata = request.POST.copy()
        form = ProductAddToCartForm(request, postdata)
        #check if posted data is valid
        if form.is_valid():
            #add to cart and redirect to cart page
            cart.add_to_cart(request)
            # if test cookie worked, get rid of it
            if request.session.test_cookie_worked():
                request.session.delete_test_cookie()
            url = urlresolvers.reverse('show_cart')
            return HttpResponseRedirect(url)
    else:
        # its a GET,create the unbound form. Note request as a kwarg
        form = ProductAddToCartForm(request=request, label_suffix=':')
        # assign the hidden input the product slug
    form.fields['product_slug'].widget.attrs['value'] = product_slug
    # set the test cookie on our first GET request
    request.session.set_test_cookie()
    stats.log_product_view(request, p)  # add to product view
    product_reviews = ProductReview.approved.filter(product=p).order_by('-date')
    review_form = ProductReviewForm()
    return render_to_response("catalog/product.html", locals(), context_instance=RequestContext(request))
Beispiel #3
0
def show_product(request, product_slug, template_name="pdcts/product.html"):

    product_cache_key = request.path
    # try to get product from cache
    p = cache.get(product_cache_key)
    # if a cache miss, fall back on db query
    if not p:
        p = get_object_or_404(Product.active, slug=product_slug)
        # store item in cache for next time
        cache.set(product_cache_key, p, CACHE_TIMEOUT)
    product_category = p.Category.all()
    page_title = p.product_name
    # meta_keywords = p.meta_keywords
    # meta_description = p.meta_description
    # evaluate the HTTP method, change as needed
    if request.method == 'POST':
        #create the bound form
        postdata = request.POST.copy()
        form = ProductAddToCartForm(request, postdata)
        #check if posted data is valid
        if form.is_valid():
            #add to cart and redirect to cart page
            cart.add_to_cart(request)
            # if test cookie worked, get rid of it
            if request.session.test_cookie_worked():
                request.session.delete_test_cookie()
            url = urlresolvers.reverse('show_cart')
            return HttpResponseRedirect(url)
    else:
        #create the unbound form. Notice the request as a keyword argument
        form = ProductAddToCartForm(request=request, label_suffix=':')
    # assign the hidden input the product slug
    form.fields['product_slug'].widget.attrs['value'] = product_slug
    # set test cookie to make sure cookies are enabled
    request.session.set_test_cookie()
    stats.log_product_view(request, p)
    # product review additions
    product_reviews = ProductReview.approved.filter(
        product=p).order_by('-date')
    store_reviews = StoreReview.objects.filter(
        product=p).order_by('rating_votes')[:3]
    reputation = User_Reputation.objects.get(user=p.user)
    user_rep = reputation.reputation * 100
    votes = reputation.votes

    review_form = ProductReviewForm()
    return render_to_response(template_name,
                              locals(),
                              context_instance=RequestContext(request))
Beispiel #4
0
def show_product(request, product_slug, template_name="pdcts/product.html"):

    product_cache_key = request.path
    # try to get product from cache
    p = cache.get(product_cache_key)
    # if a cache miss, fall back on db query
    if not p:
        p = get_object_or_404(Product.active, slug=product_slug)
        # store item in cache for next time
        cache.set(product_cache_key, p, CACHE_TIMEOUT)
    product_category = p.Category.all()
    page_title = p.product_name
    # meta_keywords = p.meta_keywords
    # meta_description = p.meta_description
    # evaluate the HTTP method, change as needed
    if request.method == 'POST':
        #create the bound form
        postdata = request.POST.copy()
        form = ProductAddToCartForm(request, postdata)
        #check if posted data is valid
        if form.is_valid():
            #add to cart and redirect to cart page
            cart.add_to_cart(request)
            # if test cookie worked, get rid of it
            if request.session.test_cookie_worked():
                request.session.delete_test_cookie()
            url = urlresolvers.reverse('show_cart')
            return HttpResponseRedirect(url)
    else:
        #create the unbound form. Notice the request as a keyword argument
        form = ProductAddToCartForm(request=request, label_suffix=':')
    # assign the hidden input the product slug
    form.fields['product_slug'].widget.attrs['value'] = product_slug
    # set test cookie to make sure cookies are enabled
    request.session.set_test_cookie()
    stats.log_product_view(request, p)
    # product review additions
    product_reviews = ProductReview.approved.filter(product=p).order_by('-date')
    store_reviews = StoreReview.objects.filter(product = p).order_by('rating_votes')[:3]
    reputation = User_Reputation.objects.get(user=p.user)
    user_rep = reputation.reputation*100
    votes = reputation.votes

    review_form = ProductReviewForm()
    return render_to_response(template_name, locals(), context_instance=RequestContext(request))
Beispiel #5
0
def show_product(request, product_slug, template_name="catalog/product_page.html"):
  """Renders the product view, on POST adds product to cart
  """
  p = get_object_or_404(Product, slug=product_slug)
  context = set_context(request, {
    'product': p,
    # 'categories': p.categories.filter(is_active=True),
    'categories': p.categories.all(),
    'page_title': p.name,
    'meta_keywords': p.meta_keywords,
    'meta_description': p.meta_description,
  })

  if request.method == 'POST':
    # add to cart...create the bound form
    postdata = request.POST.copy()
    form = ProductAddToCartForm(request, postdata)

    # check if posted data is valid
    if form.is_valid():
      # add to cart and redirect to cart page
      cart.add_to_cart(request)

      # if test cookie worked, get rid of it
      if request.session.test_cookie_worked():
        request.session.delete_test_cookie()

      # redirect user to cart view
      url = urlresolvers.reverse('show_cart')
      return HttpResponseRedirect(url)
  else:
    # GET request, note the request as a kwarg
    form = ProductAddToCartForm(request=request, label_suffix=':')

  # assign the hidden input the product slug
  form.fields['product_slug'].widget.attrs['value'] = product_slug

  # add form to context
  context['form'] = form

  # set the test cookie on our first GET request
  request.session.set_test_cookie()

  return render(request, template_name, context)
Beispiel #6
0
def show_product(request, product_slug, template_name="catalog/product.html"):
    p = get_object_or_404(Product, slug=product_slug)
    categories = p.categories.filter(is_active=True)
    title = p.name
    if request.method == "POST":
        postdata = request.POST.copy()
        form = ProductAddToCartForm(request, postdata)
        if form.is_valid():
            cart.add_to_cart(request)
            if request.session.test_cookie_worked():
                request.session.delete_test_cookie()
            url = urlresolvers.reverse('show_cart')
            return redirect(url)
    else:
        form = ProductAddToCartForm(request=request, label_suffix=":")

    form.fields['product_slug'].widget.attrs['value'] = product_slug
    request.session.set_test_cookie()
    return render_to_response("catalog/product.html", locals(), context_instance=RequestContext(request))
Beispiel #7
0
 def post(self, request, template_name="flavors.html"):
     # add to cart...create the bound form
     status = 200
     response = {
         'error': True,
     }
     postdata = request.POST.copy().dict()
     form = ProductAddToCartForm(request, postdata)
     #check if posted data is valid
     if form.is_valid():
         #add to cart and redirect to cart page
         available_qty, cart_qty, added = add_to_cart(request)
         # if test cookie worked, get rid of it
         if request.session.test_cookie_worked():
             request.session.delete_test_cookie()
             request.session.set_test_cookie()
         # url = urlresolvers.reverse('cart:show_cart')
         response.update({'error': False})
         cart_data = get_cart_data(request)
         cart_data.pop(
             'actual_cart_items'
         )  # actual cart items are django objects which are not json serializable hence we remove it
         cart_data.pop('checkout_cart_items')
         cart_data.update({
             'checkout_url':
             reverse('checkout:checkout', args=[]),
             'authenticated':
             request.user.is_authenticated(),
             'cart_url':
             reverse('cart:show_cart', args=[]),
         })
         response.update({
             'data': cart_data,
             'available_qty': available_qty,
             'cart_qty': cart_qty,
             'added': added
         })
         return JsonResponse(data=response, status=status, safe=False)
     else:
         response.update(
             {'msg': "Due to some reason we were unable to update cart"})
         return JsonResponse(data=response, status=status, safe=False)