def edit_product(request): if request.method == 'POST': try: reqdata = json.loads(request.body.decode("utf-8")) if not reqdata["product_name"]: return JsonResponse({ "status": False, "msg": "Product name shouldn't be empty" }) if not reqdata["brand"]: return JsonResponse({ "status": False, "msg": "Brand name shouldn't be empty" }) brand = Brand.objects.get(brand_name=reqdata["brand"]) query = "update product set product_name = %s, brand = %s, offers = %s, description = %s, additional_info = %s where product_id = %s" pgExecUpdate(query, [ reqdata["product_name"], brand.brand_id, reqdata["offers"], reqdata["description"], reqdata["additional_info"], reqdata["product_id"] ]) return JsonResponse({"status": True}) except: return JsonResponse({"status": False})
def place_order(request): if request.method == 'POST': reqdata = json.loads(request.body.decode("utf-8")) pnrno = reqdata["pnr"] shop_id = reqdata["shop_id"] paymode = reqdata["paymode"] cost = 0 wallet_amount = 0 try: for item in reqdata["items"]: qry = "select cost_per_plate from fooditem where item_id = %s" resultset = pgExecQuery(qry, [item["id"]]) cost += resultset[0].cost_per_plate * item["quantity"] if paymode == Order.MODE_WALLET: qry = "select wallet_amount from customer where user_id = %s" resultset = pgExecQuery(qry, [request.user.id]) wallet_amount = resultset[0].wallet_amount if cost > wallet_amount: return JsonResponse({ "status": False, "msg": "Balance in your wallet is insufficient to place the order" }) except: return JsonResponse({ "status": False, "msg": "Unknown Error Occured" }) try: resp = {"status": True} resp["order_ids"] = [] for item in reqdata["items"]: qry = "insert into orders(pnr_no, cust_id, shop_id, item_id, quantity, status, paymode, time_stamp) values (%s,%s,%s,%s,%s,%s,%s,now()) returning order_id" resultset = pgExecQuery(qry, [ pnrno, request.user.id, shop_id, item["id"], item["quantity"], Order.STATUS_PENDING, paymode ]) resp["order_ids"].append(resultset[0].order_id) if paymode == Order.MODE_WALLET: qry = "update customer set wallet_amount = %s where user_id = %s" pgExecUpdate(qry, [wallet_amount - cost, request.user.id]) return JsonResponse(resp) except: return JsonResponse({ "status": False, "msg": "Unknown Error Occured" }) else: return JsonResponse({ "status": False, "msg": "Expected method = HTTP POST" })
def cancel_order(request): if request.method == 'POST': try: reqdata = json.loads(request.body.decode("utf-8")) qry = "update orders set status = %s where order_id = %s" pgExecUpdate(qry, [Order.STATUS_CANCELLED, reqdata["order_id"]]) return JsonResponse({"status": True}) except: return JsonResponse({"status": False}) else: return JsonResponse({ "status": False, "msg": "Expected method = HTTP POST" })
def edit_brand(request): if request.method == 'POST': try: reqdata = json.loads(request.body.decode("utf-8")) if not reqdata["brand_name"]: return JsonResponse({ "status": False, "msg": "Brand name shouldn't be empty" }) query = "update brand set brand_name = %s where brand_id = %s " pgExecUpdate(query, [reqdata["brand_name"], reqdata["brand_id"]]) return JsonResponse({"status": True}) except: return JsonResponse({"status": False})
def add_item(request): if request.method == 'POST': try: reqdata = json.loads(request.body.decode("utf-8")) if not reqdata["item_name"]: return JsonResponse({ "status": False, "msg": "Item name shouldn't be empty" }) if not reqdata["product"]: return JsonResponse({ "status": False, "msg": "Product name shouldn't be empty" }) if not reqdata["item_cost"]: return JsonResponse({ "status": False, "msg": "Item Cost shouldn't be empty" }) if Item.objects.filter(item_name=reqdata["item_name"]).exists(): return JsonResponse({ "status": False, "msg": "Given Item already exists" }) if not Product.objects.filter( product_name=reqdata["product"]).exists(): return JsonResponse({ "status": False, "msg": "Product doesn't exist" }) product = Product.objects.get(product_name=reqdata["product"]) query = "insert into item (item_name, product, item_cost) values (%s, %s, %s)" pgExecUpdate(query, [ reqdata["item_name"], product.product_id, reqdata["item_cost"] ]) return JsonResponse({"status": True}) except: return JsonResponse({"status": False})
def delete_brand(request): if request.method == 'POST': try: reqdata = json.loads(request.body.decode("utf-8")) if not Brand.objects.filter(brand_id=reqdata["brand_id"]).exists(): return JsonResponse({ "status": False, "msg": "Brand doesn't exist" }) query = "delete from brand where brand_id = %s" pgExecUpdate(query, [reqdata["brand_id"]]) return JsonResponse({"status": True, "msg": "brand deleted"}) except: return JsonResponse({"status": False, "msg": "internal error"})
def post_review(request): if request.method == 'POST': try: reqdata = json.loads(request.body.decode("utf-8")) qry = "insert into review (cust_id, shop_id, msg) values(%s, %s, %s)" pgExecUpdate(qry, [request.user.id, reqdata["shop_id"], reqdata["msg"]]) posted_rating = reqdata["rating"] if posted_rating != 0: qry = "select rating, numratings from shop where shop_id=%s" resultset = pgExecQuery(qry, [reqdata["shop_id"]]) _r = resultset[0].rating _nr = resultset[0].numratings new_rating = (_r * _nr + posted_rating) / (1 + _nr) qry = "update shop set rating = %s, numratings = %s where shop_id = %s" pgExecUpdate(qry, [new_rating, 1 + _nr, reqdata["shop_id"]]) return JsonResponse({"status": True}) except: return JsonResponse({"status": False})
def status_change(request): try: reqdata = json.loads(request.body.decode("utf-8")) order_ids = reqdata["order_ids"] for order_id in order_ids: qry = "select status from orders where order_id = %s" resultset = pgExecQuery(qry, [order_id]) if resultset[0].status != Order.STATUS_CANCELLED: qry = "update orders set status = %s where order_id = %s" pgExecUpdate(qry, [reqdata["status"], order_id]) return JsonResponse({ "status": True, "msg": "Success", "order_id": order_id, "statuschange": reqdata["status"] }) except: return JsonResponse({"status": False, "msg": "Failed"})
def delete_product(request): if request.method == 'POST': try: reqdata = json.loads(request.body.decode("utf-8")) if not Product.objects.filter( product_id=reqdata["product_id"]).exists(): return JsonResponse({ "status": False, "msg": "Product doesn't exist" }) query = "delete from product where product_id = %s" pgExecUpdate(query, [reqdata["product_id"]]) return JsonResponse({"status": True, "msg": "product deleted"}) except: return JsonResponse({"status": False, "msg": "internal error"})
def edit_item(request): if request.method == 'POST': try: reqdata = json.loads(request.body.decode("utf-8")) if not reqdata["item_name"]: return JsonResponse({ "status": False, "msg": "Item name shouldn't be empty" }) if not reqdata["product"]: return JsonResponse({ "status": False, "msg": "Product name shouldn't be empty" }) if not reqdata["item_cost"]: return JsonResponse({ "status": False, "msg": "Item Cost shouldn't be empty" }) if not Product.objects.filter( product_name=reqdata["product"]).exists(): return JsonResponse({ "status": False, "msg": "Product doesn't exist" }) product = Product.objects.get(product_name=reqdata["product"]) query = "update item set item_name = %s, product = %s, item_cost = %s where item_id = %s" pgExecUpdate(query, [ reqdata["item_name"], product.product_id, reqdata["item_cost"], reqdata["item_id"] ]) return JsonResponse({"status": True}) except: return JsonResponse({"status": False})
def add_product(request): if request.method == 'POST': try: reqdata = json.loads(request.body.decode("utf-8")) if not reqdata["product_name"]: return JsonResponse({ "status": False, "msg": "Product name shouldn't be empty" }) if Product.objects.filter( product_name=reqdata["product_name"]).exists(): return JsonResponse({ "status": False, "msg": "Given Product already exists" }) if not reqdata["brand"]: return JsonResponse({ "status": False, "msg": "Brand name shouldn't be empty" }) if not Brand.objects.filter(brand_name=reqdata["brand"]).exists(): return JsonResponse({ "status": False, "msg": "Brand doesn't exist" }) brand = Brand.objects.get(brand_name=reqdata["brand"]) query = "insert into product (product_name, brand, offers, description, additional_info) values (%s, %s, %s, %s, %s)" pgExecUpdate(query, [ reqdata["product_name"], brand.brand_id, reqdata["offers"], reqdata["description"], reqdata["additional_info"] ]) return JsonResponse({"status": True}) except: return JsonResponse({"status": False})
def add_wallet_amount(request): if request.method == 'POST': try: reqdata = json.loads(request.body.decode("utf-8")) qry = "select wallet_amount from customer where user_id = %s" resultset = pgExecQuery(qry, [request.user.id]) wallet_amount = resultset[0].wallet_amount + reqdata["amount"] qry = "update customer set wallet_amount = %s where user_id = %s" pgExecUpdate(qry, [wallet_amount, request.user.id]) return JsonResponse({ "status": True, "wallet_amount": wallet_amount }) except: return JsonResponse({"status": False}) else: return JsonResponse({ "status": False, "msg": "Expected method = HTTP POST" })
def add_brand(request): if request.method == 'POST': try: reqdata = json.loads(request.body.decode("utf-8")) if not reqdata["brand_name"]: return JsonResponse({ "status": False, "msg": "Brand name shouldn't be empty" }) if Brand.objects.filter(brand_name=reqdata["brand_name"]).exists(): return JsonResponse({ "status": False, "msg": "Given Brand already exists" }) query = "insert into brand (brand_name) values (%s)" pgExecUpdate(query, [reqdata["brand_name"]]) return JsonResponse({"status": True}) except: return JsonResponse({"status": False})
def delete_item(request): if request.method == 'POST': try: reqdata = json.loads(request.body.decode("utf-8")) if not Item.objects.filter(item_id=reqdata["item_id"]).exists(): return JsonResponse({ "status": False, "msg": "Item doesn't exist" }) query = "delete from item where item_id = %s" try: pgExecUpdate(query, [reqdata["item_id"]]) except: return JsonResponse({"status": True, "msg": "internal error"}) return JsonResponse({"status": True, "msg": "item deleted"}) except: return JsonResponse({"status": False, "msg": "internal error"})
sample_cust_user_ids = ['10001', '10002'] for cid in sample_cust_user_ids: user = User(username=cid) user.set_password(cid) user.first_name = "CUSTOMER-" + cid[4] user.save() customer = Customer(user=user, wallet_amount=0, mobile="0000000000") customer.time_stamp = datetime.now() customer.save() # Stations for line in open('popdata/popdata0.sql'): if len(line) > 25: pgExecUpdate(line) sample_shop_data = [ ['00001', 'KFC', '00001'], ['00002', 'Dominos Pizza', '00001'], ['00003', 'Subway', '00001'], ['00004', 'McDonalds', '00002'], ['00005', 'Pizza Hut', '00002'], ['00006', 'Box8', '00002'], ['00007', 'StarBucks', '00002'], ['00008', 'Spot Burgers', '00003'], ['00009', 'Pizza Hut', '00003'], ['00010', 'Dominos Pizza', '00003'], ['00011', 'Cafe Coffee Day', '00003'], ['00012', 'Papa John' 's Pizza', '00003'],