StreamFieldFile(instance, field, *args, filename=None, storage=default_storage, **kwargs)
Bases: File
Attempts to mimic the behaviour of the bound fields in django models
see django.db.models.fields.files for the inspiration
Source code in hypha/apply/stream_forms/files.py
| def __init__(
self, instance, field, *args, filename=None, storage=default_storage, **kwargs
):
super().__init__(*args, **kwargs)
# Field is the wagtail field that the file was uploaded to
self.field = field
# Instance is the parent model object that created this file object
self.instance = instance
self.storage = storage
self.filename = filename or self.basename
self._committed = False
|
file = property(_get_file, _set_file, _del_file)
Source code in hypha/apply/stream_forms/files.py
| def read(self, chunk_size=None):
self.file.seek(0)
if chunk_size:
return super().read(chunk_size)
else:
return super().read()
|
Source code in hypha/apply/stream_forms/files.py
| def serialize(self):
return {
"url": self.url,
"filename": self.filename,
}
|
Source code in hypha/apply/stream_forms/files.py
| def open(self, mode="rb"):
if getattr(self, "_file", None) is None:
self.file = self.storage.open(self.name, mode)
else:
self.file.open(mode)
return self
|
Source code in hypha/apply/stream_forms/files.py
| def generate_filename(self):
return self.name
|
Source code in hypha/apply/stream_forms/files.py
| def save(self):
name = self.generate_filename()
name = self.storage.generate_filename(name)
if not self._committed:
self.name = self.storage.save(name, self.file)
self._committed = True
|
Source code in hypha/apply/stream_forms/files.py
| def delete(self, save=True):
if not self:
return
# Only close the file if it's already open, which we know by the
# presence of self._file
if hasattr(self, "_file"):
self.close()
del self.file
self.storage.delete(self.name)
self.name = None
self._committed = False
|
Source code in hypha/apply/stream_forms/files.py
| def close(self):
file = getattr(self, "_file", None)
if file is not None:
file.close()
|