def set_data_for_a_field(self,
                             model_class,
                             __instance,
                             __field,
                             persist_dependencies=True,
                             **kwargs):
        if __field.name in kwargs:
            config = kwargs[__field.name]
            try:
                data = self._process_field_with_customized_fixture(
                    __instance, __field, config, persist_dependencies)
            except PendingField:
                return  # ignore this field for a while.
            except Exception as e:
                six.reraise(
                    InvalidConfigurationError,
                    InvalidConfigurationError(get_unique_field_name(__field),
                                              e),
                    sys.exc_info()[2])
        else:
            data = self._process_field_with_default_fixture(
                __field, model_class, persist_dependencies)

        if is_file_field(__field) and data:
            django_file = data
            if isinstance(django_file, File):
                setattr(__instance, __field.name,
                        data.name)  # set the attribute
                if hasattr(django_file.file,
                           'mode') and django_file.file.mode != 'rb':
                    django_file.file.close(
                    )  # this file may be open in another mode, for example, in a+b
                    opened_file = open(
                        django_file.file.name,
                        'rb')  # to save the file it must be open in rb mode
                    django_file.file = opened_file  # we update the reference to the rb mode opened file

                # https://github.com/paulocheque/django-dynamic-fixture/issues/10
                # getattr(__instance, __field.name).save(django_file.name, django_file) # save the file into the file storage system
                # django_file.close()
                getattr(__instance, __field.name).save(django_file.name,
                                                       django_file,
                                                       save=False)

            else:  # string (saving just a name in the file, without saving the file to the storage file system
                setattr(__instance, __field.name, data)  # Model.field = data
        else:
            if self.debug_mode:
                LOGGER.debug(
                    '%s.%s = %s' %
                    (get_unique_model_name(model_class), __field.name, data))
            try:
                setattr(__instance, __field.name, data)  # Model.field = data
            except ValueError as e:
                if is_relationship_field(__field):
                    setattr(__instance, "%s_id" % __field.name,
                            data)  # Model.field = data
                else:
                    six.reraise(*sys.exc_info())
        self.fields_processed.append(__field.name)
    def set_data_for_a_field(self, model_class, __instance, __field, persist_dependencies=True, **kwargs):
        if __field.name in kwargs:
            config = kwargs[__field.name]
            try:
                data = self._process_field_with_customized_fixture(__instance, __field, config, persist_dependencies)
            except PendingField:
                return # ignore this field for a while.
            except Exception as e:
                six.reraise(InvalidConfigurationError, InvalidConfigurationError(get_unique_field_name(__field), e), sys.exc_info()[2])
        else:
            data = self._process_field_with_default_fixture(__field, model_class, persist_dependencies)

        if is_file_field(__field) and data:
            django_file = data
            if isinstance(django_file, File):
                setattr(__instance, __field.name, data.name) # set the attribute
                if django_file.file.mode != 'rb':
                    django_file.file.close() # this file may be open in another mode, for example, in a+b
                    opened_file = open(django_file.file.name, 'rb') # to save the file it must be open in rb mode
                    django_file.file = opened_file # we update the reference to the rb mode opened file
                getattr(__instance, __field.name).save(django_file.name, django_file) # save the file into the file storage system
                django_file.close()
            else: # string (saving just a name in the file, without saving the file to the storage file system
                setattr(__instance, __field.name, data) # Model.field = data
        else:
            if self.debug_mode:
                LOGGER.debug('%s.%s = %s' % (get_unique_model_name(model_class), __field.name, data))
            try:
                setattr(__instance, __field.name, data) # Model.field = data
            except ValueError as e:
                if is_relationship_field(__field):
                    setattr(__instance, "%s_id" % __field.name, data) # Model.field = data
                else:
                    six.reraise(*sys.exc_info())
        self.fields_processed.append(__field.name)
 def _process_field_with_default_fixture(self, field, model_class, persist_dependencies):
     "The field has no custom value, so the default behavior of the tool is applied."
     if field.null and not self.fill_nullable_fields:
         return None
     if field_has_default_value(field):
         if callable(field.default):
             data = field.default() # datetime default can receive a function: datetime.now
         else:
             data = field.default
     elif field_has_choices(field):
         data = field.choices[0][0] # key of the first choice
     elif is_relationship_field(field):
         data = self._process_foreign_key(model_class, field, persist_dependencies)
     else:
         data = self.data_fixture.generate_data(field)
     return data
Exemple #4
0
 def _process_field_with_default_fixture(self, field, model_class, persist_dependencies):
     "The field has no custom value, so the default behavior of the tool is applied."
     if field.null and not self.fill_nullable_fields:
         return None
     if field_has_default_value(field):
         if callable(field.default):
             data = field.default() # datetime default can receive a function: datetime.now
         else:
             data = field.default
     elif field_has_choices(field):
         data = field.flatchoices[0][0] # key of the first choice
     elif is_relationship_field(field):
         data = self._process_foreign_key(model_class, field, persist_dependencies)
     else:
         data = self.data_fixture.generate_data(field)
     return data
Exemple #5
0
 def _process_field_with_default_fixture(self, field, model_class,
                                         persist_dependencies):
     '''
     The field has no custom value (F, C, static, Lessons...), so the default behavior of the tool is applied.
     - DDF behavior priority for common fields:
     1. Use `null` if possible (considering the `fill_nullable_fields` settings)
     2. Use the `default` value
     3. Use the first option of `choices`
     - DDF behavior priority for relationship fields:
     1. Use the `default` value
     2. Use `null` if possible, or consider the `fk_min_depth` value
     3. Create a new FK model
     '''
     if is_relationship_field(field):
         if field_has_default_value(field):
             # datetime default can receive a function: datetime.now
             data = field.default() if callable(
                 field.default) else field.default
         else:
             if (not field.null) or self.fk_min_depth > 0:
                 data = self._process_foreign_key(model_class, field,
                                                  persist_dependencies)
             else:
                 data = None
     else:
         if field_has_default_value(field):
             # datetime default can receive a function: datetime.now
             data = field.default() if callable(
                 field.default) else field.default
         elif field.null and not self.fill_nullable_fields:
             data = None
         elif field_has_choices(field):
             data = field.flatchoices[0][0]  # key of the first choice
         else:
             data = self.data_fixture.generate_data(field)
     return data