<s> from django . core . exceptions import ImproperlyConfigured <EOL> from django . db import models <EOL> from django . db . models . fields import FieldDoesNotExist <EOL> from django . forms . models import BaseModelForm , BaseModelFormSet , _get_foreign_key <EOL> from django . contrib . admin . util import get_fields_from_path , NotRelationField <EOL> """<STR_LIT>""" <EOL> __all__ = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> class BaseValidator ( object ) : <EOL> def __init__ ( self ) : <EOL> models . get_apps ( ) <EOL> def validate ( self , cls , model ) : <EOL> for m in dir ( self ) : <EOL> if m . startswith ( '<STR_LIT>' ) : <EOL> getattr ( self , m ) ( cls , model ) <EOL> def check_field_spec ( self , cls , model , flds , label ) : <EOL> """<STR_LIT>""" <EOL> for fields in flds : <EOL> if type ( fields ) != tuple : <EOL> fields = ( fields , ) <EOL> for field in fields : <EOL> if field in cls . readonly_fields : <EOL> continue <EOL> try : <EOL> f = model . _meta . 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>" % ( <EOL> cls . __name__ , label , field , field ) ) <EOL> def validate_raw_id_fields ( self , cls , model ) : <EOL> "<STR_LIT>" <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 , '<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> def validate_fields ( self , cls , model ) : <EOL> "<STR_LIT>" <EOL> if cls . fields : <EOL> check_isseq ( cls , '<STR_LIT>' , cls . fields ) <EOL> self . check_field_spec ( cls , model , cls . fields , '<STR_LIT>' ) <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> def validate_fieldsets ( self , cls , model ) : <EOL> "<STR_LIT>" <EOL> from django . contrib . admin . options import flatten_fieldsets <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> self . check_field_spec ( cls , model , fieldset [ <NUM_LIT:1> ] [ '<STR_LIT>' ] , "<STR_LIT>" % idx ) <EOL> flattened_fieldsets = flatten_fieldsets ( cls . fieldsets ) <EOL> if len ( flattened_fieldsets ) > len ( set ( flattened_fieldsets ) ) : <EOL> raise ImproperlyConfigured ( '<STR_LIT>' % cls . __name__ ) <EOL> def validate_exclude ( self , cls , model ) : <EOL> "<STR_LIT>" <EOL> if cls . exclude : <EOL> check_isseq ( cls , '<STR_LIT>' , cls . exclude ) <EOL> if len ( cls . exclude ) > len ( set ( cls . exclude ) ) : <EOL> raise ImproperlyConfigured ( '<STR_LIT>' % cls . __name__ ) <EOL> def validate_form ( self , cls , model ) : <EOL> "<STR_LIT>" <EOL> if hasattr ( cls , '<STR_LIT>' ) and not issubclass ( cls . form , BaseModelForm ) : <EOL> raise ImproperlyConfigured ( "<STR_LIT>" <EOL> "<STR_LIT>" % cls . __name__ ) <EOL> def validate_filter_vertical ( self , cls , model ) : <EOL> "<STR_LIT>" <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 , '<STR_LIT>' , field ) <EOL> if not isinstance ( f , models . ManyToManyField ) : <EOL> raise ImproperlyConfigured ( "<STR_LIT>" <EOL> "<STR_LIT>" % ( cls . __name__ , idx ) ) <EOL> def validate_filter_horizontal ( self , cls , model ) : <EOL> "<STR_LIT>" <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 , '<STR_LIT>' , field ) <EOL> if not isinstance ( f , models . ManyToManyField ) : <EOL> raise ImproperlyConfigured ( "<STR_LIT>" <EOL> "<STR_LIT>" % ( cls . __name__ , idx ) ) <EOL> def validate_radio_fields ( self , cls , model ) : <EOL> "<STR_LIT>" <EOL> from django . contrib . admin . options import HORIZONTAL , VERTICAL <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 , '<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> def validate_prepopulated_fields ( self , cls , model ) : <EOL> "<STR_LIT>" <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 , '<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 , "<STR_LIT>" % ( field , idx ) , f ) <EOL> def validate_ordering ( self , cls , model ) : <EOL> "<STR_LIT>" <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 , '<STR_LIT>' % idx , field ) <EOL> def validate_readonly_fields ( self , cls , model ) : <EOL> "<STR_LIT>" <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> model . _meta . get_field ( field ) <EOL> except models . FieldDoesNotExist : <EOL> raise ImproperlyConfigured ( "<STR_LIT>" <EOL> % ( cls . __name__ , idx , field , cls . __name__ , model . _meta . object_name ) ) <EOL> class ModelAdminValidator ( BaseValidator ) : <EOL> def validate_save_as ( self , cls , model ) : <EOL> "<STR_LIT>" <EOL> check_type ( cls , '<STR_LIT>' , bool ) <EOL> def validate_save_on_top ( self , cls , model ) : <EOL> "<STR_LIT>" <EOL> check_type ( cls , '<STR_LIT>' , bool ) <EOL> def validate_inlines ( self , cls , model ) : <EOL> "<STR_LIT>" <EOL> from django . contrib . admin . options import BaseModelAdmin <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> inline . validate ( inline . model ) <EOL> self . check_inline ( inline , model ) <EOL> def check_inline ( self , cls , parent_model ) : <EOL> "<STR_LIT>" <EOL> fk = _get_foreign_key ( parent_model , cls . model , fk_name = cls . fk_name , can_fail = True ) <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 . _meta . app_label , parent_model . __name__ ) ) <EOL> def validate_list_display ( self , cls , model ) : <EOL> "<STR_LIT>" <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> model . _meta . 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 , "<STR_LIT>" % idx , field ) <EOL> if isinstance ( f , models . ManyToManyField ) : <EOL> raise ImproperlyConfigured ( "<STR_LIT>" <EOL> % ( cls . __name__ , idx , field ) ) <EOL> def validate_list_display_links ( self , cls , model ) : <EOL> "<STR_LIT>" <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> if field not in cls . list_display : <EOL> raise ImproperlyConfigured ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> % ( cls . __name__ , idx , field ) ) <EOL> def validate_list_filter ( self , cls , model ) : <EOL> """<STR_LIT>""" <EOL> from django . contrib . admin import ListFilter , FieldListFilter <EOL> if hasattr ( cls , '<STR_LIT>' ) : <EOL> check_isseq ( cls , '<STR_LIT>' , cls . list_filter ) <EOL> for idx , item in enumerate ( cls . list_filter ) : <EOL> if callable ( item ) and not isinstance ( item , models . Field ) : <EOL> if not issubclass ( item , ListFilter ) : <EOL> raise ImproperlyConfigured ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> % ( cls . __name__ , idx , item . __name__ ) ) <EOL> if issubclass ( item , FieldListFilter ) : <EOL> raise ImproperlyConfigured ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> % ( cls . __name__ , idx , item . __name__ ) ) <EOL> else : <EOL> if isinstance ( item , ( tuple , list ) ) : <EOL> field , list_filter_class = item <EOL> if not issubclass ( list_filter_class , FieldListFilter ) : <EOL> raise ImproperlyConfigured ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> % ( cls . __name__ , idx , list_filter_class . __name__ ) ) <EOL> else : <EOL> field = item <EOL> try : <EOL> get_fields_from_path ( model , field ) <EOL> except ( NotRelationField , FieldDoesNotExist ) : <EOL> raise ImproperlyConfigured ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> % ( cls . __name__ , idx , field ) ) <EOL> def validate_list_select_related ( self , cls , model ) : <EOL> "<STR_LIT>" <EOL> list_select_related = getattr ( cls , '<STR_LIT>' , None ) <EOL> if list_select_related : <EOL> types = ( bool , tuple , list ) <EOL> if not isinstance ( list_select_related , types ) : <EOL> raise ImproperlyConfigured ( "<STR_LIT>" <EOL> "<STR_LIT>" % <EOL> cls . __name__ ) <EOL> def validate_list_per_page ( self , cls , model ) : <EOL> "<STR_LIT>" <EOL> check_type ( cls , '<STR_LIT>' , int ) <EOL> def validate_list_max_show_all ( self , cls , model ) : <EOL> "<STR_LIT>" <EOL> check_type ( cls , '<STR_LIT>' , int ) <EOL> def validate_list_editable ( self , cls , model ) : <EOL> """<STR_LIT>""" <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 = model . _meta . 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 . _meta . app_label , 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> def validate_search_fields ( self , cls , model ) : <EOL> "<STR_LIT>" <EOL> if hasattr ( cls , '<STR_LIT>' ) : <EOL> check_isseq ( cls , '<STR_LIT>' , cls . search_fields ) <EOL> def validate_date_hierarchy ( self , cls , model ) : <EOL> "<STR_LIT>" <EOL> if cls . date_hierarchy : <EOL> f = get_field ( cls , model , '<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> class InlineValidator ( BaseValidator ) : <EOL> def validate_fk_name ( self , cls , model ) : <EOL> "<STR_LIT>" <EOL> if cls . fk_name : <EOL> f = get_field ( cls , model , '<STR_LIT>' , cls . fk_name ) <EOL> if not isinstance ( f , models . ForeignKey ) : <EOL> raise ImproperlyConfigured ( "<STR_LIT>" <EOL> "<STR_LIT>" % cls . __name__ ) <EOL> def validate_extra ( self , cls , model ) : <EOL> "<STR_LIT>" <EOL> check_type ( cls , '<STR_LIT>' , int ) <EOL> def validate_max_num ( self , cls , model ) : <EOL> "<STR_LIT>" <EOL> check_type ( cls , '<STR_LIT>' , int ) <EOL> def validate_formset ( self , cls , model ) : <EOL> "<STR_LIT>" <EOL> if hasattr ( cls , '<STR_LIT>' ) and not issubclass ( cls . formset , BaseModelFormSet ) : <EOL> raise ImproperlyConfigured ( "<STR_LIT>" <EOL> "<STR_LIT>" % cls . __name__ ) <EOL> def check_type ( cls , attr , type_ ) : <EOL> if getattr ( cls , attr , None ) is not None and not isinstance ( getattr ( cls , attr ) , type_ ) : <EOL> raise ImproperlyConfigured ( "<STR_LIT>" <EOL> % ( cls . __name__ , attr , type_ . __name__ ) ) <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 , label , field ) : <EOL> try : <EOL> return model . _meta . get_field ( field ) <EOL> except models . FieldDoesNotExist : <EOL> raise ImproperlyConfigured ( "<STR_LIT>" <EOL> % ( cls . __name__ , label , field , model . _meta . app_label , model . __name__ ) ) <EOL> def fetch_attr ( cls , model , label , field ) : <EOL> try : <EOL> return model . _meta . 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 . _meta . app_label , model . __name__ ) ) </s>
