def get(self, model_class, shelve=False, named_shelve=None, **kwargs):
        """
        Create an instance with data and persist it.

        :shelve: the current configuration will be stored in the DDF library.
        :named_shelve: restore configuration saved in DDF library with a name.
        """
        instance = self.new(model_class,
                            shelve=shelve,
                            named_shelve=named_shelve,
                            **kwargs)
        if is_model_abstract(model_class):
            raise InvalidModelError(get_unique_model_name(model_class))
        try:
            if self.validate_models:
                instance.full_clean()
            if model_class in _PRE_SAVE:
                try:
                    _PRE_SAVE[model_class](instance)
                except Exception as e:
                    six.reraise(InvalidReceiverError, InvalidReceiverError(e),
                                sys.exc_info()[2])
            self._save_the_instance(instance)
            if model_class in _POST_SAVE:
                try:
                    _POST_SAVE[model_class](instance)
                except Exception as e:
                    six.reraise(InvalidReceiverError, InvalidReceiverError(e),
                                sys.exc_info()[2])
        except Exception as e:
            if self.print_errors:
                print_field_values(instance)
            six.reraise(BadDataError,
                        BadDataError(get_unique_model_name(model_class), e),
                        sys.exc_info()[2])
        self.fields_processed = []  # TODO: need more tests for M2M and Copier
        self.pending_fields = []
        for field in get_many_to_many_fields_from_model(model_class):
            if field.name in kwargs.keys():  # TODO: library
                manytomany_field = getattr(instance, field.name)
                fixture = kwargs[field.name]
                try:
                    self._process_many_to_many_field(field, manytomany_field,
                                                     fixture, instance)
                except InvalidManyToManyConfigurationError as e:
                    six.reraise(InvalidManyToManyConfigurationError, e,
                                sys.exc_info()[2])
                except Exception as e:
                    six.reraise(
                        InvalidManyToManyConfigurationError,
                        InvalidManyToManyConfigurationError(
                            get_unique_field_name(field), e),
                        sys.exc_info()[2])
        return instance
Beispiel #2
0
    def get(self, model_class, ddf_lesson=None, **kwargs):
        '''
        Create an instance with data and persist it.

        :ddf_lesson: a custom lesson that will be used to create the model object.
        '''
        instance = self.new(model_class, ddf_lesson=ddf_lesson, **kwargs)
        if is_model_abstract(model_class):
            raise InvalidModelError(get_unique_model_name(model_class))
        try:
            if self.validate_models:
                instance.full_clean()
            if model_class in _PRE_SAVE:
                try:
                    _PRE_SAVE[model_class](instance)
                except Exception as e:
                    six.reraise(InvalidReceiverError, InvalidReceiverError(e),
                                sys.exc_info()[2])
            self._save_the_instance(instance)
            if model_class in _POST_SAVE:
                try:
                    _POST_SAVE[model_class](instance)
                except Exception as e:
                    six.reraise(InvalidReceiverError, InvalidReceiverError(e),
                                sys.exc_info()[2])
        except Exception as e:
            if self.print_errors:
                print_field_values(instance)
            six.reraise(BadDataError,
                        BadDataError(get_unique_model_name(model_class), e),
                        sys.exc_info()[2])
        self.fields_processed = []  # TODO: need more tests for M2M and Copier
        self.pending_fields = []
        for field in get_many_to_many_fields_from_model(model_class):
            if field.name in kwargs.keys():  # TODO: library
                manytomany_field = getattr(instance, field.name)
                fixture = kwargs[field.name]
                try:
                    self._process_many_to_many_field(field, manytomany_field,
                                                     fixture, instance)
                except InvalidManyToManyConfigurationError as e:
                    six.reraise(InvalidManyToManyConfigurationError, e,
                                sys.exc_info()[2])
                except Exception as e:
                    six.reraise(
                        InvalidManyToManyConfigurationError,
                        InvalidManyToManyConfigurationError(
                            get_unique_field_name(field), e),
                        sys.exc_info()[2])
        return instance