|
|
|
|
@ -2,14 +2,16 @@ import datetime |
|
|
|
|
import logging |
|
|
|
|
import uuid |
|
|
|
|
from functools import reduce |
|
|
|
|
|
|
|
|
|
from decimal import Decimal |
|
|
|
|
|
|
|
|
|
from django.conf import settings |
|
|
|
|
from django.db import transaction |
|
|
|
|
from django.http import HttpResponseRedirect |
|
|
|
|
from django.shortcuts import redirect |
|
|
|
|
from django.urls import reverse_lazy |
|
|
|
|
from django.utils.translation import ugettext_lazy as _ |
|
|
|
|
from django.views.generic import ListView |
|
|
|
|
from django.contrib import messages |
|
|
|
|
|
|
|
|
|
from cart.models import Buying, Discount, Offer |
|
|
|
|
from core.models import City |
|
|
|
|
@ -35,6 +37,12 @@ class CartAddView(ProtectedBaseFormView): |
|
|
|
|
self.request.cart.add(offer_id=form.cleaned_data['offer'].product_id, quantity=form.cleaned_data['amount']) |
|
|
|
|
return super().form_valid(form) |
|
|
|
|
|
|
|
|
|
def form_invalid(self, form): |
|
|
|
|
if form.errors: |
|
|
|
|
for error in form.errors: |
|
|
|
|
messages.error(self.request, error) |
|
|
|
|
return HttpResponseRedirect(self.get_success_url()) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class CartRemoveView(ProtectedBaseFormView): |
|
|
|
|
http_method_names = ('post',) |
|
|
|
|
@ -79,7 +87,8 @@ class CartView(ProtectedListView): |
|
|
|
|
context = super().get_context_data(object_list=object_list, **kwargs) |
|
|
|
|
context['title'] = self.title |
|
|
|
|
context['total_price'] = self.get_total_price(self.object_list) |
|
|
|
|
context['total_price_currency'] = context['total_cashback_currency'] = self.object_list.first().currency.sign if self.object_list.first() else '' |
|
|
|
|
context['total_price_currency'] = context[ |
|
|
|
|
'total_cashback_currency'] = self.object_list.first().currency.sign if self.object_list.first() else '' |
|
|
|
|
context['total_cashback'] = self.get_total_cashback(self.object_list) |
|
|
|
|
return context |
|
|
|
|
|
|
|
|
|
@ -108,11 +117,27 @@ class CartConfirmView(ProtectedFormView): |
|
|
|
|
form_class = CartCheckoutForm |
|
|
|
|
title = _('Подтверждение заказа') |
|
|
|
|
|
|
|
|
|
def get_total_price(self, offers): |
|
|
|
|
return reduce( |
|
|
|
|
lambda initial, offer: initial + offer.price * Decimal(self.request.cart[offer.product_id]['quantity']), |
|
|
|
|
offers, |
|
|
|
|
Decimal(0) |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
def get_form_kwargs(self): |
|
|
|
|
kwargs = super().get_form_kwargs() |
|
|
|
|
data = kwargs['data'].copy() |
|
|
|
|
data.update({ |
|
|
|
|
'total_price': self.get_total_price(Offer.active.filter(product_id__in=self.request.cart.keys())) |
|
|
|
|
}) |
|
|
|
|
kwargs['data'] = data |
|
|
|
|
return kwargs |
|
|
|
|
|
|
|
|
|
def form_valid(self, form): |
|
|
|
|
if form.is_valid(): |
|
|
|
|
try: |
|
|
|
|
with transaction.atomic(): |
|
|
|
|
form.save() |
|
|
|
|
form.save(self.request.user) |
|
|
|
|
for item in self.request.cart: |
|
|
|
|
buying_form = CartCheckoutBuyingForm(initial={ |
|
|
|
|
'offer': item, |
|
|
|
|
@ -123,10 +148,16 @@ class CartConfirmView(ProtectedFormView): |
|
|
|
|
form.send_order_invoice(self.request) |
|
|
|
|
form.send_order_request(self.request) |
|
|
|
|
self.request.cart.clear() |
|
|
|
|
return self.render_to_response(self.get_context_data()) |
|
|
|
|
except Exception as e: |
|
|
|
|
logger.critical(e) |
|
|
|
|
|
|
|
|
|
return super().form_invalid(form) |
|
|
|
|
return self.form_invalid(form) |
|
|
|
|
|
|
|
|
|
def form_invalid(self, form): |
|
|
|
|
for error in form.errors: |
|
|
|
|
messages.error(self.request, error) |
|
|
|
|
return HttpResponseRedirect(self.request.META['HTTP_REFERER']) |
|
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs): |
|
|
|
|
context = super().get_context_data(**kwargs) |
|
|
|
|
|