diff options
author | django-bot <ops@djangoproject.com> | 2022-02-03 20:24:19 +0100 |
---|---|---|
committer | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2022-02-07 20:37:05 +0100 |
commit | 9c19aff7c7561e3a82978a272ecdaad40dda5c00 (patch) | |
tree | f0506b668a013d0063e5fba3dbf4863b466713ba /django/db/models/fields/files.py | |
parent | f68fa8b45dfac545cfc4111d4e52804c86db68d3 (diff) | |
download | django-9c19aff7c7561e3a82978a272ecdaad40dda5c00.tar.gz |
Refs #33476 -- Reformatted code with Black.
Diffstat (limited to 'django/db/models/fields/files.py')
-rw-r--r-- | django/db/models/fields/files.py | 123 |
1 files changed, 75 insertions, 48 deletions
diff --git a/django/db/models/fields/files.py b/django/db/models/fields/files.py index 18900f7b85..33a1176ed6 100644 --- a/django/db/models/fields/files.py +++ b/django/db/models/fields/files.py @@ -24,7 +24,7 @@ class FieldFile(File): def __eq__(self, other): # Older code may be expecting FileField values to be simple strings. # By overriding the == operator, it can remain backwards compatibility. - if hasattr(other, 'name'): + if hasattr(other, "name"): return self.name == other.name return self.name == other @@ -37,12 +37,14 @@ class FieldFile(File): def _require_file(self): if not self: - raise ValueError("The '%s' attribute has no file associated with it." % self.field.name) + raise ValueError( + "The '%s' attribute has no file associated with it." % self.field.name + ) def _get_file(self): self._require_file() - if getattr(self, '_file', None) is None: - self._file = self.storage.open(self.name, 'rb') + if getattr(self, "_file", None) is None: + self._file = self.storage.open(self.name, "rb") return self._file def _set_file(self, file): @@ -70,13 +72,14 @@ class FieldFile(File): return self.file.size return self.storage.size(self.name) - def open(self, mode='rb'): + def open(self, mode="rb"): self._require_file() - if getattr(self, '_file', None) is None: + if getattr(self, "_file", None) is None: self.file = self.storage.open(self.name, mode) else: self.file.open(mode) return self + # open() doesn't alter the file's contents, but it does reset the pointer open.alters_data = True @@ -93,6 +96,7 @@ class FieldFile(File): # Save the object because it has changed, unless save is False if save: self.instance.save() + save.alters_data = True def delete(self, save=True): @@ -100,7 +104,7 @@ class FieldFile(File): return # Only close the file if it's already open, which we know by the # presence of self._file - if hasattr(self, '_file'): + if hasattr(self, "_file"): self.close() del self.file @@ -112,15 +116,16 @@ class FieldFile(File): if save: self.instance.save() + delete.alters_data = True @property def closed(self): - file = getattr(self, '_file', None) + file = getattr(self, "_file", None) return file is None or file.closed def close(self): - file = getattr(self, '_file', None) + file = getattr(self, "_file", None) if file is not None: file.close() @@ -129,12 +134,12 @@ class FieldFile(File): # the file's name. Everything else will be restored later, by # FileDescriptor below. return { - 'name': self.name, - 'closed': False, - '_committed': True, - '_file': None, - 'instance': self.instance, - 'field': self.field, + "name": self.name, + "closed": False, + "_committed": True, + "_file": None, + "instance": self.instance, + "field": self.field, } def __setstate__(self, state): @@ -156,6 +161,7 @@ class FileDescriptor(DeferredAttribute): >>> with open('/path/to/hello.world') as f: ... instance.file = File(f) """ + def __get__(self, instance, cls=None): if instance is None: return self @@ -198,7 +204,7 @@ class FileDescriptor(DeferredAttribute): # Finally, because of the (some would say boneheaded) way pickle works, # the underlying FieldFile might not actually itself have an associated # file. So we need to reset the details of the FieldFile in those cases. - elif isinstance(file, FieldFile) and not hasattr(file, 'field'): + elif isinstance(file, FieldFile) and not hasattr(file, "field"): file.instance = instance file.field = self.field file.storage = self.field.storage @@ -225,8 +231,10 @@ class FileField(Field): description = _("File") - def __init__(self, verbose_name=None, name=None, upload_to='', storage=None, **kwargs): - self._primary_key_set_explicitly = 'primary_key' in kwargs + def __init__( + self, verbose_name=None, name=None, upload_to="", storage=None, **kwargs + ): + self._primary_key_set_explicitly = "primary_key" in kwargs self.storage = storage or default_storage if callable(self.storage): @@ -236,11 +244,15 @@ class FileField(Field): if not isinstance(self.storage, Storage): raise TypeError( "%s.storage must be a subclass/instance of %s.%s" - % (self.__class__.__qualname__, Storage.__module__, Storage.__qualname__) + % ( + self.__class__.__qualname__, + Storage.__module__, + Storage.__qualname__, + ) ) self.upload_to = upload_to - kwargs.setdefault('max_length', 100) + kwargs.setdefault("max_length", 100) super().__init__(verbose_name, name, **kwargs) def check(self, **kwargs): @@ -254,23 +266,24 @@ class FileField(Field): if self._primary_key_set_explicitly: return [ checks.Error( - "'primary_key' is not a valid argument for a %s." % self.__class__.__name__, + "'primary_key' is not a valid argument for a %s." + % self.__class__.__name__, obj=self, - id='fields.E201', + id="fields.E201", ) ] else: return [] def _check_upload_to(self): - if isinstance(self.upload_to, str) and self.upload_to.startswith('/'): + if isinstance(self.upload_to, str) and self.upload_to.startswith("/"): return [ checks.Error( "%s's 'upload_to' argument must be a relative path, not an " "absolute path." % self.__class__.__name__, obj=self, - id='fields.E202', - hint='Remove the leading slash.', + id="fields.E202", + hint="Remove the leading slash.", ) ] else: @@ -280,9 +293,9 @@ class FileField(Field): name, path, args, kwargs = super().deconstruct() if kwargs.get("max_length") == 100: del kwargs["max_length"] - kwargs['upload_to'] = self.upload_to + kwargs["upload_to"] = self.upload_to if self.storage is not default_storage: - kwargs['storage'] = getattr(self, '_storage_callable', self.storage) + kwargs["storage"] = getattr(self, "_storage_callable", self.storage) return name, path, args, kwargs def get_internal_type(self): @@ -329,14 +342,16 @@ class FileField(Field): if data is not None: # This value will be converted to str and stored in the # database, so leaving False as-is is not acceptable. - setattr(instance, self.name, data or '') + setattr(instance, self.name, data or "") def formfield(self, **kwargs): - return super().formfield(**{ - 'form_class': forms.FileField, - 'max_length': self.max_length, - **kwargs, - }) + return super().formfield( + **{ + "form_class": forms.FileField, + "max_length": self.max_length, + **kwargs, + } + ) class ImageFileDescriptor(FileDescriptor): @@ -344,6 +359,7 @@ class ImageFileDescriptor(FileDescriptor): Just like the FileDescriptor, but for ImageFields. The only difference is assigning the width/height to the width_field/height_field, if appropriate. """ + def __set__(self, instance, value): previous_file = instance.__dict__.get(self.field.attname) super().__set__(instance, value) @@ -364,7 +380,7 @@ class ImageFileDescriptor(FileDescriptor): class ImageFieldFile(ImageFile, FieldFile): def delete(self, save=True): # Clear the image dimensions cache - if hasattr(self, '_dimensions_cache'): + if hasattr(self, "_dimensions_cache"): del self._dimensions_cache super().delete(save) @@ -374,7 +390,14 @@ class ImageField(FileField): descriptor_class = ImageFileDescriptor description = _("Image") - def __init__(self, verbose_name=None, name=None, width_field=None, height_field=None, **kwargs): + def __init__( + self, + verbose_name=None, + name=None, + width_field=None, + height_field=None, + **kwargs, + ): self.width_field, self.height_field = width_field, height_field super().__init__(verbose_name, name, **kwargs) @@ -390,11 +413,13 @@ class ImageField(FileField): except ImportError: return [ checks.Error( - 'Cannot use ImageField because Pillow is not installed.', - hint=('Get Pillow at https://pypi.org/project/Pillow/ ' - 'or run command "python -m pip install Pillow".'), + "Cannot use ImageField because Pillow is not installed.", + hint=( + "Get Pillow at https://pypi.org/project/Pillow/ " + 'or run command "python -m pip install Pillow".' + ), obj=self, - id='fields.E210', + id="fields.E210", ) ] else: @@ -403,9 +428,9 @@ class ImageField(FileField): def deconstruct(self): name, path, args, kwargs = super().deconstruct() if self.width_field: - kwargs['width_field'] = self.width_field + kwargs["width_field"] = self.width_field if self.height_field: - kwargs['height_field'] = self.height_field + kwargs["height_field"] = self.height_field return name, path, args, kwargs def contribute_to_class(self, cls, name, **kwargs): @@ -445,9 +470,9 @@ class ImageField(FileField): if not file and not force: return - dimension_fields_filled = not( - (self.width_field and not getattr(instance, self.width_field)) or - (self.height_field and not getattr(instance, self.height_field)) + dimension_fields_filled = not ( + (self.width_field and not getattr(instance, self.width_field)) + or (self.height_field and not getattr(instance, self.height_field)) ) # When both dimension fields have values, we are most likely loading # data from the database or updating an image field that already had @@ -475,7 +500,9 @@ class ImageField(FileField): setattr(instance, self.height_field, height) def formfield(self, **kwargs): - return super().formfield(**{ - 'form_class': forms.ImageField, - **kwargs, - }) + return super().formfield( + **{ + "form_class": forms.ImageField, + **kwargs, + } + ) |