<s> from __future__ import unicode_literals <EOL> from six import with_metaclass <EOL> from collections import OrderedDict <EOL> from django . db import models <EOL> from django . core . exceptions import ImproperlyConfigured <EOL> from django . utils . translation import ugettext_lazy as _ <EOL> from jsonfield . fields import JSONField <EOL> from shop . modifiers . pool import cart_modifiers_pool <EOL> from shop . money import Money <EOL> from . product import BaseProduct <EOL> from . import deferred <EOL> from shop . models . customer import CustomerModel <EOL> class CartItemManager ( models . Manager ) : <EOL> """<STR_LIT>""" <EOL> def get_or_create ( self , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> cart = kwargs . pop ( '<STR_LIT>' ) <EOL> product = kwargs . pop ( '<STR_LIT>' ) <EOL> quantity = int ( kwargs . pop ( '<STR_LIT>' , <NUM_LIT:1> ) ) <EOL> watched = not quantity <EOL> cart_item = product . is_in_cart ( cart , watched = watched , ** kwargs ) <EOL> if cart_item : <EOL> if not watched : <EOL> cart_item . quantity += quantity <EOL> created = False <EOL> else : <EOL> cart_item = self . model ( cart = cart , product = product , quantity = quantity , ** kwargs ) <EOL> created = True <EOL> cart_item . save ( ) <EOL> return cart_item , created <EOL> def filter_cart_items ( self , cart , request ) : <EOL> """<STR_LIT>""" <EOL> cart_items = self . filter ( cart = cart , quantity__gt = <NUM_LIT:0> ) <EOL> for modifier in cart_modifiers_pool . get_all_modifiers ( ) : <EOL> cart_items = modifier . arrange_cart_items ( cart_items , request ) <EOL> return cart_items <EOL> def filter_watch_items ( self , cart , request ) : <EOL> """<STR_LIT>""" <EOL> watch_items = self . filter ( cart = cart , quantity = <NUM_LIT:0> ) <EOL> for modifier in cart_modifiers_pool . get_all_modifiers ( ) : <EOL> watch_items = modifier . arrange_watch_items ( watch_items , request ) <EOL> return watch_items <EOL> class BaseCartItem ( with_metaclass ( deferred . ForeignKeyBuilder , models . Model ) ) : <EOL> """<STR_LIT>""" <EOL> cart = deferred . ForeignKey ( '<STR_LIT>' , related_name = '<STR_LIT>' ) <EOL> product = deferred . ForeignKey ( BaseProduct ) <EOL> extra = JSONField ( default = { } , verbose_name = _ ( "<STR_LIT>" ) ) <EOL> objects = CartItemManager ( ) <EOL> class Meta : <EOL> abstract = True <EOL> verbose_name = _ ( "<STR_LIT>" ) <EOL> verbose_name_plural = _ ( "<STR_LIT>" ) <EOL> @ classmethod <EOL> def perform_model_checks ( cls ) : <EOL> try : <EOL> allowed_types = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> field = [ f for f in cls . _meta . fields if f . attname == '<STR_LIT>' ] [ <NUM_LIT:0> ] <EOL> if not field . get_internal_type ( ) in allowed_types : <EOL> msg = "<STR_LIT>" <EOL> raise ImproperlyConfigured ( msg . format ( cls . __name__ , allowed_types ) ) <EOL> except IndexError : <EOL> msg = "<STR_LIT>" <EOL> raise ImproperlyConfigured ( msg . format ( cls . __name__ ) ) <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> all_field_names = self . _meta . get_all_field_names ( ) <EOL> model_kwargs = { k : v for k , v in kwargs . items ( ) if k in all_field_names } <EOL> super ( BaseCartItem , self ) . __init__ ( * args , ** model_kwargs ) <EOL> self . extra_rows = OrderedDict ( ) <EOL> self . _dirty = True <EOL> def save ( self , * args , ** kwargs ) : <EOL> super ( BaseCartItem , self ) . save ( * args , ** kwargs ) <EOL> self . _dirty = True <EOL> self . cart . _dirty = True <EOL> def update ( self , request ) : <EOL> """<STR_LIT>""" <EOL> if not self . _dirty : <EOL> return <EOL> self . extra_rows = OrderedDict ( ) <EOL> for modifier in cart_modifiers_pool . get_all_modifiers ( ) : <EOL> modifier . process_cart_item ( self , request ) <EOL> self . _dirty = False <EOL> CartItemModel = deferred . MaterializedModel ( BaseCartItem ) <EOL> class CartManager ( models . Manager ) : <EOL> """<STR_LIT>""" <EOL> def get_from_request ( self , request ) : <EOL> """<STR_LIT>""" <EOL> if request . customer . is_visitor ( ) : <EOL> raise self . model . DoesNotExist ( "<STR_LIT>" ) <EOL> cart , temp = self . get_or_create ( customer = request . customer ) <EOL> return cart <EOL> def get_or_create_from_request ( self , request ) : <EOL> if request . customer . is_visitor ( ) : <EOL> request . customer = CustomerModel . objects . get_or_create_from_request ( request ) <EOL> cart , temp = self . get_or_create ( customer = request . customer ) <EOL> return cart <EOL> class BaseCart ( with_metaclass ( deferred . ForeignKeyBuilder , models . Model ) ) : <EOL> """<STR_LIT>""" <EOL> customer = deferred . OneToOneField ( '<STR_LIT>' , verbose_name = _ ( "<STR_LIT>" ) , related_name = '<STR_LIT>' ) <EOL> created_at = models . DateTimeField ( auto_now_add = True , verbose_name = _ ( "<STR_LIT>" ) ) <EOL> updated_at = models . DateTimeField ( auto_now = True , verbose_name = _ ( "<STR_LIT>" ) ) <EOL> extra = JSONField ( default = { } , verbose_name = _ ( "<STR_LIT>" ) ) <EOL> objects = CartManager ( ) <EOL> class Meta : <EOL> abstract = True <EOL> verbose_name = _ ( "<STR_LIT>" ) <EOL> verbose_name_plural = _ ( "<STR_LIT>" ) <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( BaseCart , self ) . __init__ ( * args , ** kwargs ) <EOL> self . extra_rows = OrderedDict ( ) <EOL> self . _cached_cart_items = None <EOL> self . _dirty = True <EOL> def save ( self , * args , ** kwargs ) : <EOL> super ( BaseCart , self ) . save ( * args , ** kwargs ) <EOL> self . _dirty = True <EOL> def update ( self , request ) : <EOL> """<STR_LIT>""" <EOL> if not self . _dirty : <EOL> return <EOL> if self . _cached_cart_items : <EOL> items = self . _cached_cart_items <EOL> else : <EOL> items = CartItemModel . objects . filter_cart_items ( self , request ) <EOL> for modifier in cart_modifiers_pool . get_all_modifiers ( ) : <EOL> modifier . pre_process_cart ( self , request ) <EOL> for item in items : <EOL> modifier . pre_process_cart_item ( self , item , request ) <EOL> self . extra_rows = OrderedDict ( ) <EOL> self . subtotal = <NUM_LIT:0> <EOL> for item in items : <EOL> item . update ( request ) <EOL> self . subtotal += item . line_total <EOL> for modifier in cart_modifiers_pool . get_all_modifiers ( ) : <EOL> modifier . process_cart ( self , request ) <EOL> for modifier in reversed ( cart_modifiers_pool . get_all_modifiers ( ) ) : <EOL> modifier . post_process_cart ( self , request ) <EOL> self . _cached_cart_items = items <EOL> self . _dirty = False <EOL> def empty ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . pk : <EOL> self . items . all ( ) . delete ( ) <EOL> self . delete ( ) <EOL> def __str__ ( self ) : <EOL> return "<STR_LIT:{}>" . format ( self . pk ) or '<STR_LIT>' <EOL> @ property <EOL> def num_items ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . items . filter ( quantity__gt = <NUM_LIT:0> ) . count ( ) <EOL> @ property <EOL> def total_quantity ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . items . aggregate ( models . Sum ( '<STR_LIT>' ) ) [ '<STR_LIT>' ] <EOL> @ property <EOL> def is_empty ( self ) : <EOL> return self . total_quantity == <NUM_LIT:0> <EOL> def get_caption_data ( self ) : <EOL> return { '<STR_LIT>' : self . num_items , '<STR_LIT>' : self . total_quantity , <EOL> '<STR_LIT>' : self . subtotal , '<STR_LIT>' : self . total } <EOL> @ classmethod <EOL> def get_default_caption_data ( cls ) : <EOL> return { '<STR_LIT>' : <NUM_LIT:0> , '<STR_LIT>' : <NUM_LIT:0> , '<STR_LIT>' : Money ( ) , '<STR_LIT>' : Money ( ) } <EOL> CartModel = deferred . MaterializedModel ( BaseCart ) </s>
