Skip to content

Commit

Permalink
Create m4-p4-s3-views.py
Browse files Browse the repository at this point in the history
  • Loading branch information
divanov11 authored Apr 24, 2020
1 parent 4a65ebd commit 485e4f8
Showing 1 changed file with 108 additions and 0 deletions.
108 changes: 108 additions & 0 deletions m4-p4-s3-views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
from django.shortcuts import render
from django.http import JsonResponse
import json
import datetime
from .models import *
from .utils import cookieCart

def store(request):

if request.user.is_authenticated:
customer = request.user.customer
order, created = Order.objects.get_or_create(customer=customer, complete=False)
items = order.orderitem_set.all()
cartItems = order.get_cart_items
else:
cookieData = cookieCart(request)
cartItems = cookieData['cartItems']
order = cookieData['order']
items = cookieData['items']

products = Product.objects.all()
context = {'products':products, 'cartItems':cartItems}
return render(request, 'store/store.html', context)

def cart(request):

if request.user.is_authenticated:
customer = request.user.customer
order, created = Order.objects.get_or_create(customer=customer, complete=False)
items = order.orderitem_set.all()
cartItems = order.get_cart_items
else:
cookieData = cookieCart(request)
cartItems = cookieData['cartItems']
order = cookieData['order']
items = cookieData['items']

context = {'items':items, 'order':order, 'cartItems':cartItems}
return render(request, 'store/cart.html', context)

def checkout(request):
if request.user.is_authenticated:
customer = request.user.customer
order, created = Order.objects.get_or_create(customer=customer, complete=False)
items = order.orderitem_set.all()
cartItems = order.get_cart_items
else:
#Create empty cart for now for non-logged in user
cookieData = cookieCart(request)
cartItems = cookieData['cartItems']
order = cookieData['order']
items = cookieData['items']

context = {'items':items, 'order':order, 'cartItems':cartItems}
return render(request, 'store/checkout.html', context)

def updateItem(request):
data = json.loads(request.body)
productId = data['productId']
action = data['action']
print('Action:', action)
print('Product:', productId)

customer = request.user.customer
product = Product.objects.get(id=productId)
order, created = Order.objects.get_or_create(customer=customer, complete=False)

orderItem, created = OrderItem.objects.get_or_create(order=order, product=product)

if action == 'add':
orderItem.quantity = (orderItem.quantity + 1)
elif action == 'remove':
orderItem.quantity = (orderItem.quantity - 1)

orderItem.save()

if orderItem.quantity <= 0:
orderItem.delete()

return JsonResponse('Item was added', safe=False)

def processOrder(request):
transaction_id = datetime.datetime.now().timestamp()
data = json.loads(request.body)

if request.user.is_authenticated:
customer = request.user.customer
order, created = Order.objects.get_or_create(customer=customer, complete=False)
total = float(data['form']['total'])
order.transaction_id = transaction_id

if total == order.get_cart_total:
order.complete = True
order.save()

if order.shipping == True:
ShippingAddress.objects.create(
customer=customer,
order=order,
address=data['shipping']['address'],
city=data['shipping']['city'],
state=data['shipping']['state'],
zipcode=data['shipping']['zipcode'],
)
else:
print('User is not logged in')

return JsonResponse('Payment submitted..', safe=False)

0 comments on commit 485e4f8

Please sign in to comment.