def Salad(request): item_selections = request.POST if len(item_selections) !=1: Total_cost = 0.0 if Order.objects.last() == None: od = Order(1) else: od = Order(Order.objects.last().id+1) od.save() for i in item_selections.getlist("salad"): obj = Menu.objects.all().filter(id=i) Total_cost += obj[0].Price_small od.Others.add(obj[0]) od.order_total = Total_cost od.save() if Cart.objects.all().filter(user =str(request.user),order_status=False).last() == None: if Cart.objects.last() == None: cart = Cart(1,user =str(request.user)) else: cart = Cart(Cart.objects.last().id + 1,user =str(request.user)) cart.save() else: cart = Cart.objects.all().filter(user =str(request.user),order_status=False).first() cart.stuff.add(od) cart.Total += Total_cost cart.save() return JsonResponse({"output":"Nope"}) else: return JsonResponse({"output":"Fail"})
def post(self, request): user_data = json.loads(request.body) try: if User.objects.filter(account=user_data['account']).exists(): return HttpResponse(status=400) if self.invalid_account(user_data['account']): return HttpResponse(status=400) validate_email(user_data['email']) if self.invalid_password(user_data['password']): return HttpResponse(status=400) if self.invalid_phone(user_data['phone']): return HttpResponse(status=400) if user_data['name'] is None: return HttpResponse(status=400) with transaction.atomic(): password = bcrypt.hashpw(user_data['password'].encode('utf-8'), bcrypt.gensalt()) user_model = User( account=user_data['account'], grade=Grade.objects.get(id=1), password=password, name=user_data['name'], email=user_data['email'], phone=user_data['phone'], gender=Gender.objects.get(name=user_data['gender']), birth=user_data['birthday']) user_model.save() Address(user=User.objects.get(id=user_model.id), address=user_data['address'], is_capital_area=self.check_capital_area( user_data['address'])).save() Cart(user=User.objects.get(id=user_model.id)).save() return JsonResponse( { "data": { 'account': user_data['account'], 'name': user_data['name'], 'email': user_data['email'] } }, status=200) except KeyError: return HttpResponse(status=400) except ValidationError: return HttpResponse(status=400)
def add_to_cart(request): goodsid = request.GET.get('goodsid') carts = Cart.objects.filter(c_user=request.user).filter(c_goods_id=goodsid) if carts.exists(): cart_obj = carts.first() cart_obj.c_goods_num = cart_obj.c_goods_num + 1 else: cart_obj = Cart() cart_obj.c_goods_id = goodsid cart_obj.c_user = request.user cart_obj.save() data = { 'status': 200, 'msg': 'add success', 'c_goods_num': cart_obj.c_goods_num } return JsonResponse(data)
topping_qty=Topping_qty.objects.get(pk=topping_qty), size=Size.objects.get(pk=size), price=price) product.save() if __name__ == "__main__": main() f = open("toppings.csv") reader = csv.reader(f) for name in reader: topping = Topping(name=name) topping.save() c = Cart(user_id=User.objects.get(pk=1), menu_id=MenuPrice.objects.get(pk=1)) import csv from orders.models import MenuPrice, Category, Topping_qty, Size f = open("products.csv") reader = csv.reader(f) for category, name, topping_qty, size, price in reader: if topping_qty == "" and size == "": product = MenuPrice(category=Category.objects.get(pk=category), name=name, topping_qty=None, size=None, price=price) elif topping_qty == "": product = MenuPrice(category=Category.objects.get(pk=category),
def Regular(request): item_selections =request.POST items = {} #print(item_selections) order = [None,None,None,None,None] for i in ["base","items","specials"]: if i in item_selections: if i == "base": order[0] = item_selections.getlist(i)[0] elif i == "items": for n in range(len(item_selections.getlist(i))): order[1+n] = item_selections.getlist(i)[n] else: order[-1] = item_selections.getlist(i)[0] if "specials" in item_selections: if "specials_size" in item_selections: items['specials']= ["specials","specials_size",item_selections["specials"]] else: items['specials']=["specials",item_selections["specials"]] else: items['specials']=[] if "base" in item_selections: if "base_size" in item_selections: items['base']= ["base","base_size",item_selections["base"]] else: items['base']=["base",item_selections["base"]] else: items['base'] = [] if "items" in item_selections: toppings = [int(i) for i in item_selections.getlist("items")] if "items_size" in item_selections: selection =[int(i) for i in item_selections.getlist("items_size")] items['toppings']= [toppings,selection] else: items['toppings']=[toppings] else: items['toppings']=[] if Order.objects.last() == None: od = Order(1,*order) else: od = Order(Order.objects.last().id+1,*order) if Cart.objects.all().filter(order_status=False,user=str(request.user)).last() == None: if Cart.objects.last() == None: cart = Cart(1,user = str(request.user)) else: cart = Cart(Cart.objects.last().id + 1,user = str(request.user)) cart.save() else: cart = Cart.objects.all().filter(order_status=False,user = str(request.user)).first() od.order_total = round(get_price(items,RegularPrice),2) od.save() cart.stuff.add(od) cart.Total += od.order_total cart.save() return HttpResponse("r-updated!")