def new(self, model_class, shelve=False, named_shelve=None, persist_dependencies=True, **kwargs):
     """
     Create an instance filled with data without persist it.
     1) validate all kwargs match Model.fields.
     2) validate model is a model.Model class.
     3) Iterate model fields: for each field, fill it with data.
     @shelve: the current configuration will be stored in the DDF library. It can be True or a string (named shelve).
     @named_shelve: restore configuration saved in DDF library with a name.
     @persist_dependencies: tell if internal dependencies will be saved in the database or not.
     """
     if self.debug_mode:
         LOGGER.debug('>>> [%s] Generating instance.' % get_unique_model_name(model_class))
     configuration = self._configure_params(model_class, shelve, named_shelve, **kwargs)
     instance = model_class()
     if not is_model_class(instance):
         raise InvalidModelError(get_unique_model_name(model_class)), None, sys.exc_info()[2]
     for field in get_fields_from_model(model_class):
         if is_key_field(field) and 'id' not in configuration: continue
         if field.name in self.ignore_fields: continue
         self.set_data_for_a_field(model_class, instance, field, persist_dependencies=persist_dependencies, **configuration)
     number_of_pending_fields = len(self.pending_fields)
     # For Copier fixtures: dealing with pending fields that need to receive values of another fields.
     i = 0
     while self.pending_fields != []:
         field_name = self.pending_fields.pop(0)
         field = get_field_by_name_or_raise(model_class, field_name)
         self.set_data_for_a_field(model_class, instance, field, persist_dependencies=persist_dependencies, **configuration)
         i += 1
         if i > 2 * number_of_pending_fields: # dealing with infinite loop too.
             raise InvalidConfigurationError(get_unique_field_name(field), u'Cyclic dependency of Copiers.'), None, sys.exc_info()[2]
     if self.debug_mode:
         LOGGER.debug('<<< [%s] Instance created.' % get_unique_model_name(model_class))
     return instance
Beispiel #2
0
    def new(self, model_class, shelve=False, named_shelve=None, persist_dependencies=True, **kwargs):
        """
        Create an instance filled with data without persist it.
        1) validate all kwargs match Model.fields.
        2) validate model is a model.Model class.
        3) Iterate model fields: for each field, fill it with data.

        :shelve: the current configuration will be stored in the DDF library. It can be True or a string (named shelve).
        :named_shelve: restore configuration saved in DDF library with a name.
        :persist_dependencies: tell if internal dependencies will be saved in the database or not.
        """
        if self.debug_mode:
            LOGGER.debug('>>> [%s] Generating instance.' % get_unique_model_name(model_class))
        configuration = self._configure_params(model_class, shelve, named_shelve, **kwargs)
        instance = model_class()
        if not is_model_class(instance):
            raise InvalidModelError(get_unique_model_name(model_class))
        for field in get_fields_from_model(model_class):
            if is_key_field(field) and 'id' not in configuration: continue
            if field.name not in self.kwargs and self._is_ignored_field(field.name): continue
            self.set_data_for_a_field(model_class, instance, field, persist_dependencies=persist_dependencies, **configuration)
        number_of_pending_fields = len(self.pending_fields)
        # For Copier fixtures: dealing with pending fields that need to receive values of another fields.
        i = 0
        while self.pending_fields != []:
            field_name = self.pending_fields.pop(0)
            field = get_field_by_name_or_raise(model_class, field_name)
            self.set_data_for_a_field(model_class, instance, field, persist_dependencies=persist_dependencies, **configuration)
            i += 1
            if i > 2 * number_of_pending_fields: # dealing with infinite loop too.
                raise InvalidConfigurationError(get_unique_field_name(field), 'Cyclic dependency of Copiers.')
        if self.debug_mode:
            LOGGER.debug('<<< [%s] Instance created.' % get_unique_model_name(model_class))
        return instance
Beispiel #3
0
    def new(self,
            model_class,
            ddf_lesson=None,
            persist_dependencies=True,
            **kwargs):
        '''
        Create an instance filled with data without persist it.
        1) validate all kwargs match Model.fields.
        2) validate model is a model.Model class.
        3) Iterate model fields: for each field, fill it with data.

        :ddf_lesson: the lesson that will be used to create the model instance, if exists.
        :persist_dependencies: tell if internal dependencies will be saved in the database or not.
        '''
        if self.debug_mode:
            LOGGER.debug('>>> [%s] Generating instance.' %
                         get_unique_model_name(model_class))
        configuration = self._configure_params(model_class, ddf_lesson,
                                               **kwargs)
        instance = model_class()
        if not is_model_class(instance):
            raise InvalidModelError(get_unique_model_name(model_class))

        try:
            # https://github.com/paulocheque/django-dynamic-fixture/pull/112
            from polymorphic import PolymorphicModel
            is_polymorphic = isinstance(instance, PolymorphicModel)
        except ImportError:
            # Django-polymorphic is not installed so the model can't be polymorphic.
            is_polymorphic = False
        for field in get_fields_from_model(model_class):
            if is_key_field(field) and field.name not in configuration:
                continue
            if field.name not in self.kwargs and self._is_ignored_field(
                    field.name):
                continue
            if is_polymorphic and (field.name == 'polymorphic_ctype'
                                   or field.primary_key):
                continue
            self.set_data_for_a_field(
                model_class,
                instance,
                field,
                persist_dependencies=persist_dependencies,
                **configuration)
        number_of_pending_fields = len(self.pending_fields)
        # For Copier fixtures: dealing with pending fields that need to receive values of another fields.
        i = 0
        while self.pending_fields != []:
            field_name = self.pending_fields.pop(0)
            field = get_field_by_name_or_raise(model_class, field_name)
            self.set_data_for_a_field(
                model_class,
                instance,
                field,
                persist_dependencies=persist_dependencies,
                **configuration)
            i += 1
            if i > 2 * number_of_pending_fields:  # dealing with infinite loop too.
                raise InvalidConfigurationError(
                    get_unique_field_name(field),
                    'Cyclic dependency of Copiers.')
        if self.debug_mode:
            LOGGER.debug('<<< [%s] Instance created.' %
                         get_unique_model_name(model_class))
        return instance