Skip to content

Commit

Permalink
Merge pull request #267 from patrys/feature/move-shipping-data-to-order
Browse files Browse the repository at this point in the history
Move shipping data to order
  • Loading branch information
mociepka committed May 15, 2015
2 parents a368c93 + a604156 commit 64ab3a9
Show file tree
Hide file tree
Showing 28 changed files with 433 additions and 626 deletions.
31 changes: 20 additions & 11 deletions saleor/cart/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,16 @@
from django.utils.encoding import python_2_unicode_compatible, smart_text
from prices import Price
from satchless import cart
from satchless.item import ItemList
from satchless.item import ItemList, partition
from saleor.product.models import Product


CART_SESSION_KEY = 'cart'

class DigitalGroup(ItemList):
'''
Group for digital products.
'''
pass
class ProductGroup(ItemList):

def is_shipping_required(self):
return any(p.is_shipping_required() for p in self)


class CartLine(cart.CartLine):
Expand All @@ -28,6 +27,9 @@ def get_price_per_item(self, **kwargs):
kwargs.setdefault('discounts', self.discounts)
return super(CartLine, self).get_price_per_item(**kwargs)

def is_shipping_required(self):
return self.product.is_shipping_required()


@python_2_unicode_compatible
class Cart(cart.Cart):
Expand All @@ -43,6 +45,11 @@ def __init__(self, session_cart, discounts=None):
self.session_cart = session_cart
self.discounts = discounts

def __str__(self):
return pgettext(
'Shopping cart',
'Your cart (%(cart_count)s)') % {'cart_count': self.count()}

@classmethod
def for_session_cart(cls, session_cart, discounts=None):
cart = Cart(session_cart, discounts=discounts)
Expand All @@ -63,11 +70,6 @@ def for_session_cart(cls, session_cart, discounts=None):
skip_session_cart=True)
return cart

def __str__(self):
return pgettext(
'Shopping cart',
'Your cart (%(cart_count)s)') % {'cart_count': self.count()}

def get_data_for_product(self, variant):
variant_price = variant.get_price_per_item(discounts=self.discounts)
variant_data = {
Expand All @@ -93,6 +95,13 @@ def clear(self):
def create_line(self, product, quantity, data):
return CartLine(product, quantity, data=data, discounts=self.discounts)

def is_shipping_required(self):
return any(line.is_shipping_required() for line in self)

def partition(self):
return partition(
self, lambda p: 'physical' if p.is_shipping_required() else 'digital',
ProductGroup)


class SessionCartLine(cart.CartLine):
Expand Down
3 changes: 1 addition & 2 deletions saleor/cart/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@
from django.shortcuts import redirect
from django.template.response import TemplateResponse
from django.utils.translation import ugettext as _
from satchless.item import Partitioner

from .forms import ReplaceCartLineFormSet
from . import Cart


def index(request):
cart = Cart.for_session_cart(request.cart, discounts=request.discounts)
cart_partitioner = Partitioner(cart)
cart_partitioner = cart.partition()
formset = ReplaceCartLineFormSet(request.POST or None,
cart=cart)
if formset.is_valid():
Expand Down
68 changes: 45 additions & 23 deletions saleor/checkout/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@
from django.conf import settings
from prices import Price
from satchless.process import ProcessManager
from satchless.item import Partitioner

from .steps import (BillingAddressStep, ShippingStep, DigitalDeliveryStep,
SummaryStep)
from ..cart import DigitalGroup, Cart
from .steps import BillingAddressStep, ShippingStep, SummaryStep
from ..cart import Cart
from ..core import analytics
from ..order.models import Order
from ..userprofile.models import Address
Expand Down Expand Up @@ -44,22 +42,21 @@ def __init__(self, request):
discounts=request.discounts)
self.generate_steps(self.cart)

def __iter__(self):
return iter(self.steps)

def generate_steps(self, cart):
self.items = Partitioner(cart)
self.cart = cart
self.billing = BillingAddressStep(
self.request, self.get_storage('billing'))
self.steps.append(self.billing)
for index, delivery_group in enumerate(self.items):
if isinstance(delivery_group, DigitalGroup):
storage = self.get_storage('digital_%s' % (index,))
step = DigitalDeliveryStep(
self.request, storage, delivery_group, _id=index)
else:
storage = self.get_storage('shipping_%s' % (index,))
step = ShippingStep(
self.request, storage, delivery_group, _id=index,
default_address=self.billing_address)
self.steps.append(step)
if self.is_shipping_required():
self.shipping = ShippingStep(
self.request, self.get_storage('shipping'),
self.cart, default_address=self.billing_address)
self.steps.append(self.shipping)
else:
self.shipping = None
summary_step = SummaryStep(
self.request, self.get_storage('summary'), checkout=self)
self.steps.append(summary_step)
Expand Down Expand Up @@ -95,14 +92,32 @@ def billing_address(self):
storage = self.get_storage('billing')
storage['address'] = None

@property
def shipping_address(self):
storage = self.get_storage('shipping')
address_data = storage.get('address', {})
return Address(**address_data)

@shipping_address.setter
def shipping_address(self, address):
storage = self.get_storage('shipping')
storage['address'] = address.as_data()

@shipping_address.deleter
def shipping_address(self):
storage = self.get_storage('shipping')
storage['address'] = None

def get_storage(self, name):
return self.storage[name]

def get_total(self, **kwargs):
zero = Price(0, currency=settings.DEFAULT_CURRENCY)
total = sum((step.group.get_total_with_delivery(**kwargs)
for step in self if step.group),
zero)
total = sum(
(total_with_delivery
for delivery, delivery_cost, total_with_delivery
in self.get_deliveries(**kwargs)),
zero)
return total

def save(self):
Expand All @@ -112,11 +127,18 @@ def clear_storage(self):
del self.request.session[STORAGE_SESSION_KEY]
self.cart.clear()

def __iter__(self):
return iter(self.steps)
def is_shipping_required(self):
return self.cart.is_shipping_required()

def delivery_steps(self):
return [step for step in self.steps if step.group]
def get_deliveries(self, **kwargs):
for partition in self.cart.partition():
if self.shipping:
delivery_cost = self.shipping.delivery_method.get_delivery_total(
partition)
else:
delivery_cost = Price(0, currency=settings.DEFAULT_CURRENCY)
total_with_delivery = partition.get_total(**kwargs) + delivery_cost
yield partition, delivery_cost, total_with_delivery

def create_order(self):
order = Order()
Expand Down
16 changes: 0 additions & 16 deletions saleor/checkout/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from django.contrib.auth.models import AnonymousUser
from django.utils.translation import ugettext_lazy as _

from ..order.models import DigitalDeliveryGroup
from ..userprofile.forms import AddressForm


Expand All @@ -11,21 +10,6 @@ class ShippingForm(AddressForm):
use_billing = forms.BooleanField(initial=True)


class DigitalDeliveryForm(forms.ModelForm):

class Meta:
model = DigitalDeliveryGroup
fields = ['email']

def __init__(self, *args, **kwargs):
user = kwargs.pop('user', None) or AnonymousUser()
super(DigitalDeliveryForm, self).__init__(*args, **kwargs)
email = self.fields['email']
email.required = True
if user.is_authenticated():
email.initial = user.email


class DeliveryForm(forms.Form):

method = forms.ChoiceField(label=_('Shipping method'))
Expand Down

0 comments on commit 64ab3a9

Please sign in to comment.