<s> from django . core . exceptions import ImproperlyConfigured <EOL> from django . db import models <EOL> from django . forms . models import ( BaseModelForm , BaseModelFormSet , fields_for_model , <EOL> _get_foreign_key ) <EOL> from django . contrib . admin . options import flatten_fieldsets , BaseModelAdmin <EOL> from django . contrib . admin . options import HORIZONTAL , VERTICAL <EOL> from django . contrib . admin . util import lookup_field <EOL> __all__ = [ '<STR_LIT>' ] <EOL> def validate ( cls , model ) : <EOL> """<STR_LIT>""" <EOL> models . get_apps ( ) <EOL> opts = model . _meta <EOL> validate_base ( cls , model ) <EOL> if hasattr ( cls , '<STR_LIT>' ) : <EOL> check_isseq ( cls , '<STR_LIT>' , cls . list_display ) <EOL> for idx , field in enumerate ( cls . list_display ) : <EOL> if not callable ( field ) : <EOL> if not hasattr ( cls , field ) : <EOL> if not hasattr ( model , field ) : <EOL> try : <EOL> opts . get_field ( field ) <EOL> except models . FieldDoesNotExist : <EOL> raise ImproperlyConfigured ( "<STR_LIT>" <EOL> % ( cls . __name__ , idx , field , cls . __name__ , model . _meta . object_name ) ) <EOL> else : <EOL> f = fetch_attr ( cls , model , opts , "<STR_LIT>" % idx , field ) <EOL> if isinstance ( f , models . ManyToManyField ) : <EOL> raise ImproperlyConfigured ( "<STR_LIT>" <EOL> % ( cls . __name__ , idx , field ) ) <EOL> if hasattr ( cls , '<STR_LIT>' ) : <EOL> check_isseq ( cls , '<STR_LIT>' , cls . list_display_links ) <EOL> for idx , field in enumerate ( cls . list_display_links ) : <EOL> fetch_attr ( cls , model , opts , '<STR_LIT>' % idx , field ) <EOL> if field not in cls . list_display : <EOL> raise ImproperlyConfigured ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> % ( cls . __name__ , idx , field ) ) <EOL> if hasattr ( cls , '<STR_LIT>' ) : <EOL> check_isseq ( cls , '<STR_LIT>' , cls . list_filter ) <EOL> for idx , field in enumerate ( cls . list_filter ) : <EOL> get_field ( cls , model , opts , '<STR_LIT>' % idx , field ) <EOL> if hasattr ( cls , '<STR_LIT>' ) and not isinstance ( cls . list_per_page , int ) : <EOL> raise ImproperlyConfigured ( "<STR_LIT>" <EOL> % cls . __name__ ) <EOL> if hasattr ( cls , '<STR_LIT>' ) and cls . list_editable : <EOL> check_isseq ( cls , '<STR_LIT>' , cls . list_editable ) <EOL> for idx , field_name in enumerate ( cls . list_editable ) : <EOL> try : <EOL> field = opts . get_field_by_name ( field_name ) [ <NUM_LIT:0> ] <EOL> except models . FieldDoesNotExist : <EOL> raise ImproperlyConfigured ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> % ( cls . __name__ , idx , field_name , model . __name__ ) ) <EOL> if field_name not in cls . list_display : <EOL> raise ImproperlyConfigured ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> % ( cls . __name__ , idx , field_name ) ) <EOL> if field_name in cls . list_display_links : <EOL> raise ImproperlyConfigured ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> % ( field_name , cls . __name__ , cls . __name__ ) ) <EOL> if not cls . list_display_links and cls . list_display [ <NUM_LIT:0> ] in cls . list_editable : <EOL> raise ImproperlyConfigured ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> % ( cls . __name__ , idx , cls . list_display [ <NUM_LIT:0> ] ) ) <EOL> if not field . editable : <EOL> raise ImproperlyConfigured ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> % ( cls . __name__ , idx , field_name ) ) <EOL> if hasattr ( cls , '<STR_LIT>' ) : <EOL> check_isseq ( cls , '<STR_LIT>' , cls . search_fields ) <EOL> if cls . date_hierarchy : <EOL> f = get_field ( cls , model , opts , '<STR_LIT>' , cls . date_hierarchy ) <EOL> if not isinstance ( f , ( models . DateField , models . DateTimeField ) ) : <EOL> raise ImproperlyConfigured ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> % cls . __name__ ) <EOL> if cls . ordering : <EOL> check_isseq ( cls , '<STR_LIT>' , cls . ordering ) <EOL> for idx , field in enumerate ( cls . ordering ) : <EOL> if field == '<STR_LIT:?>' and len ( cls . ordering ) != <NUM_LIT:1> : <EOL> raise ImproperlyConfigured ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> % cls . __name__ ) <EOL> if field == '<STR_LIT:?>' : <EOL> continue <EOL> if field . startswith ( '<STR_LIT:->' ) : <EOL> field = field [ <NUM_LIT:1> : ] <EOL> if '<STR_LIT>' in field : <EOL> continue <EOL> get_field ( cls , model , opts , '<STR_LIT>' % idx , field ) <EOL> if hasattr ( cls , "<STR_LIT>" ) : <EOL> check_isseq ( cls , "<STR_LIT>" , cls . readonly_fields ) <EOL> for idx , field in enumerate ( cls . readonly_fields ) : <EOL> if not callable ( field ) : <EOL> if not hasattr ( cls , field ) : <EOL> if not hasattr ( model , field ) : <EOL> try : <EOL> opts . get_field ( field ) <EOL> except models . FieldDoesNotExist : <EOL> raise ImproperlyConfigured ( "<STR_LIT>" <EOL> % ( cls . __name__ , idx , field , cls . __name__ , model . _meta . object_name ) ) <EOL> for attr in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> if not isinstance ( getattr ( cls , attr ) , bool ) : <EOL> raise ImproperlyConfigured ( "<STR_LIT>" <EOL> % ( cls . __name__ , attr ) ) <EOL> if hasattr ( cls , '<STR_LIT>' ) : <EOL> check_isseq ( cls , '<STR_LIT>' , cls . inlines ) <EOL> for idx , inline in enumerate ( cls . inlines ) : <EOL> if not issubclass ( inline , BaseModelAdmin ) : <EOL> raise ImproperlyConfigured ( "<STR_LIT>" <EOL> "<STR_LIT>" % ( cls . __name__ , idx ) ) <EOL> if not inline . model : <EOL> raise ImproperlyConfigured ( "<STR_LIT>" <EOL> "<STR_LIT>" % ( cls . __name__ , idx ) ) <EOL> if not issubclass ( inline . model , models . Model ) : <EOL> raise ImproperlyConfigured ( "<STR_LIT>" <EOL> "<STR_LIT>" % ( cls . __name__ , idx ) ) <EOL> validate_base ( inline , inline . model ) <EOL> validate_inline ( inline , cls , model ) <EOL> def validate_inline ( cls , parent , parent_model ) : <EOL> if cls . fk_name : <EOL> f = get_field ( cls , cls . model , cls . model . _meta , '<STR_LIT>' , cls . fk_name ) <EOL> if not isinstance ( f , models . ForeignKey ) : <EOL> raise ImproperlyConfigured ( "<STR_LIT>" <EOL> "<STR_LIT>" % cls . __name__ ) <EOL> fk = _get_foreign_key ( parent_model , cls . model , fk_name = cls . fk_name , can_fail = True ) <EOL> if not isinstance ( getattr ( cls , '<STR_LIT>' ) , int ) : <EOL> raise ImproperlyConfigured ( "<STR_LIT>" <EOL> % cls . __name__ ) <EOL> max_num = getattr ( cls , '<STR_LIT>' , None ) <EOL> if max_num is not None and not isinstance ( max_num , int ) : <EOL> raise ImproperlyConfigured ( "<STR_LIT>" <EOL> % cls . __name__ ) <EOL> if hasattr ( cls , '<STR_LIT>' ) and not issubclass ( cls . formset , BaseModelFormSet ) : <EOL> raise ImproperlyConfigured ( "<STR_LIT>" <EOL> "<STR_LIT>" % cls . __name__ ) <EOL> if hasattr ( cls , '<STR_LIT>' ) and cls . exclude : <EOL> if fk and fk . name in cls . exclude : <EOL> raise ImproperlyConfigured ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % ( cls . __name__ , fk . name , parent_model . __name__ ) ) <EOL> def validate_base ( cls , model ) : <EOL> opts = model . _meta <EOL> if hasattr ( cls , '<STR_LIT>' ) : <EOL> check_isseq ( cls , '<STR_LIT>' , cls . raw_id_fields ) <EOL> for idx , field in enumerate ( cls . raw_id_fields ) : <EOL> f = get_field ( cls , model , opts , '<STR_LIT>' , field ) <EOL> if not isinstance ( f , ( models . ForeignKey , models . ManyToManyField ) ) : <EOL> raise ImproperlyConfigured ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> % ( cls . __name__ , idx , field ) ) <EOL> if cls . fields : <EOL> check_isseq ( cls , '<STR_LIT>' , cls . fields ) <EOL> for field in cls . fields : <EOL> if field in cls . readonly_fields : <EOL> continue <EOL> check_formfield ( cls , model , opts , '<STR_LIT>' , field ) <EOL> try : <EOL> f = opts . get_field ( field ) <EOL> except models . FieldDoesNotExist : <EOL> continue <EOL> if isinstance ( f , models . ManyToManyField ) and not f . rel . through . _meta . auto_created : <EOL> raise ImproperlyConfigured ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % ( cls . __name__ , field , field ) ) <EOL> if cls . fieldsets : <EOL> raise ImproperlyConfigured ( '<STR_LIT>' % cls . __name__ ) <EOL> if len ( cls . fields ) > len ( set ( cls . fields ) ) : <EOL> raise ImproperlyConfigured ( '<STR_LIT>' % cls . __name__ ) <EOL> if cls . fieldsets : <EOL> check_isseq ( cls , '<STR_LIT>' , cls . fieldsets ) <EOL> for idx , fieldset in enumerate ( cls . fieldsets ) : <EOL> check_isseq ( cls , '<STR_LIT>' % idx , fieldset ) <EOL> if len ( fieldset ) != <NUM_LIT:2> : <EOL> raise ImproperlyConfigured ( "<STR_LIT>" <EOL> "<STR_LIT>" % ( cls . __name__ , idx ) ) <EOL> check_isdict ( cls , '<STR_LIT>' % idx , fieldset [ <NUM_LIT:1> ] ) <EOL> if '<STR_LIT>' not in fieldset [ <NUM_LIT:1> ] : <EOL> raise ImproperlyConfigured ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> % ( cls . __name__ , idx ) ) <EOL> for fields in fieldset [ <NUM_LIT:1> ] [ '<STR_LIT>' ] : <EOL> if type ( fields ) != tuple : <EOL> fields = ( fields , ) <EOL> for field in fields : <EOL> if field in cls . readonly_fields : <EOL> continue <EOL> check_formfield ( cls , model , opts , "<STR_LIT>" % idx , field ) <EOL> try : <EOL> f = opts . get_field ( field ) <EOL> if isinstance ( f , models . ManyToManyField ) and not f . rel . through . _meta . auto_created : <EOL> raise ImproperlyConfigured ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % ( <EOL> cls . __name__ , idx , field , field ) ) <EOL> except models . FieldDoesNotExist : <EOL> pass <EOL> flattened_fieldsets = flatten_fieldsets ( cls . fieldsets ) <EOL> if len ( flattened_fieldsets ) > len ( set ( flattened_fieldsets ) ) : <EOL> raise ImproperlyConfigured ( '<STR_LIT>' % cls . __name__ ) <EOL> if cls . exclude : <EOL> check_isseq ( cls , '<STR_LIT>' , cls . exclude ) <EOL> for field in cls . exclude : <EOL> check_formfield ( cls , model , opts , '<STR_LIT>' , field ) <EOL> try : <EOL> f = opts . get_field ( field ) <EOL> except models . FieldDoesNotExist : <EOL> continue <EOL> if len ( cls . exclude ) > len ( set ( cls . exclude ) ) : <EOL> raise ImproperlyConfigured ( '<STR_LIT>' % cls . __name__ ) <EOL> if hasattr ( cls , '<STR_LIT>' ) and not issubclass ( cls . form , BaseModelForm ) : <EOL> raise ImproperlyConfigured ( "<STR_LIT>" <EOL> "<STR_LIT>" % cls . __name__ ) <EOL> if hasattr ( cls , '<STR_LIT>' ) : <EOL> check_isseq ( cls , '<STR_LIT>' , cls . filter_vertical ) <EOL> for idx , field in enumerate ( cls . filter_vertical ) : <EOL> f = get_field ( cls , model , opts , '<STR_LIT>' , field ) <EOL> if not isinstance ( f , models . ManyToManyField ) : <EOL> raise ImproperlyConfigured ( "<STR_LIT>" <EOL> "<STR_LIT>" % ( cls . __name__ , idx ) ) <EOL> if hasattr ( cls , '<STR_LIT>' ) : <EOL> check_isseq ( cls , '<STR_LIT>' , cls . filter_horizontal ) <EOL> for idx , field in enumerate ( cls . filter_horizontal ) : <EOL> f = get_field ( cls , model , opts , '<STR_LIT>' , field ) <EOL> if not isinstance ( f , models . ManyToManyField ) : <EOL> raise ImproperlyConfigured ( "<STR_LIT>" <EOL> "<STR_LIT>" % ( cls . __name__ , idx ) ) <EOL> if hasattr ( cls , '<STR_LIT>' ) : <EOL> check_isdict ( cls , '<STR_LIT>' , cls . radio_fields ) <EOL> for field , val in cls . radio_fields . items ( ) : <EOL> f = get_field ( cls , model , opts , '<STR_LIT>' , field ) <EOL> if not ( isinstance ( f , models . ForeignKey ) or f . choices ) : <EOL> raise ImproperlyConfigured ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % ( cls . __name__ , field ) ) <EOL> if not val in ( HORIZONTAL , VERTICAL ) : <EOL> raise ImproperlyConfigured ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> % ( cls . __name__ , field ) ) <EOL> if hasattr ( cls , '<STR_LIT>' ) : <EOL> check_isdict ( cls , '<STR_LIT>' , cls . prepopulated_fields ) <EOL> for field , val in cls . prepopulated_fields . items ( ) : <EOL> f = get_field ( cls , model , opts , '<STR_LIT>' , field ) <EOL> if isinstance ( f , ( models . DateTimeField , models . ForeignKey , <EOL> models . ManyToManyField ) ) : <EOL> raise ImproperlyConfigured ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> % ( cls . __name__ , field ) ) <EOL> check_isseq ( cls , "<STR_LIT>" % field , val ) <EOL> for idx , f in enumerate ( val ) : <EOL> get_field ( cls , model , opts , "<STR_LIT>" % ( field , idx ) , f ) <EOL> def check_isseq ( cls , label , obj ) : <EOL> if not isinstance ( obj , ( list , tuple ) ) : <EOL> raise ImproperlyConfigured ( "<STR_LIT>" % ( cls . __name__ , label ) ) <EOL> def check_isdict ( cls , label , obj ) : <EOL> if not isinstance ( obj , dict ) : <EOL> raise ImproperlyConfigured ( "<STR_LIT>" % ( cls . __name__ , label ) ) <EOL> def get_field ( cls , model , opts , label , field ) : <EOL> try : <EOL> return opts . get_field ( field ) <EOL> except models . FieldDoesNotExist : <EOL> raise ImproperlyConfigured ( "<STR_LIT>" <EOL> % ( cls . __name__ , label , field , model . __name__ ) ) <EOL> def check_formfield ( cls , model , opts , label , field ) : <EOL> if getattr ( cls . form , '<STR_LIT>' , None ) : <EOL> try : <EOL> cls . form . base_fields [ field ] <EOL> except KeyError : <EOL> raise ImproperlyConfigured ( "<STR_LIT>" <EOL> "<STR_LIT>" % ( cls . __name__ , label , field ) ) <EOL> else : <EOL> fields = fields_for_model ( model ) <EOL> try : <EOL> fields [ field ] <EOL> except KeyError : <EOL> raise ImproperlyConfigured ( "<STR_LIT>" <EOL> "<STR_LIT>" % ( cls . __name__ , label , field ) ) <EOL> def fetch_attr ( cls , model , opts , label , field ) : <EOL> try : <EOL> return opts . get_field ( field ) <EOL> except models . FieldDoesNotExist : <EOL> pass <EOL> try : <EOL> return getattr ( model , field ) <EOL> except AttributeError : <EOL> raise ImproperlyConfigured ( "<STR_LIT>" <EOL> % ( cls . __name__ , label , field , model . __name__ ) ) </s>
