def add_to_draft_old(request): url_referer = request.META.get('HTTP_REFERER', '/') cart = Cart(request) cartItem = cart.count() totalprice = cart.summary() args = dict(cart=Cart(request)) args['cartItem'] = cartItem args['cartTotal'] = totalprice args['username'] = auth.get_user(request).username if request.method == 'POST': if request.user.is_authenticated(): draft = get_object_or_404(Draft, pk=int(request.POST['draft']), accounts_id=auth.get_user(request).id) itemD = ItemD.objects.filter(draft=draft) for item in cart: flagItem = 0 for draft_item in itemD: if item.product == draft_item.product and item.stock.pk == draft_item.stock.pk and item.brand == draft_item.brand: draft_item.quantity += item.quantity draft_item.save() flagItem = 1 if flagItem != 1: itemd = ItemD() itemd.draft = draft itemd.product = item.product itemd.stock = Stock.objects.get(pk=item.stock.pk) itemd.quantity = item.quantity itemd.unit_price = item.unit_price itemd.brand = item.brand itemd.save() cart.clear() cartItem = cart.count() totalprice = cart.summary() args['cartItem'] = cartItem args['cartTotal'] = totalprice return redirect('/lk/drafts/', "draft/draft.html", args) else: return render(request, "cart/cart.html", args) else: return render(request, "cart/cart.html", args)
def add_to_draft(request): url_referer = request.META.get('HTTP_REFERER', '/') cart = Cart(request) cartItem = cart.count() totalprice = cart.summary() args = dict(cart=Cart(request)) args['cartItem'] = cartItem args['cartTotal'] = totalprice args['username'] = auth.get_user(request).username if request.method == 'POST': if len(request.POST['name_draft'] ) > 2 and request.user.is_authenticated(): draft = Draft() draft.name = request.POST['name_draft'] draft.creation_date = datetime.datetime.now() draft.accounts = Accounts.objects.get( username=auth.get_user(request)) draft.save() for item in cart: itemd = ItemD() itemd.draft = draft itemd.product = item.product itemd.stock = Stock.objects.get(pk=item.stock.pk) itemd.quantity = item.quantity itemd.unit_price = item.unit_price itemd.brand = item.brand itemd.save() cart.clear() cartItem = cart.count() totalprice = cart.summary() args['cartItem'] = cartItem args['cartTotal'] = totalprice return redirect('/lk/drafts/', "draft/draft.html", args) else: return redirect('/cart/', "cart/cart.html", args) else: return render(request, "cart/cart.html", args)
def order( request ): # периписать все как логин все!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! url_referer = request.META.get('HTTP_REFERER', '/') cart = Cart(request) cartItem = cart.count() totalprice = cart.summary() args = dict(cart=Cart(request)) args['cartItem'] = cartItem args['cartTotal'] = totalprice if request.method == 'GET': if request.user.is_authenticated(): #if user is not None and user.is_active: # Правильный пароль и пользователь "активен" args['f_name'] = request.user.first_name args['l_name'] = request.user.last_name args['phone'] = request.user.accounts.telephone args['e_mail'] = request.user.email args['username'] = auth.get_user(request).username #auth.login(request, user) # Перенаправление на "правильную" страницу return render(request, "orders/order.html", args) else: # Отображение страницы с ошибкой args['login_errors'] = 'Пользователь не найден!' args['username'] = auth.get_user(request).username return redirect('%s?next=%s' % ('/lk/login/', request.path), args) #return render(request,"lk/login.html",args) else: '''-------------функция сохранения заказа-------------- Сделать проверку формы!!! Сделать страницу с редиректом после оформления заказа Готово--- Сделать отправку письма с заказом на почту клиенту !!!Функция рабочая!!! ''' fError = 0 args['f_name'] = request.POST['f_name'] args['l_name'] = request.POST['l_name'] args['phone'] = request.POST['telephone'] args['e_mail'] = request.POST['e_mail'] args['username'] = auth.get_user(request).username args['address'] = request.POST['address'] args['commentorder'] = request.POST['commentorder'] if len(request.POST['f_name']) < 2: args['f_name_errors'] = 'Заполните поле "Имя"' fError = 1 if len(request.POST['address']) < 10: args['address_errors'] = 'Заполните поле "Адрес"' fError = 1 if len(request.POST['e_mail']) < 4: args['e_mail_errors'] = 'Заполните поле "E-mail"' fError = 1 elif '@' not in request.POST['e_mail']: args['e_mail_errors'] = 'Указанный вами "E-mail" некорректен' fError = 1 else: pass if len(request.POST['telephone']) < 2: args['phone'] = 'Заполните поле "Телефон"' fError = 1 if fError == 1: return render(request, "orders/order.html", args) order = Order() order.status = OrderStatus.objects.get(pk=1) order.accounts = Accounts.objects.get(username=auth.get_user(request)) order.name = auth.get_user(request).username order.address = request.POST['address'] order.comment = request.POST['commentorder'] order.save() cart = Cart(request) for item in cart: orderP = OrderPosition() orderP.order = order orderP.product = item.product orderP.id_stock = Stock.objects.get(pk=item.stock.pk) orderP.count = item.quantity # переписать на поиск цены по Бренду orderP.price_purchase = Price.objects.get( p_product=item.product, id_stock=item.stock.pk).purchase_price orderP.price_sale = item.unit_price orderP.brand = item.brand orderP.save() orderPositions = OrderPosition.objects.filter(order=order) order_mail_sus(request.user.first_name, request.POST['e_mail'], order.pk, orderPositions, order.address, order.comment) cart.clear() cartItem = cart.count() totalprice = cart.summary() request.session['id_order'] = order.number_order request.session.save() balance_order(request, order) return redirect( '/order_finish/', { 'username': auth.get_user(request).username, 'cartItem': cartItem, 'cartTotal': totalprice })