Beispiel #1
0
 def update(self, instance: HierarkeyProxy, validated_data):
     for attr, value in validated_data.items():
         if value is None:
             instance.delete(attr)
         elif instance.get(attr, as_type=type(value)) != value:
             instance.set(attr, value)
     return instance
Beispiel #2
0
    def update(self, instance: HierarkeyProxy, validated_data):
        for attr, value in validated_data.items():
            if isinstance(value, FieldFile):
                # Delete old file
                fname = instance.get(attr, as_type=File)
                if fname:
                    try:
                        default_storage.delete(fname.name)
                    except OSError:  # pragma: no cover
                        logger.error('Deleting file %s failed.' % fname.name)

                # Create new file
                newname = default_storage.save(self.get_new_filename(value.name), value)
                instance.set(attr, File(file=value, name=newname))
                self.changed_data.append(attr)
            elif isinstance(self.fields[attr], UploadedFileField):
                if value is None:
                    fname = instance.get(attr, as_type=File)
                    if fname:
                        try:
                            default_storage.delete(fname.name)
                        except OSError:  # pragma: no cover
                            logger.error('Deleting file %s failed.' % fname.name)
                    instance.delete(attr)
                else:
                    # file is unchanged
                    continue
            elif value is None:
                instance.delete(attr)
                self.changed_data.append(attr)
            elif instance.get(attr, as_type=type(value)) != value:
                instance.set(attr, value)
                self.changed_data.append(attr)
        return instance