<s> from sellmo . utils . text import underscore_concat <EOL> from sellmo . contrib . attribute . helpers import ( <EOL> ProductAttributeHelper as _ProductAttributeHelper <EOL> ) <EOL> from sellmo . contrib . attribute . helpers import AttributeHelper <EOL> from django . db import models <EOL> from django . db . models import Q <EOL> from django . core . exceptions import ValidationError <EOL> from django . utils . functional import cached_property <EOL> from django . utils . translation import ugettext_lazy as _ <EOL> from django . utils . encoding import smart_text <EOL> from django . utils . text import capfirst <EOL> from django . utils import six <EOL> import sellmo . contrib . attribute as _attribute <EOL> def differs_field_name ( field_name ) : <EOL> return underscore_concat ( field_name , '<STR_LIT>' ) <EOL> class ProductAttributeHelper ( _ProductAttributeHelper ) : <EOL> def get_values_queryset ( self ) : <EOL> values = super ( ProductAttributeHelper , self ) . get_values_queryset ( ) <EOL> values = values . filter ( variates = False ) <EOL> return values <EOL> def get_new_value ( self ) : <EOL> value = super ( ProductAttributeHelper , self ) . get_new_value ( ) <EOL> value . variates = False <EOL> return value <EOL> class VariantAttributeHelper ( ProductAttributeHelper ) : <EOL> def get_values ( self ) : <EOL> variant_values = { <EOL> value . attribute . key : value <EOL> for value in super ( VariantAttributeHelper , self ) . get_values ( ) <EOL> } <EOL> product_values = { <EOL> value . attribute . key : value <EOL> for value in self . _product . product . attributes . get_values ( ) <EOL> } <EOL> return list ( six . itervalues ( dict ( product_values , ** variant_values ) ) ) <EOL> def get_value ( self , key ) : <EOL> value = super ( VariantAttributeHelper , self ) . get_value ( key ) <EOL> if value is None : <EOL> value = self . _product . product . attributes . get_value ( key ) <EOL> return value <EOL> def set_value_value ( self , key , value_value ) : <EOL> attribute = self . get_attribute ( key ) <EOL> product_value = self . _product . product . attributes . get_value ( key ) <EOL> variant_value = self . get_own_value ( key ) <EOL> if ( <EOL> not attribute . variates and product_value is not None and <EOL> product_value . value == value_value <EOL> ) : <EOL> if variant_value : <EOL> variant_value . value = None <EOL> else : <EOL> super ( VariantAttributeHelper , self ) . set_value_value ( <EOL> key , value_value <EOL> ) <EOL> class VariationAttributeHelper ( AttributeHelper ) : <EOL> def __init__ ( self , variation ) : <EOL> super ( VariationAttributeHelper , self ) . __init__ ( ) <EOL> self . _variation = variation <EOL> @ cached_property <EOL> def _product ( self ) : <EOL> if hasattr ( self . _variation , '<STR_LIT>' ) : <EOL> return self . _variation . product . downcast ( ) <EOL> def get_values ( self ) : <EOL> variation_values = { <EOL> value . attribute . key : value <EOL> for value in self . _variation . values . all ( ) <EOL> } <EOL> variant_values = { } <EOL> if self . _product : <EOL> variant_values = { <EOL> value . attribute . key : value <EOL> for value in self . _product . attributes . get_values ( ) <EOL> } <EOL> return list ( six . itervalues ( dict ( variant_values , ** variation_values ) ) ) <EOL> def get_value ( self , key ) : <EOL> attribute = self . get_attribute ( key ) <EOL> value = None <EOL> try : <EOL> value = self . _variation . values . get ( attribute = attribute ) <EOL> except _attribute . models . Value . DoesNotExist : <EOL> if self . _product : <EOL> value = self . _product . attributes . get_value ( key ) <EOL> return value </s>
