Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Equal-Distribution-maintenance-system/EDMS/shoppingCart/views.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
77 lines (54 sloc)
2.38 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.shortcuts import render,redirect | |
from django.http import HttpResponse | |
from .models import * | |
# Create your views here. | |
from django.http.response import JsonResponse | |
import json | |
from django.contrib import messages | |
# main page | |
def main(request): | |
customer = Customer.objects.get(id=1) | |
order, created = Order.objects.get_or_create(customer=customer, complete= False) | |
customerOrderItem = order.orderitem_set.all() | |
products = Product.objects.all() | |
context = {'customer':customer, 'order':order, 'customerOrderItem':customerOrderItem, 'products':products} | |
return render(request,'shoppingCart/main.html', context) | |
#Updateitem view | |
def updateItem(request): | |
data = json.loads(request.body) | |
productId = data['productId'] | |
action = data['action'] | |
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 orderItem.quantity >= 9 : | |
messages.error(request, 'You bought 9 ' + str(orderItem.product) +"'s this week already") | |
exit | |
elif action == 'add': | |
orderItem.quantity = (orderItem.quantity + 1) | |
elif action =='remove': | |
orderItem.quantity = (orderItem.quantity -1) | |
orderItem.save() | |
if orderItem.quantity <= 0: | |
orderItem.delete() | |
print('ProductID: ', productId, 'Action:',action) | |
return JsonResponse('item added', safe=False) | |
def store_dashBoard(request): | |
store = Store.objects.all() | |
total_stores = store.count() | |
products = Product.objects.all() | |
delivered = StoreOrder.objects.filter(status='Delivered').count() | |
storeorder = StoreOrder.objects.all() | |
context = {'store': store, 'total_stores':total_stores,'products':products, 'storeorder':storeorder, 'delivered':delivered, } | |
return render(request, 'ShoppingCart/store_dashBoard.html', context) | |
def products(request): | |
products = Product.objects.all() | |
context = {'products': products } | |
return render(request, 'ShoppingCart/products.html', context) | |
def allocate(request): | |
return render(request, 'ShoppingCart/allocate.html') | |
def storeorder(request): | |
storeorder = StoreOrder_set.objects.all() | |
context = { 'store': store,} | |
return render(request, 'ShoppingCart/store.html', context) |