<s> from functools import partial <EOL> from django . contrib import admin <EOL> from django . contrib . admin import widgets <EOL> from django . utils . translation import ugettext_lazy as _ <EOL> from . models import AttributeSet , Value <EOL> from . forms import ProductAttributeFormFactory <EOL> class BaseProductAttributeMixin ( object ) : <EOL> def get_attribute_formfactory ( <EOL> self , <EOL> request , <EOL> factory , <EOL> obj = None , <EOL> prefix = None , <EOL> mixin = None , <EOL> ** kwargs <EOL> ) : <EOL> attribute_set = None <EOL> if obj : <EOL> if not hasattr ( obj , '<STR_LIT>' ) : <EOL> attribute_set = self . model . objects . get ( pk = obj . pk ) . attribute_set <EOL> obj . _attribute_set = attribute_set <EOL> else : <EOL> attribute_set = obj . _attribute_set <EOL> return factory ( <EOL> form = kwargs . get ( '<STR_LIT>' , None ) , <EOL> prefix = prefix , <EOL> mixin = mixin , <EOL> formfield_callback = partial ( <EOL> self . admin_attribute_formfield , <EOL> request = request <EOL> ) , <EOL> attribute_set = attribute_set <EOL> ) <EOL> def admin_attribute_formfield ( self , attribute , formfield , request ) : <EOL> typ = attribute . get_type ( ) <EOL> model = typ . get_model ( ) <EOL> if model : <EOL> remote_field = Value . _meta . get_field ( <EOL> typ . get_value_field_name ( ) <EOL> ) . rel <EOL> related_modeladmin = self . admin_site . _registry . get ( model ) <EOL> wrapper_kwargs = { } <EOL> if related_modeladmin : <EOL> wrapper_kwargs . update ( <EOL> can_add_related = related_modeladmin . has_add_permission ( <EOL> request <EOL> ) , <EOL> can_change_related = <EOL> related_modeladmin . has_change_permission ( request ) , <EOL> can_delete_related = <EOL> related_modeladmin . has_delete_permission ( request ) <EOL> ) <EOL> formfield . widget = widgets . RelatedFieldWidgetWrapper ( <EOL> formfield . widget , remote_field , self . admin_site , <EOL> ** wrapper_kwargs <EOL> ) <EOL> return formfield <EOL> class ProductAttributeMixin ( BaseProductAttributeMixin ) : <EOL> def get_form ( self , request , obj = None , ** kwargs ) : <EOL> kwargs [ '<STR_LIT>' ] = ( <EOL> self . get_attribute_formfactory ( <EOL> request , <EOL> ProductAttributeFormFactory , <EOL> prefix = '<STR_LIT>' , <EOL> obj = obj , <EOL> ** kwargs <EOL> ) . factory ( ) <EOL> ) <EOL> return ( <EOL> super ( ProductAttributeMixin , self ) . get_form ( <EOL> request , <EOL> obj = obj , <EOL> ** kwargs <EOL> ) <EOL> ) <EOL> def get_fieldsets ( self , request , obj = None ) : <EOL> fieldsets = super ( ProductAttributeMixin , self ) . get_fieldsets ( <EOL> request , obj <EOL> ) <EOL> fields = ( <EOL> self . get_attribute_formfactory ( <EOL> request , <EOL> ProductAttributeFormFactory , <EOL> prefix = '<STR_LIT>' , <EOL> obj = obj <EOL> ) . get_attribute_formfield_names ( ) <EOL> ) <EOL> fieldsets += ( ( _ ( "<STR_LIT>" ) , { '<STR_LIT>' : fields } ) , ) <EOL> return fieldsets <EOL> class BaseAttributeSetAdmin ( admin . ModelAdmin ) : <EOL> def get_queryset ( self , request ) : <EOL> return super ( BaseAttributeSetAdmin , <EOL> self ) . get_queryset ( request ) . flat_ordered ( ) <EOL> class AttributeSetParentListFilter ( admin . SimpleListFilter ) : <EOL> title = _ ( "<STR_LIT>" ) <EOL> parameter_name = '<STR_LIT>' <EOL> def lookups ( self , request , model_admin ) : <EOL> return [ <EOL> ( str ( sett . pk ) , unicode ( sett ) ) <EOL> for sett in AttributeSet . objects . all ( ) . flat_ordered ( ) <EOL> ] <EOL> def queryset ( self , request , queryset ) : <EOL> pk = self . value ( ) <EOL> if pk != None : <EOL> sett = AttributeSet . objects . get ( pk = pk ) <EOL> return queryset . in_parent ( sett ) <EOL> else : <EOL> return queryset . all ( ) </s>
