Esempio n. 1
0
    def create_one(self, commit=True):
        '''
        Create and return one model instance. If *commit* is ``False`` the
        instance will not be saved and many to many relations will not be
        processed.

        Subclasses that override ``create_one`` can specify arbitrary keyword
        arguments. They will be passed through by the
        :meth:`autofixture.base.AutoFixture.create` method and the helper
        functions :func:`autofixture.create` and
        :func:`autofixture.create_one`.

        May raise :exc:`CreateInstanceError` if constraints are not satisfied.
        '''
        tries = self.tries
        instance = self.model()
        process = instance._meta.fields
        while process and tries > 0:
            for field in process:
                self.process_field(instance, field)
            process = self.check_constraints(instance)
            tries -= 1
        if tries == 0:
            raise CreateInstanceError(
                u'Cannot solve constraints for "%s", tried %d times. '
                u'Please check value generators or model constraints. '
                u'At least the following fields are involved: %s' % (
                    '%s.%s' %
                    (self.model._meta.app_label, self.model._meta.object_name),
                    self.tries,
                    ', '.join([field.name for field in process]),
                ))

        instance = self.pre_process_instance(instance)

        if commit:
            instance.save()

            #to handle particular case of GenericRelation
            #in Django pre 1.6 it appears in .many_to_many
            many_to_many = [
                f for f in instance._meta.many_to_many
                if not isinstance(f, get_GenericRelation())
            ]
            for field in many_to_many:
                self.process_m2m(instance, field)
        signals.instance_created.send(sender=self,
                                      model=self.model,
                                      instance=instance,
                                      committed=commit)

        post_process_kwargs = {}
        if 'commit' in getargnames(self.post_process_instance):
            post_process_kwargs['commit'] = commit
        else:
            warnings.warn(
                "Subclasses of AutoFixture need to provide a `commit` "
                "argument for post_process_instance methods",
                DeprecationWarning)
        return self.post_process_instance(instance, **post_process_kwargs)
Esempio n. 2
0
    def create_one(self, commit=True):
        '''
        Create and return one model instance. If *commit* is ``False`` the
        instance will not be saved and many to many relations will not be
        processed.

        Subclasses that override ``create_one`` can specify arbitrary keyword
        arguments. They will be passed through by the
        :meth:`autofixture.base.AutoFixture.create` method and the helper
        functions :func:`autofixture.create` and
        :func:`autofixture.create_one`.

        May raise :exc:`CreateInstanceError` if constraints are not satisfied.
        '''
        tries = self.tries
        instance = self.model()
        process = instance._meta.fields
        while process and tries > 0:
            for field in process:
                self.process_field(instance, field)
            process = self.check_constraints(instance)
            tries -= 1
        if tries == 0:
            raise CreateInstanceError(
                u'Cannot solve constraints for "%s", tried %d times. '
                u'Please check value generators or model constraints. '
                u'At least the following fields are involved: %s' % (
                    '%s.%s' % (
                        self.model._meta.app_label,
                        self.model._meta.object_name),
                    self.tries,
                    ', '.join([field.name for field in process]),
            ))

        instance = self.pre_process_instance(instance)

        if commit:
            instance.save()

            #to handle particular case of GenericRelation
            #in Django pre 1.6 it appears in .many_to_many
            many_to_many = [f for f in instance._meta.many_to_many
                            if not isinstance(f, get_GenericRelation())]
            for field in many_to_many:
                self.process_m2m(instance, field)
        signals.instance_created.send(
            sender=self,
            model=self.model,
            instance=instance,
            committed=commit)

        post_process_kwargs = {}
        if 'commit' in getargnames(self.post_process_instance):
            post_process_kwargs['commit'] = commit
        else:
            warnings.warn(
                "Subclasses of AutoFixture need to provide a `commit` "
                "argument for post_process_instance methods", DeprecationWarning)
        return self.post_process_instance(instance, **post_process_kwargs)
Esempio n. 3
0
def create(model, count, *args, **kwargs):
    '''
    Create *count* instances of *model* using the either an appropiate
    autofixture that was :ref:`registry <registry>` or fall back to the
    default:class:`AutoFixture` class. *model* can be a model class or its
    string representation (e.g. ``"app.ModelClass"``).

    All positional and keyword arguments are passed to the autofixture
    constructor. It is demonstrated in the example below which will create ten
    superusers::

        import autofixture
        admins = autofixture.create(
            'auth.User', 10, field_values={'is_superuser': True})

    .. note:: See :ref:`AutoFixture` for more information.

    :func:`create` will return a list of the created objects.
    '''
    from .compat import get_model

    if isinstance(model, string_types):
        model = get_model(*model.split('.', 1))
    if model in REGISTRY:
        autofixture_class = REGISTRY[model]
    else:
        autofixture_class = AutoFixture
    # Get keyword arguments that the create_one method accepts and pass them
    # into create_one instead of AutoFixture.__init__
    argnames = set(getargnames(autofixture_class.create_one))
    argnames -= set(['self'])
    create_kwargs = {}
    for argname in argnames:
        if argname in kwargs:
            create_kwargs[argname] = kwargs.pop(argname)
    autofixture = autofixture_class(model, *args, **kwargs)
    return autofixture.create(count, **create_kwargs)
def create(model, count, *args, **kwargs):
    '''
    Create *count* instances of *model* using the either an appropiate
    autofixture that was :ref:`registry <registry>` or fall back to the
    default:class:`AutoFixture` class. *model* can be a model class or its
    string representation (e.g. ``"app.ModelClass"``).

    All positional and keyword arguments are passed to the autofixture
    constructor. It is demonstrated in the example below which will create ten
    superusers::

        import autofixture
        admins = autofixture.create('auth.User', 10, field_values={'is_superuser': True})

    .. note:: See :ref:`AutoFixture` for more information.

    :func:`create` will return a list of the created objects.
    '''
    from .compat import get_model

    if isinstance(model, string_types):
        model = get_model(*model.split('.', 1))
    if model in REGISTRY:
        autofixture_class = REGISTRY[model]
    else:
        autofixture_class = AutoFixture
    # Get keyword arguments that the create_one method accepts and pass them
    # into create_one instead of AutoFixture.__init__
    argnames = set(getargnames(autofixture_class.create_one))
    argnames -= set(['self'])
    create_kwargs = {}
    for argname in argnames:
        if argname in kwargs:
            create_kwargs[argname] = kwargs.pop(argname)
    autofixture = autofixture_class(model, *args, **kwargs)
    return autofixture.create(count, **create_kwargs)