<s> """<STR_LIT>""" <EOL> from django . core . exceptions import ObjectDoesNotExist <EOL> from django . db import connection <EOL> from django . db . models import signals <EOL> from django . db import models <EOL> from django . db . models . fields . related import RelatedField , Field , ManyToManyRel <EOL> from django . db . models . loading import get_model <EOL> from django . forms import ModelForm <EOL> from django . forms . models import BaseModelFormSet , modelformset_factory , save_instance <EOL> from django . contrib . admin . options import InlineModelAdmin , flatten_fieldsets <EOL> from django . utils . encoding import smart_unicode <EOL> class GenericForeignKey ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , ct_field = "<STR_LIT>" , fk_field = "<STR_LIT>" ) : <EOL> self . ct_field = ct_field <EOL> self . fk_field = fk_field <EOL> def contribute_to_class ( self , cls , name ) : <EOL> self . name = name <EOL> self . model = cls <EOL> self . cache_attr = "<STR_LIT>" % name <EOL> cls . _meta . add_virtual_field ( self ) <EOL> signals . pre_init . connect ( self . instance_pre_init , sender = cls , weak = False ) <EOL> setattr ( cls , name , self ) <EOL> def instance_pre_init ( self , signal , sender , args , kwargs , ** _kwargs ) : <EOL> """<STR_LIT>""" <EOL> if self . name in kwargs : <EOL> value = kwargs . pop ( self . name ) <EOL> kwargs [ self . ct_field ] = self . get_content_type ( obj = value ) <EOL> kwargs [ self . fk_field ] = value . _get_pk_val ( ) <EOL> def get_content_type ( self , obj = None , id = None ) : <EOL> ContentType = get_model ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> if obj : <EOL> return ContentType . objects . get_for_model ( obj ) <EOL> elif id : <EOL> return ContentType . objects . get_for_id ( id ) <EOL> else : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> def __get__ ( self , instance , instance_type = None ) : <EOL> if instance is None : <EOL> raise AttributeError , u"<STR_LIT>" % self . name <EOL> try : <EOL> return getattr ( instance , self . cache_attr ) <EOL> except AttributeError : <EOL> rel_obj = None <EOL> f = self . model . _meta . get_field ( self . ct_field ) <EOL> ct_id = getattr ( instance , f . get_attname ( ) , None ) <EOL> if ct_id : <EOL> ct = self . get_content_type ( id = ct_id ) <EOL> try : <EOL> rel_obj = ct . get_object_for_this_type ( pk = getattr ( instance , self . fk_field ) ) <EOL> except ObjectDoesNotExist : <EOL> pass <EOL> setattr ( instance , self . cache_attr , rel_obj ) <EOL> return rel_obj <EOL> def __set__ ( self , instance , value ) : <EOL> if instance is None : <EOL> raise AttributeError , u"<STR_LIT>" % self . related . opts . object_name <EOL> ct = None <EOL> fk = None <EOL> if value is not None : <EOL> ct = self . get_content_type ( obj = value ) <EOL> fk = value . _get_pk_val ( ) <EOL> setattr ( instance , self . ct_field , ct ) <EOL> setattr ( instance , self . fk_field , fk ) <EOL> setattr ( instance , self . cache_attr , value ) <EOL> class GenericRelation ( RelatedField , Field ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , to , ** kwargs ) : <EOL> kwargs [ '<STR_LIT>' ] = kwargs . get ( '<STR_LIT>' , None ) <EOL> kwargs [ '<STR_LIT>' ] = GenericRel ( to , <EOL> related_name = kwargs . pop ( '<STR_LIT:related_name>' , None ) , <EOL> limit_choices_to = kwargs . pop ( '<STR_LIT>' , None ) , <EOL> symmetrical = kwargs . pop ( '<STR_LIT>' , True ) ) <EOL> self . creates_table = False <EOL> self . object_id_field_name = kwargs . pop ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> self . content_type_field_name = kwargs . pop ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> kwargs [ '<STR_LIT:blank>' ] = True <EOL> kwargs [ '<STR_LIT>' ] = False <EOL> kwargs [ '<STR_LIT>' ] = False <EOL> Field . __init__ ( self , ** kwargs ) <EOL> def get_choices_default ( self ) : <EOL> return Field . get_choices ( self , include_blank = False ) <EOL> def value_to_string ( self , obj ) : <EOL> qs = getattr ( obj , self . name ) . all ( ) <EOL> return smart_unicode ( [ instance . _get_pk_val ( ) for instance in qs ] ) <EOL> def m2m_db_table ( self ) : <EOL> return self . rel . to . _meta . db_table <EOL> def m2m_column_name ( self ) : <EOL> return self . object_id_field_name <EOL> def m2m_reverse_name ( self ) : <EOL> return self . model . _meta . pk . column <EOL> def contribute_to_class ( self , cls , name ) : <EOL> super ( GenericRelation , self ) . contribute_to_class ( cls , name ) <EOL> self . model = cls <EOL> setattr ( cls , self . name , ReverseGenericRelatedObjectsDescriptor ( self ) ) <EOL> def contribute_to_related_class ( self , cls , related ) : <EOL> pass <EOL> def set_attributes_from_rel ( self ) : <EOL> pass <EOL> def get_internal_type ( self ) : <EOL> return "<STR_LIT>" <EOL> def db_type ( self ) : <EOL> return None <EOL> def extra_filters ( self , pieces , pos , negate ) : <EOL> """<STR_LIT>""" <EOL> if negate : <EOL> return [ ] <EOL> ContentType = get_model ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> content_type = ContentType . objects . get_for_model ( self . model ) <EOL> prefix = "<STR_LIT>" . join ( pieces [ : pos + <NUM_LIT:1> ] ) <EOL> return [ ( "<STR_LIT>" % ( prefix , self . content_type_field_name ) , <EOL> content_type ) ] <EOL> class ReverseGenericRelatedObjectsDescriptor ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , field ) : <EOL> self . field = field <EOL> def __get__ ( self , instance , instance_type = None ) : <EOL> if instance is None : <EOL> raise AttributeError , "<STR_LIT>" <EOL> from django . contrib . contenttypes . models import ContentType <EOL> rel_model = self . field . rel . to <EOL> superclass = rel_model . _default_manager . __class__ <EOL> RelatedManager = create_generic_related_manager ( superclass ) <EOL> qn = connection . ops . quote_name <EOL> manager = RelatedManager ( <EOL> model = rel_model , <EOL> instance = instance , <EOL> symmetrical = ( self . field . rel . symmetrical and instance . __class__ == rel_model ) , <EOL> join_table = qn ( self . field . m2m_db_table ( ) ) , <EOL> source_col_name = qn ( self . field . m2m_column_name ( ) ) , <EOL> target_col_name = qn ( self . field . m2m_reverse_name ( ) ) , <EOL> content_type = ContentType . objects . get_for_model ( self . field . model ) , <EOL> content_type_field_name = self . field . content_type_field_name , <EOL> object_id_field_name = self . field . object_id_field_name <EOL> ) <EOL> return manager <EOL> def __set__ ( self , instance , value ) : <EOL> if instance is None : <EOL> raise AttributeError , "<STR_LIT>" <EOL> manager = self . __get__ ( instance ) <EOL> manager . clear ( ) <EOL> for obj in value : <EOL> manager . add ( obj ) <EOL> def create_generic_related_manager ( superclass ) : <EOL> """<STR_LIT>""" <EOL> class GenericRelatedObjectManager ( superclass ) : <EOL> def __init__ ( self , model = None , core_filters = None , instance = None , symmetrical = None , <EOL> join_table = None , source_col_name = None , target_col_name = None , content_type = None , <EOL> content_type_field_name = None , object_id_field_name = None ) : <EOL> super ( GenericRelatedObjectManager , self ) . __init__ ( ) <EOL> self . core_filters = core_filters or { } <EOL> self . model = model <EOL> self . content_type = content_type <EOL> self . symmetrical = symmetrical <EOL> self . instance = instance <EOL> self . join_table = join_table <EOL> self . join_table = model . _meta . db_table <EOL> self . source_col_name = source_col_name <EOL> self . target_col_name = target_col_name <EOL> self . content_type_field_name = content_type_field_name <EOL> self . object_id_field_name = object_id_field_name <EOL> self . pk_val = self . instance . _get_pk_val ( ) <EOL> def get_query_set ( self ) : <EOL> query = { <EOL> '<STR_LIT>' % self . content_type_field_name : self . content_type . id , <EOL> '<STR_LIT>' % self . object_id_field_name : self . pk_val , <EOL> } <EOL> return superclass . get_query_set ( self ) . filter ( ** query ) <EOL> def add ( self , * objs ) : <EOL> for obj in objs : <EOL> setattr ( obj , self . content_type_field_name , self . content_type ) <EOL> setattr ( obj , self . object_id_field_name , self . pk_val ) <EOL> obj . save ( ) <EOL> add . alters_data = True <EOL> def remove ( self , * objs ) : <EOL> for obj in objs : <EOL> obj . delete ( ) <EOL> remove . alters_data = True <EOL> def clear ( self ) : <EOL> for obj in self . all ( ) : <EOL> obj . delete ( ) <EOL> clear . alters_data = True <EOL> def create ( self , ** kwargs ) : <EOL> kwargs [ self . content_type_field_name ] = self . content_type <EOL> kwargs [ self . object_id_field_name ] = self . pk_val <EOL> return super ( GenericRelatedObjectManager , self ) . create ( ** kwargs ) <EOL> create . alters_data = True <EOL> return GenericRelatedObjectManager <EOL> class GenericRel ( ManyToManyRel ) : <EOL> def __init__ ( self , to , related_name = None , limit_choices_to = None , symmetrical = True ) : <EOL> self . to = to <EOL> self . related_name = related_name <EOL> self . limit_choices_to = limit_choices_to or { } <EOL> self . symmetrical = symmetrical <EOL> self . multiple = True <EOL> class BaseGenericInlineFormSet ( BaseModelFormSet ) : <EOL> """<STR_LIT>""" <EOL> ct_field_name = "<STR_LIT>" <EOL> ct_fk_field_name = "<STR_LIT>" <EOL> def __init__ ( self , data = None , files = None , instance = None , save_as_new = None ) : <EOL> opts = self . model . _meta <EOL> self . instance = instance <EOL> self . rel_name = '<STR_LIT:->' . join ( ( <EOL> opts . app_label , opts . object_name . lower ( ) , <EOL> self . ct_field . name , self . ct_fk_field . name , <EOL> ) ) <EOL> super ( BaseGenericInlineFormSet , self ) . __init__ ( <EOL> queryset = self . get_queryset ( ) , data = data , files = files , <EOL> prefix = self . rel_name <EOL> ) <EOL> def get_queryset ( self ) : <EOL> from django . contrib . contenttypes . models import ContentType <EOL> if self . instance is None : <EOL> return self . model . _default_manager . empty ( ) <EOL> return self . model . _default_manager . filter ( ** { <EOL> self . ct_field . name : ContentType . objects . get_for_model ( self . instance ) , <EOL> self . ct_fk_field . name : self . instance . pk , <EOL> } ) <EOL> def save_new ( self , form , commit = True ) : <EOL> from django . contrib . contenttypes . models import ContentType <EOL> kwargs = { <EOL> self . ct_field . get_attname ( ) : ContentType . objects . get_for_model ( self . instance ) . pk , <EOL> self . ct_fk_field . get_attname ( ) : self . instance . pk , <EOL> } <EOL> new_obj = self . model ( ** kwargs ) <EOL> return save_instance ( form , new_obj , commit = commit ) <EOL> def generic_inlineformset_factory ( model , form = ModelForm , <EOL> formset = BaseGenericInlineFormSet , <EOL> ct_field = "<STR_LIT>" , fk_field = "<STR_LIT>" , <EOL> fields = None , exclude = None , <EOL> extra = <NUM_LIT:3> , can_order = False , can_delete = True , <EOL> max_num = <NUM_LIT:0> , <EOL> formfield_callback = lambda f : f . formfield ( ) ) : <EOL> """<STR_LIT>""" <EOL> opts = model . _meta <EOL> from django . contrib . contenttypes . models import ContentType <EOL> ct_field = opts . get_field ( ct_field ) <EOL> if not isinstance ( ct_field , models . ForeignKey ) or ct_field . rel . to != ContentType : <EOL> raise Exception ( "<STR_LIT>" % ct_field ) <EOL> fk_field = opts . get_field ( fk_field ) <EOL> if exclude is not None : <EOL> exclude . extend ( [ ct_field . name , fk_field . name ] ) <EOL> else : <EOL> exclude = [ ct_field . name , fk_field . name ] <EOL> FormSet = modelformset_factory ( model , form = form , <EOL> formfield_callback = formfield_callback , <EOL> formset = formset , <EOL> extra = extra , can_delete = can_delete , can_order = can_order , <EOL> fields = fields , exclude = exclude , max_num = max_num ) <EOL> FormSet . ct_field = ct_field <EOL> FormSet . ct_fk_field = fk_field <EOL> return FormSet <EOL> class GenericInlineModelAdmin ( InlineModelAdmin ) : <EOL> ct_field = "<STR_LIT>" <EOL> ct_fk_field = "<STR_LIT>" <EOL> formset = BaseGenericInlineFormSet <EOL> def get_formset ( self , request , obj = None ) : <EOL> if self . declared_fieldsets : <EOL> fields = flatten_fieldsets ( self . declared_fieldsets ) <EOL> else : <EOL> fields = None <EOL> defaults = { <EOL> "<STR_LIT>" : self . ct_field , <EOL> "<STR_LIT>" : self . ct_fk_field , <EOL> "<STR_LIT>" : self . form , <EOL> "<STR_LIT>" : self . formfield_for_dbfield , <EOL> "<STR_LIT>" : self . formset , <EOL> "<STR_LIT>" : self . extra , <EOL> "<STR_LIT>" : True , <EOL> "<STR_LIT>" : False , <EOL> "<STR_LIT>" : fields , <EOL> } <EOL> return generic_inlineformset_factory ( self . model , ** defaults ) <EOL> class GenericStackedInline ( GenericInlineModelAdmin ) : <EOL> template = '<STR_LIT>' <EOL> class GenericTabularInline ( GenericInlineModelAdmin ) : <EOL> template = '<STR_LIT>' </s>
