def save_storage(self, instance, schema):
        """Save basic:json values to a Storage collection."""
        for field_schema, fields in iterate_fields(instance, schema):
            name = field_schema['name']
            value = fields[name]
            if field_schema.get('type', '').startswith('basic:json:'):
                if value and not self.pk:
                    raise ValidationError(
                        'Data object must be `created` before creating `basic:json:` fields')

                if isinstance(value, int):
                    # already in Storage
                    continue

                if isinstance(value, str):
                    file_path = self.location.get_path(filename=value)  # pylint: disable=no-member
                    if os.path.isfile(file_path):
                        try:
                            with open(file_path) as file_handler:
                                value = json.load(file_handler)
                        except json.JSONDecodeError:
                            with open(file_path) as file_handler:
                                content = file_handler.read()
                                content = content.rstrip()
                                raise ValidationError(
                                    "Value of '{}' must be a valid JSON, current: {}".format(name, content)
                                )

                storage = self.storages.create(  # pylint: disable=no-member
                    name='Storage for data id {}'.format(self.pk),
                    contributor=self.contributor,
                    json=value,
                )

                # `value` is copied by value, so `fields[name]` must be changed
                fields[name] = storage.pk
def save_storage ( self , instance , schema ) : for field_schema , fields in iterate_fields ( instance , schema ) : name = field_schema [ 'name' ] value = fields [ name ] if field_schema . get ( 'type' , '' ) . startswith ( 'basic:json:' ) : if value and not self . pk : raise ValidationError ( 'Data object must be `created` before creating `basic:json:` fields' ) if isinstance ( value , int ) : continue if isinstance ( value , str ) : file_path = self . location . get_path ( filename = value ) if os . path . isfile ( file_path ) : try : with open ( file_path ) as file_handler : value = json . load ( file_handler ) except json . JSONDecodeError : with open ( file_path ) as file_handler : content = file_handler . read ( ) content = content . rstrip ( ) raise ValidationError ( "Value of '{}' must be a valid JSON, current: {}" . format ( name , content ) ) storage = self . storages . create ( name = 'Storage for data id {}' . format ( self . pk ) , contributor = self . contributor , json = value , ) fields [ name ] = storage . pk
Save basic:json values to a Storage collection.