Example #1
0
    def generate_value(self, field, **fk_attrs):
        '''
        Calls the generator associated with a field passing all required args.

        Generator Resolution Precedence Order:
        -- attr_mapping - mapping per attribute name
        -- choices -- mapping from avaiable field choices
        -- type_mapping - mapping from user defined type associated generators
        -- default_mapping - mapping from pre-defined type associated
           generators

        `attr_mapping` and `type_mapping` can be defined easely overwriting the
        model.
        '''
        if field.name in self.attr_mapping:
            generator = self.attr_mapping[field.name]
        elif getattr(field, 'choices'):
            generator = generators.gen_from_choices(field.choices)
        elif isinstance(field, ForeignKey) and isinstance(field.rel.to, ContentType):
            generator = self.type_mapping[ContentType]
        elif field.__class__ in self.type_mapping:
            generator = self.type_mapping[field.__class__]
        else:
            raise TypeError('%s is not supported by mommy.' % field.__class__)

        # attributes like max_length, decimal_places are take in account when
        # generating the value.
        generator_attrs = get_required_values(generator, field)

        if isinstance(field, ForeignKey):
            generator_attrs.update(filter_fk_attrs(field.name, **fk_attrs))

        return generator(**generator_attrs)
Example #2
0
    def generate_value(self, field):
        '''Calls the generator associated with a field passing all required args.

        Generator Resolution Precedence Order:
        -- attr_mapping - mapping per attribute name
        -- choices -- mapping from avaiable field choices
        -- type_mapping - mapping from user defined type associated generators
        -- default_mapping - mapping from pre-defined type associated generators

        `attr_mapping` and `type_mapping` can be defined easely overwriting the model.
        '''
        
        if field.name in self.attr_mapping:
            generator = self.attr_mapping[field.name]
        elif getattr(field, 'choices'):
            generator = generators.gen_from_choices(field.choices)        
        elif isinstance(field, ForeignKey) and field.rel.to is ContentType:
            generator = self.type_mapping[ContentType]
        elif field.__class__ in self.type_mapping:
            generator = self.type_mapping[field.__class__]
        else:
            raise TypeError('%s is not supported by mommy.' % field.__class__)

        required_fields = get_required_values(generator, field)
        return generator(**required_fields)
Example #3
0
    def generate_value(self, field):
        '''
        Calls the generator associated with a field passing all required args.

        Generator Resolution Precedence Order:
        -- attr_mapping - mapping per attribute name
        -- choices -- mapping from avaiable field choices
        -- type_mapping - mapping from user defined type associated generators
        -- default_mapping - mapping from pre-defined type associated
           generators

        `attr_mapping` and `type_mapping` can be defined easely overwriting the
        model.
        '''
        if field.name in self.attr_mapping:
            generator = self.attr_mapping[field.name]
        elif getattr(field, 'choices'):
            generator = generators.gen_from_choices(field.choices)
        elif isinstance(field, ForeignKey) and isinstance(field.rel.to, ContentType):
            generator = self.type_mapping[ContentType]
        elif field.__class__ in self.type_mapping:
            generator = self.type_mapping[field.__class__]
        else:
            raise TypeError('%s is not supported by mommy.' % field.__class__)

        # attributes like max_length, decimal_places are take in account when
        # generating the value.
        generator_attrs = get_required_values(generator, field)

        if field.name in self.rel_fields:
            generator_attrs.update(filter_rel_attrs(field.name, **self.rel_attrs))

        return generator(**generator_attrs)
Example #4
0
    def generate_value(self, field):
        '''Calls the generator associated with a field passing all required args.

        Generator Resolution Precedence Order:
        -- attr_mapping - mapping per attribute name
        -- choices -- mapping from avaiable field choices
        -- type_mapping - mapping from user defined type associated generators
        -- default_mapping - mapping from pre-defined type associated generators

        `attr_mapping` and `type_mapping` can be defined easely overwriting the model.
        '''

        if field.name in self.attr_mapping:
            generator = self.attr_mapping[field.name]
        elif getattr(field, 'choices'):
            generator = generators.gen_from_choices(field.choices)
        elif isinstance(field, ForeignKey) and field.rel.to is ContentType:
            generator = self.type_mapping[ContentType]
        elif field.__class__ in self.type_mapping:
            generator = self.type_mapping[field.__class__]
        else:
            raise TypeError('%s is not supported by mommy.' % field.__class__)

        required_fields = get_required_values(generator, field)
        return generator(**required_fields)
Example #5
0
    def generate_value(self, field):
        '''Calls the generator associated with a field passing all required args.

        Generator Resolution Precedence Order:
        -- attr_mapping - mapping per attribute name
        -- choices -- mapping from avaiable field choices
        -- type_mapping - mapping from user defined type associated generators
        -- default_mapping - mapping from pre-defined type associated generators

        `attr_mapping` and `type_mapping` can be defined easely overwriting the model.
        '''
        if field.name in self.attr_mapping:
            generator = self.attr_mapping[field.name]
        elif getattr(field, 'choices'):
            generator = generators.gen_from_choices(field.choices)
        elif field.__class__ in self.type_mapping:
            generator = self.type_mapping[field.__class__]

        required_fields = get_required_values(generator, field)
        return generator(**required_fields)
Example #6
0
    def generate_value(self, field):
        '''
        Calls the generator associated with a field passing all required args.

        Generator Resolution Precedence Order:
        -- attr_mapping - mapping per attribute name
        -- choices -- mapping from avaiable field choices
        -- type_mapping - mapping from user defined type associated generators
        -- default_mapping - mapping from pre-defined type associated
           generators

        `attr_mapping` and `type_mapping` can be defined easely overwriting the
        model.
        '''
        if field.name in self.attr_mapping:
            generator = self.attr_mapping[field.name]
        elif getattr(field, 'choices'):
            generator = generators.gen_from_choices(field.choices)
        elif isinstance(field, ForeignKey) and isinstance(field.rel.to, ContentType):
            generator = self.type_mapping[ContentType]
        elif field.__class__ in self.type_mapping:
            generator = self.type_mapping[field.__class__]
        elif isinstance(field, ManyToManyField):
            generator = __m2m_generator
        elif isinstance(field, BooleanField):
            generator = generators.gen_boolean
        elif isinstance(field, ContentType):
            generator = generators.gen_content_type
        elif isinstance(field, DateField):
            generator = generators.gen_date
        elif isinstance(field, DateTimeField):
            generator = generators.gen_datetime
        elif isinstance(field, DecimalField):
            generator = generators.gen_decimal
        elif isinstance(field, EmailField):
            generator = generators.gen_email
        elif isinstance(field, FileField):
            generator = generators.gen_file_field
        elif isinstance(field, FloatField):
            generator = generators.gen_float
        elif isinstance(field, ImageField):
            generator = generators.gen_image_field
        elif isinstance(field, (BigIntegerField, IntegerField, SmallIntegerField)):
            generator = generators.gen_integer
        elif isinstance(field, SlugField):
            generator = generators.gen_slug
        elif isinstance(field, CharField):
            generator = generators.gen_string
        elif isinstance(field, TextField):
            generator = generators.gen_text
        elif isinstance(field, TimeField):
            generator = generators.gen_time
        elif isinstance(field, URLField):
            generator = generators.gen_url
        elif isinstance(field, (PositiveIntegerField, PositiveSmallIntegerField)):
            generator = lambda: generators.gen_integer(0)
        elif isinstance(field, ForeignKey):
            generator = make
        elif isinstance(field, OneToOneField):
            generator = make
        else:
            raise TypeError('%s is not supported by mommy.' % field.__class__)

        # attributes like max_length, decimal_places are take in account when
        # generating the value.
        generator_attrs = get_required_values(generator, field)

        if field.name in self.rel_fields:
            generator_attrs.update(filter_rel_attrs(field.name, **self.rel_attrs))

        return generator(**generator_attrs)