Demo entry 6716186

test

   

Submitted by sd on Feb 16, 2018 at 08:18
Language: Python 3. Code size: 4.0 kB.

from django.http import JsonResponse

from django.shortcuts import render, redirect

from .models import Cart

from products.models import Product

from orders.models import Order

from accounts.models import GuestEmail

from accounts.forms import LoginForm, GuestForm

from billing.models import BillingProfile

# Create your views here.


def cart_detail_api_view(request):
    cart_obj, new_obj = Cart.objects.new_or_get(request)
    products = [{"name": x.name, "price": x.price}
                for x in cart_obj.products.all()]
    # products_list = []

    # for x in cart_obj.products.all():
    #     products_list.append(
    #         {"name": x.name, "price": x.price}
    #     )

    cart_data = {"products": products,
                 "subtotal": cart_obj.subtotal, "total": cart_obj.total}

    return JsonResponse(cart_data)


def cart_home(request):
    cart_obj, new_obj = Cart.objects.new_or_get(request)
    # request.session['cart_id'] = '12'
    # cart_id = request.session.get("cart_id", None)
    # qs = Cart.objects.filter(id=cart_id)
    # if qs.count() == 1:
    #     cart_obj = qs.first()
    #     if request.user.is_authenticated and cart_obj.user is None:
    #         cart_obj.user = request.user
    #         cart_obj.save()
    # else:
    #     cart_obj = Cart.objects.new(user=request.user)
    #     request.session['cart_id'] = cart_obj.id
    # print(request.session)
    # print(dir(request.session))
    # request.session.set_expiry(300)
    # key = request.session.session_key
    # print(key)
    return render(request, "carts/home.html", {"cart": cart_obj})


def cart_update(request):
    product_id = request.POST.get('product_id')
    if product_id is not None:
        try:
            print("if product_id is not None: try")
            product_obj = Product.objects.get(id=product_id)
        except Product.DoesNotExist:
            print("except Product.DoesNotExist:")
            print("message for user: product is gone")
            return redirect("cart:home")
        cart_obj, new_obj = Cart.objects.new_or_get(request)
        if product_obj in cart_obj.products.all():
            print("if product_obj in cart_obj.products.all():")
            cart_obj.products.remove(product_obj)
            added = False
        else:
            print("if product_obj in cart_obj.products.all(): else")
            cart_obj.products.add(product_obj)
            added = True
        request.session['cart_items'] = cart_obj.products.count()
    # return redirect(product_obj.get_absolute_url())
        if request.is_ajax():
            json_data = {
                "added": added,
                "remove": not added,
                "navCartCount": cart_obj.products.count(),
            }
            return JsonResponse(json_data)
    return redirect("cart:home")


def checkout_home(request):
    cart_obj, cart_created = Cart.objects.new_or_get(request)
    order_obj = None
    if cart_created or cart_obj.products.count() == 0:
        return redirect("cart:home")
    else:
        order_obj, new_order_obj = Order.objects.get_or_create(cart=cart_obj)
    user = request.user
    billing_profile = None
    login_form = LoginForm()
    guest_form = GuestForm()
    guest_email_id = request.session.get('guest_email_id')
    if user.is_authenticated:
        billing_profile = BillingProfile.objects.get_or_create(
            user=user, email=user.email)
    elif guest_email_id is not None:
        print('guest_form')
        guest_email_obj = GuestEmail.objects.get(id=guest_email_id)
        billing_profile, billing_profile_created = BillingProfile.objects.get_or_create(
            email=guest_email_obj.email)
    else:
        pass

    context = {
        "object": order_obj,
        "billing_profile": billing_profile,
        "login_form": login_form,
        "guest_form": guest_form
    }
    return render(request, "carts/checkout.html", context)

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).