<s> import warnings <EOL> from django . db . models import fields , ImageField <EOL> from django . db . models . fields import related <EOL> from django . utils . six import with_metaclass <EOL> import autofixture <EOL> from autofixture import constraints , generators , signals <EOL> from autofixture . values import Values <EOL> from autofixture . compat import ( <EOL> OrderedDict , <EOL> get_GenericRelation , <EOL> get_remote_field , <EOL> get_remote_field_to , <EOL> getargnames , <EOL> ) <EOL> class CreateInstanceError ( Exception ) : <EOL> pass <EOL> class Link ( object ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , fields = None , default = None ) : <EOL> self . fields = { } <EOL> self . subfields = { } <EOL> self . default = default <EOL> fields = fields or { } <EOL> if fields is True : <EOL> fields = { '<STR_LIT>' : None } <EOL> if not isinstance ( fields , dict ) : <EOL> fields = dict ( [ ( v , None ) for v in fields ] ) <EOL> for field , value in fields . items ( ) : <EOL> try : <EOL> fieldname , subfield = field . split ( '<STR_LIT>' , <NUM_LIT:1> ) <EOL> self . subfields . setdefault ( fieldname , { } ) [ subfield ] = value <EOL> except ValueError : <EOL> self . fields [ field ] = value <EOL> def __getitem__ ( self , key ) : <EOL> return self . fields . get ( key , <EOL> self . fields . get ( '<STR_LIT>' , self . default ) ) <EOL> def __iter__ ( self ) : <EOL> for field in self . fields : <EOL> yield field <EOL> for key , value in self . subfields . items ( ) : <EOL> yield '<STR_LIT>' % ( key , value ) <EOL> def __contains__ ( self , value ) : <EOL> if '<STR_LIT>' in self . fields : <EOL> return True <EOL> if value in self . fields : <EOL> return True <EOL> return False <EOL> def get_deep_links ( self , field ) : <EOL> if '<STR_LIT>' in self . fields : <EOL> fields = { '<STR_LIT>' : self . fields [ '<STR_LIT>' ] } <EOL> else : <EOL> fields = self . subfields . get ( field , { } ) <EOL> if '<STR_LIT>' in fields : <EOL> fields = { '<STR_LIT>' : fields [ '<STR_LIT>' ] } <EOL> return Link ( fields , default = self . default ) <EOL> class AutoFixtureMetaclass ( type ) : <EOL> def __new__ ( mcs , name , bases , attrs ) : <EOL> values = Values ( ) <EOL> for base in bases [ : : - <NUM_LIT:1> ] : <EOL> values += base . field_values <EOL> values += Values ( attrs . pop ( '<STR_LIT>' , { } ) ) <EOL> values += attrs . get ( '<STR_LIT>' , Values ( ) ) <EOL> attrs [ '<STR_LIT>' ] = values <EOL> return super ( AutoFixtureMetaclass , mcs ) . __new__ ( mcs , name , bases , attrs ) <EOL> class AutoFixtureBase ( object ) : <EOL> '''<STR_LIT>''' <EOL> class IGNORE_FIELD ( object ) : <EOL> pass <EOL> overwrite_defaults = False <EOL> follow_fk = True <EOL> generate_fk = False <EOL> follow_m2m = { '<STR_LIT>' : ( <NUM_LIT:1> , <NUM_LIT:5> ) } <EOL> generate_m2m = False <EOL> none_p = <NUM_LIT> <EOL> tries = <NUM_LIT:1000> <EOL> field_to_generator = OrderedDict ( ( <EOL> ( fields . BooleanField , generators . BooleanGenerator ) , <EOL> ( fields . NullBooleanField , generators . NullBooleanGenerator ) , <EOL> ( fields . DateTimeField , generators . DateTimeGenerator ) , <EOL> ( fields . DateField , generators . DateGenerator ) , <EOL> ( fields . PositiveSmallIntegerField , generators . PositiveSmallIntegerGenerator ) , <EOL> ( fields . PositiveIntegerField , generators . PositiveIntegerGenerator ) , <EOL> ( fields . SmallIntegerField , generators . SmallIntegerGenerator ) , <EOL> ( fields . IntegerField , generators . IntegerGenerator ) , <EOL> ( fields . FloatField , generators . FloatGenerator ) , <EOL> ( fields . IPAddressField , generators . IPAddressGenerator ) , <EOL> ( fields . GenericIPAddressField , generators . IPAddressGenerator ) , <EOL> ( fields . TextField , generators . LoremGenerator ) , <EOL> ( fields . TimeField , generators . TimeGenerator ) , <EOL> ( ImageField , generators . ImageGenerator ) , <EOL> ) ) <EOL> if hasattr ( fields , '<STR_LIT>' ) : <EOL> field_to_generator [ fields . UUIDField ] = generators . UUIDGenerator <EOL> field_values = Values ( ) <EOL> default_constraints = [ <EOL> constraints . unique_constraint , <EOL> constraints . unique_together_constraint ] <EOL> def __init__ ( self , model , <EOL> field_values = None , none_p = None , overwrite_defaults = None , <EOL> constraints = None , follow_fk = None , generate_fk = None , <EOL> follow_m2m = None , generate_m2m = None ) : <EOL> '''<STR_LIT>''' <EOL> self . model = model <EOL> self . field_values = Values ( self . __class__ . field_values ) <EOL> self . field_values += Values ( field_values ) <EOL> self . constraints = constraints or [ ] <EOL> if none_p is not None : <EOL> self . none_p = none_p <EOL> if overwrite_defaults is not None : <EOL> self . overwrite_defaults = overwrite_defaults <EOL> if follow_fk is not None : <EOL> self . follow_fk = follow_fk <EOL> if not isinstance ( self . follow_fk , Link ) : <EOL> self . follow_fk = Link ( self . follow_fk ) <EOL> if generate_fk is not None : <EOL> self . generate_fk = generate_fk <EOL> if not isinstance ( self . generate_fk , Link ) : <EOL> self . generate_fk = Link ( self . generate_fk ) <EOL> if follow_m2m is not None : <EOL> if not isinstance ( follow_m2m , dict ) : <EOL> if follow_m2m : <EOL> follow_m2m = Link ( { '<STR_LIT>' : follow_m2m } ) <EOL> else : <EOL> follow_m2m = Link ( False ) <EOL> self . follow_m2m = follow_m2m <EOL> if not isinstance ( self . follow_m2m , Link ) : <EOL> self . follow_m2m = Link ( self . follow_m2m ) <EOL> if generate_m2m is not None : <EOL> if not isinstance ( generate_m2m , dict ) : <EOL> if generate_m2m : <EOL> generate_m2m = Link ( { '<STR_LIT>' : generate_m2m } ) <EOL> else : <EOL> generate_m2m = Link ( False ) <EOL> self . generate_m2m = generate_m2m <EOL> if not isinstance ( self . generate_m2m , Link ) : <EOL> self . generate_m2m = Link ( self . generate_m2m ) <EOL> for constraint in self . default_constraints : <EOL> self . add_constraint ( constraint ) <EOL> self . _field_generators = { } <EOL> self . prepare_class ( ) <EOL> def prepare_class ( self ) : <EOL> '''<STR_LIT>''' <EOL> pass <EOL> def add_field_value ( self , name , value ) : <EOL> '''<STR_LIT>''' <EOL> self . field_values [ name ] = value <EOL> def add_constraint ( self , constraint ) : <EOL> '''<STR_LIT>''' <EOL> self . constraints . append ( constraint ) <EOL> def is_inheritance_parent ( self , field ) : <EOL> '''<STR_LIT>''' <EOL> return ( <EOL> isinstance ( field , related . OneToOneField ) and <EOL> field . primary_key and <EOL> issubclass ( field . model , get_remote_field_to ( field ) ) <EOL> ) <EOL> def get_generator ( self , field ) : <EOL> '''<STR_LIT>''' <EOL> if isinstance ( field , fields . AutoField ) : <EOL> return None <EOL> if self . is_inheritance_parent ( field ) : <EOL> return None <EOL> if ( <EOL> field . default is not fields . NOT_PROVIDED and <EOL> not self . overwrite_defaults and <EOL> field . name not in self . field_values ) : <EOL> return None <EOL> kwargs = { } <EOL> if field . name in self . field_values : <EOL> value = self . field_values [ field . name ] <EOL> if isinstance ( value , generators . Generator ) : <EOL> return value <EOL> elif isinstance ( value , AutoFixture ) : <EOL> return generators . InstanceGenerator ( autofixture = value ) <EOL> elif callable ( value ) : <EOL> return generators . CallableGenerator ( value = value ) <EOL> return generators . StaticGenerator ( value = value ) <EOL> if field . null : <EOL> kwargs [ '<STR_LIT>' ] = self . none_p <EOL> if field . choices : <EOL> return generators . ChoicesGenerator ( choices = field . choices , ** kwargs ) <EOL> if isinstance ( field , related . ForeignKey ) : <EOL> is_self_fk = ( get_remote_field_to ( field ) ( ) . __class__ == self . model ) <EOL> if field . name in self . generate_fk and not is_self_fk : <EOL> return generators . InstanceGenerator ( <EOL> autofixture . get ( <EOL> get_remote_field_to ( field ) , <EOL> follow_fk = self . follow_fk . get_deep_links ( field . name ) , <EOL> generate_fk = self . generate_fk . get_deep_links ( field . name ) ) , <EOL> limit_choices_to = get_remote_field ( field ) . limit_choices_to ) <EOL> if field . name in self . follow_fk : <EOL> selected = generators . InstanceSelector ( <EOL> get_remote_field_to ( field ) , <EOL> limit_choices_to = get_remote_field ( field ) . limit_choices_to ) <EOL> if selected . get_value ( ) is not None : <EOL> return selected <EOL> if field . blank or field . null : <EOL> return generators . NoneGenerator ( ) <EOL> if is_self_fk and not field . null : <EOL> raise CreateInstanceError ( <EOL> u'<STR_LIT>' % ( <EOL> field . name , <EOL> '<STR_LIT>' % ( <EOL> get_remote_field_to ( field ) . _meta . app_label , <EOL> get_remote_field_to ( field ) . _meta . object_name , <EOL> ) <EOL> ) ) <EOL> raise CreateInstanceError ( <EOL> u'<STR_LIT>' <EOL> u'<STR_LIT>' % ( <EOL> field . name , <EOL> '<STR_LIT>' % ( <EOL> get_remote_field_to ( field ) . _meta . app_label , <EOL> get_remote_field_to ( field ) . _meta . object_name , <EOL> ) <EOL> ) ) <EOL> if isinstance ( field , related . ManyToManyField ) : <EOL> if field . name in self . generate_m2m : <EOL> min_count , max_count = self . generate_m2m [ field . name ] <EOL> return generators . MultipleInstanceGenerator ( <EOL> autofixture . get ( get_remote_field_to ( field ) ) , <EOL> limit_choices_to = get_remote_field ( field ) . limit_choices_to , <EOL> min_count = min_count , <EOL> max_count = max_count , <EOL> ** kwargs ) <EOL> if field . name in self . follow_m2m : <EOL> min_count , max_count = self . follow_m2m [ field . name ] <EOL> return generators . InstanceSelector ( <EOL> get_remote_field_to ( field ) , <EOL> limit_choices_to = get_remote_field ( field ) . limit_choices_to , <EOL> min_count = min_count , <EOL> max_count = max_count , <EOL> ** kwargs ) <EOL> if field . blank or field . null : <EOL> return generators . StaticGenerator ( [ ] ) <EOL> raise CreateInstanceError ( <EOL> u'<STR_LIT>' <EOL> u'<STR_LIT>' % ( <EOL> '<STR_LIT>' % ( <EOL> get_remote_field_to ( field ) . _meta . app_label , <EOL> get_remote_field_to ( field ) . _meta . object_name , <EOL> ) , <EOL> field . name , <EOL> ) ) <EOL> if isinstance ( field , fields . FilePathField ) : <EOL> return generators . FilePathGenerator ( <EOL> path = field . path , match = field . match , recursive = field . recursive , <EOL> max_length = field . max_length , ** kwargs ) <EOL> if isinstance ( field , fields . CharField ) : <EOL> if isinstance ( field , fields . SlugField ) : <EOL> generator = generators . SlugGenerator <EOL> elif isinstance ( field , fields . EmailField ) : <EOL> return generators . EmailGenerator ( <EOL> max_length = min ( field . max_length , <NUM_LIT:30> ) ) <EOL> elif isinstance ( field , fields . URLField ) : <EOL> return generators . URLGenerator ( <EOL> max_length = min ( field . max_length , <NUM_LIT> ) ) <EOL> elif field . max_length > <NUM_LIT:15> : <EOL> return generators . LoremSentenceGenerator ( <EOL> common = False , <EOL> max_length = field . max_length ) <EOL> else : <EOL> generator = generators . StringGenerator <EOL> return generator ( max_length = field . max_length ) <EOL> if isinstance ( field , fields . DecimalField ) : <EOL> return generators . DecimalGenerator ( <EOL> decimal_places = field . decimal_places , <EOL> max_digits = field . max_digits ) <EOL> if hasattr ( fields , '<STR_LIT>' ) : <EOL> if isinstance ( field , fields . BigIntegerField ) : <EOL> return generators . IntegerGenerator ( <EOL> min_value = - field . MAX_BIGINT - <NUM_LIT:1> , <EOL> max_value = field . MAX_BIGINT , <EOL> ** kwargs ) <EOL> if isinstance ( field , ImageField ) : <EOL> return generators . ImageGenerator ( storage = field . storage , ** kwargs ) <EOL> for field_class , generator in self . field_to_generator . items ( ) : <EOL> if isinstance ( field , field_class ) : <EOL> return generator ( ** kwargs ) <EOL> return None <EOL> def get_value ( self , field ) : <EOL> '''<STR_LIT>''' <EOL> if field not in self . _field_generators : <EOL> self . _field_generators [ field ] = self . get_generator ( field ) <EOL> generator = self . _field_generators [ field ] <EOL> if generator is None : <EOL> return self . IGNORE_FIELD <EOL> value = generator ( ) <EOL> return value <EOL> def process_field ( self , instance , field ) : <EOL> value = self . get_value ( field ) <EOL> if value is self . IGNORE_FIELD : <EOL> return <EOL> setattr ( instance , field . name , value ) <EOL> def process_m2m ( self , instance , field ) : <EOL> auto_created_through_model = False <EOL> through = get_remote_field ( field ) . through <EOL> auto_created_through_model = through . _meta . auto_created <EOL> if auto_created_through_model : <EOL> return self . process_field ( instance , field ) <EOL> kwargs = { } <EOL> if field . name in self . generate_m2m : <EOL> related_fks = [ fk <EOL> for fk in through . _meta . fields <EOL> if isinstance ( fk , related . ForeignKey ) and get_remote_field_to ( fk ) is get_remote_field_to ( field ) ] <EOL> self_fks = [ fk <EOL> for fk in through . _meta . fields <EOL> if isinstance ( fk , related . ForeignKey ) and get_remote_field_to ( fk ) is self . model ] <EOL> assert len ( related_fks ) == <NUM_LIT:1> <EOL> assert len ( self_fks ) == <NUM_LIT:1> <EOL> related_fk = related_fks [ <NUM_LIT:0> ] <EOL> self_fk = self_fks [ <NUM_LIT:0> ] <EOL> min_count , max_count = self . generate_m2m [ field . name ] <EOL> intermediary_model = generators . MultipleInstanceGenerator ( <EOL> AutoFixture ( <EOL> through , <EOL> field_values = { <EOL> self_fk . name : instance , <EOL> related_fk . name : generators . InstanceGenerator ( <EOL> autofixture . get ( get_remote_field_to ( field ) ) ) <EOL> } ) , <EOL> min_count = min_count , <EOL> max_count = max_count , <EOL> ** kwargs ) . generate ( ) <EOL> def check_constrains ( self , * args , ** kwargs ) : <EOL> raise TypeError ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> def check_constraints ( self , instance ) : <EOL> '''<STR_LIT>''' <EOL> recalc_fields = [ ] <EOL> for constraint in self . constraints : <EOL> try : <EOL> constraint ( self . model , instance ) <EOL> except constraints . InvalidConstraint as e : <EOL> recalc_fields . extend ( e . fields ) <EOL> return recalc_fields <EOL> def post_process_instance ( self , instance , commit ) : <EOL> '''<STR_LIT>''' <EOL> return instance <EOL> def pre_process_instance ( self , instance ) : <EOL> '''<STR_LIT>''' <EOL> return instance <EOL> def create_one ( self , commit = True ) : <EOL> '''<STR_LIT>''' <EOL> tries = self . tries <EOL> instance = self . model ( ) <EOL> process = instance . _meta . fields <EOL> while process and tries > <NUM_LIT:0> : <EOL> for field in process : <EOL> self . process_field ( instance , field ) <EOL> process = self . check_constraints ( instance ) <EOL> tries -= <NUM_LIT:1> <EOL> if tries == <NUM_LIT:0> : <EOL> raise CreateInstanceError ( <EOL> u'<STR_LIT>' <EOL> u'<STR_LIT>' <EOL> u'<STR_LIT>' % ( <EOL> '<STR_LIT>' % ( <EOL> self . model . _meta . app_label , <EOL> self . model . _meta . object_name ) , <EOL> self . tries , <EOL> '<STR_LIT:U+002CU+0020>' . join ( [ field . name for field in process ] ) , <EOL> ) ) <EOL> instance = self . pre_process_instance ( instance ) <EOL> if commit : <EOL> instance . save ( ) <EOL> many_to_many = [ f for f in instance . _meta . many_to_many <EOL> if not isinstance ( f , get_GenericRelation ( ) ) ] <EOL> for field in many_to_many : <EOL> self . process_m2m ( instance , field ) <EOL> signals . instance_created . send ( <EOL> sender = self , <EOL> model = self . model , <EOL> instance = instance , <EOL> committed = commit ) <EOL> post_process_kwargs = { } <EOL> if '<STR_LIT>' in getargnames ( self . post_process_instance ) : <EOL> post_process_kwargs [ '<STR_LIT>' ] = commit <EOL> else : <EOL> warnings . warn ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" , DeprecationWarning ) <EOL> return self . post_process_instance ( instance , ** post_process_kwargs ) <EOL> def create ( self , count = <NUM_LIT:1> , commit = True , ** kwargs ) : <EOL> '''<STR_LIT>''' <EOL> object_list = [ ] <EOL> for i in range ( count ) : <EOL> instance = self . create_one ( commit = commit , ** kwargs ) <EOL> object_list . append ( instance ) <EOL> return object_list <EOL> def iter ( self , count = <NUM_LIT:1> , commit = True ) : <EOL> for i in range ( count ) : <EOL> yield self . create_one ( commit = commit ) <EOL> def __iter__ ( self ) : <EOL> yield self . create_one ( ) <EOL> class AutoFixture ( with_metaclass ( AutoFixtureMetaclass , AutoFixtureBase ) ) : <EOL> pass </s>
