Example #1
0
def show_checkout(request):
    if cart.is_empty(request):
        cart_url = urlresolvers.reverse('show_cart')
        return HttpResponseRedirect(cart_url)

    if request.method == 'POST':
        post_data = request.POST.copy()
        form = CheckoutForm(post_data)
        if form.is_valid():
            response = checkout.process(request)
            order_number = response.get('order_number', 0)
            error_message = response.get('message', '')
            if order_number:
                request.session['order_number'] = order_number
                receipt_url = urlresolvers.reverse('checkout_receipt')
                return HttpResponseRedirect(receipt_url)
            else:
                return HttpResponse(error_message)
        else:
            error_message = 'Corrige los errores abajo'
    else:
        # si la peticion es get, tratar de vincular el form con los datos del perfil del usuario
        if request.user.is_authenticated():
            user_profile = profile.get_profile(request)
            form = CheckoutForm(instance=user_profile, label_suffix="")
        else:
            form = CheckoutForm(label_suffix="")
    page_title = 'Checkout'
    small_text = u"Total en el carrito"
    big_text = "%.2fcuc" % OrderTotal.objects.get(
        id=request.session['ordertotalid']).total
    return render_to_response('checkout/checkout.html',
                              locals(),
                              context_instance=RequestContext(request))
Example #2
0
def show_product(request, product_slug):
    """
    new product view, with POST vs GET detection
    """
    p = get_object_or_404(Product, slug=product_slug)
    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
        post_data = request.POST.copy()
        product_add_to_cart_form = ProductAddToCartForm(request, post_data)
        # check if posted data is valid
        if product_add_to_cart_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:
        # it's a GET, create the unbound form. Note request as a kwarg
        product_add_to_cart_form = ProductAddToCartForm(request=request,
                                                        label_suffix=':')
        # assign the hidden input the product slug
        product_add_to_cart_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)

    customers_who_bought_this_p_also_bought = utils.products_bought_together(p)

    product_reviews = ProductReview.approved.filter(
        product=p).order_by('-date')
    review_form = ProductReviewForm()
    review_rating = ProductRatingForm()
    # rating promedio
    avg_rating = p.avg_rating()
    cant_rating = p.productrating_set.count()
    desired_by_user = False
    user_already_voted = 0
    if request.user.is_authenticated():
        # el usuario loguead ya voto por este prod?
        try:
            user_product_rating = ProductRating.objects.get(user=request.user,
                                                            product=p)
            user_already_voted = user_product_rating.rating
        except ProductRating.DoesNotExist:
            user_already_voted = 0
        # deseado por el usuario logueado?
        user_profile = profile.get_profile(request)
        if p in user_profile.wish_list.all():
            desired_by_user = True
    return render_to_response("catalog/product.html",
                              locals(),
                              context_instance=RequestContext(request))
Example #3
0
def create_order(request, order_total, transaction_id):
    order = Order()
    checkout_form = CheckoutForm(request.POST, instance=order)
    order = checkout_form.save(commit=False)
    order.transaction_id = transaction_id
    order.ip_address = request.META.get('REMOTE_ADDR')
    order.status = Order.SUBMITTED
    order.user = None
    # order.order_total = order_total
    order_total.purchased = True

    if request.user.is_authenticated():
        order.user = request.user
        from accounts import profile

        user_profile = profile.get_profile(request)
        # verificar si campos requeridos estan vacios, si lo estan crear un user_profile para este usuario desde
        # aqui, asi no tendria q escribirlo en el form UserProfileForm
        if not user_profile.email or not user_profile.shipping_name or user_profile.shipping_city == 0:
            profile.set_profile(request)
    order.save()
    order_total.order = order
    order_total.save()
    # if the order save succeeded
    if order.pk:
        # verificar si el usuario tuvo la promo4, para eliminarla
        try:
            promo4_id = request.session['promo4_id']
            del request.session['promo4_id']
            promo4 = Promo4.objects.get(id=promo4_id)
            promo4.active = False
            promo4.save()
        except KeyError:
            pass
        try:  # eliminar el codigo de promoion para el usuario
            promo_id = request.session['promo3_id']
            del request.session['promo3_id']
            Promo3.objects.get(id=promo_id).delete()
        except KeyError:
            pass
        cart_items = cart.get_cart_items(request)
        for ci in cart_items:
            # create order item for each cart item
            oi = OrderItem()
            oi.order = order
            oi.quantity = ci.quantity
            oi.price = ci.price()  # now using @property
            oi.product = ci.product
            # disminuir del almacen la cant disponible para este prod
            ci.product.quantity -= ci.quantity
            ci.product.save()
            oi.save()
            # all set, empty cart
        cart.empty_cart(request)
        # utils.send_email("Leoshop", "Gracias por preferirnos", user_profile.email)
    # return the new order object
    return order
Example #4
0
def remove_wish_list(request):
    slug = request.POST.get('slug')
    product = Product()
    user_profile = profile.get_profile(request)
    try:
        product = user_profile.wish_list.get(slug=slug)
        user_profile.wish_list.remove(product)
        response = json.dumps({'success': 'True'})
    except product.DoesNotExist:
        response = json.dumps({'success': 'False'})
    return HttpResponse(response,
                        content_type='application/javascript; charset=utf-8')
Example #5
0
def add_wish_list(request):
    slug = request.POST.get('slug')
    product = Product.active.get(slug=slug)
    print("product", product)
    user_profile = profile.get_profile(request)
    print("wishlist", user_profile.wish_list.all())
    if product not in user_profile.wish_list.all():
        user_profile.wish_list.add(product)
        response = json.dumps({'success': 'True'})
    else:
        response = json.dumps({'success': 'False'})
    print("wishlist", user_profile.wish_list.all())
    return HttpResponse(response,
                        content_type='application/javascript; charset=utf-8')