<s> from __future__ import unicode_literals <EOL> from datetime import datetime <EOL> from functools import reduce <EOL> import operator <EOL> from django . core . exceptions import ImproperlyConfigured <EOL> from django . db import models <EOL> from django . utils import six <EOL> from django . utils . encoding import python_2_unicode_compatible , force_text <EOL> from django . utils . translation import ugettext_lazy as _ <EOL> from polymorphic . manager import PolymorphicManager <EOL> from polymorphic . models import PolymorphicModel <EOL> from polymorphic . base import PolymorphicModelBase <EOL> from . import deferred <EOL> class BaseProductManager ( PolymorphicManager ) : <EOL> """<STR_LIT>""" <EOL> def select_lookup ( self , search_term ) : <EOL> """<STR_LIT>""" <EOL> filter_by_term = ( models . Q ( ( sf , search_term ) ) for sf in self . model . lookup_fields ) <EOL> queryset = self . get_queryset ( ) . filter ( reduce ( operator . or_ , filter_by_term ) ) <EOL> return queryset <EOL> def indexable ( self ) : <EOL> """<STR_LIT>""" <EOL> queryset = self . get_queryset ( ) . filter ( active = True ) <EOL> return queryset <EOL> class PolymorphicProductMetaclass ( PolymorphicModelBase ) : <EOL> """<STR_LIT>""" <EOL> def __new__ ( cls , name , bases , attrs ) : <EOL> Model = super ( PolymorphicProductMetaclass , cls ) . __new__ ( cls , name , bases , attrs ) <EOL> if Model . _meta . abstract : <EOL> return Model <EOL> for baseclass in bases : <EOL> if not isinstance ( baseclass , cls ) : <EOL> continue <EOL> try : <EOL> if issubclass ( baseclass . _materialized_model , Model ) : <EOL> baseclass . _materialized_model = Model <EOL> elif not issubclass ( Model , baseclass . _materialized_model ) : <EOL> raise ImproperlyConfigured ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> . format ( name , Model , baseclass . _materialized_model ) ) <EOL> except ( AttributeError , TypeError ) : <EOL> baseclass . _materialized_model = Model <EOL> deferred . ForeignKeyBuilder . process_pending_mappings ( Model , baseclass . __name__ ) <EOL> cls . perform_model_checks ( Model ) <EOL> return Model <EOL> @ classmethod <EOL> def perform_model_checks ( cls , Model ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( Model . objects , BaseProductManager ) : <EOL> msg = "<STR_LIT>" <EOL> raise NotImplementedError ( msg . format ( Model . __name__ ) ) <EOL> if not isinstance ( getattr ( Model , '<STR_LIT>' , None ) , ( list , tuple ) ) : <EOL> msg = "<STR_LIT>" <EOL> raise NotImplementedError ( msg . format ( Model . __name__ ) ) <EOL> try : <EOL> Model ( ) . product_name <EOL> except AttributeError : <EOL> msg = "<STR_LIT>" <EOL> raise NotImplementedError ( msg . format ( Model . __name__ ) ) <EOL> if not callable ( getattr ( Model , '<STR_LIT>' , None ) ) : <EOL> msg = "<STR_LIT>" <EOL> raise NotImplementedError ( msg . format ( cls . __name__ ) ) <EOL> @ python_2_unicode_compatible <EOL> class BaseProduct ( six . with_metaclass ( PolymorphicProductMetaclass , PolymorphicModel ) ) : <EOL> """<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> active = models . BooleanField ( default = True , verbose_name = _ ( "<STR_LIT>" ) , <EOL> help_text = _ ( "<STR_LIT>" ) ) <EOL> class Meta : <EOL> abstract = True <EOL> verbose_name = _ ( "<STR_LIT>" ) <EOL> verbose_name_plural = _ ( "<STR_LIT>" ) <EOL> def __str__ ( self ) : <EOL> return self . product_name <EOL> def product_type ( self ) : <EOL> """<STR_LIT>""" <EOL> return force_text ( self . polymorphic_ctype ) <EOL> product_type . short_description = _ ( "<STR_LIT>" ) <EOL> @ property <EOL> def product_model ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . polymorphic_ctype . model <EOL> def get_absolute_url ( self ) : <EOL> """<STR_LIT>""" <EOL> msg = "<STR_LIT>" <EOL> raise NotImplementedError ( msg . format ( self . __class__ . __name__ ) ) <EOL> def get_price ( self , request ) : <EOL> """<STR_LIT>""" <EOL> msg = "<STR_LIT>" <EOL> raise NotImplementedError ( msg . format ( self . __class__ . __name__ ) ) <EOL> def get_availability ( self , request ) : <EOL> """<STR_LIT>""" <EOL> return [ ( True , datetime . max ) ] <EOL> def is_in_cart ( self , cart , watched = False , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> from . cart import CartItemModel <EOL> cart_item_qs = CartItemModel . objects . filter ( cart = cart , product = self ) <EOL> return cart_item_qs . first ( ) <EOL> ProductModel = deferred . MaterializedModel ( BaseProduct ) </s>
