PrivateStreamFieldFile(instance, field, *args, filename=None, storage=default_storage, **kwargs)
Bases: StreamFieldFile
Represents a file from a Wagtail/Hypha Stream Form block.
Aside: with imports in their usual place, there could be circular imports. Deferring or delaying import to methods
resolves the issue.
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
|
instance
instance-attribute
storage
instance-attribute
filename
instance-attribute
file
class-attribute
instance-attribute
file = property(_get_file, _set_file, _del_file)
modification_time
property
is_placeholder
class-attribute
instance-attribute
read
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()
|
serialize
Source code in hypha/apply/stream_forms/files.py
| def serialize(self):
return {
"url": self.url,
"filename": self.filename,
}
|
open
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
|
save
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
|
delete
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
|
close
Source code in hypha/apply/stream_forms/files.py
| def close(self):
file = getattr(self, "_file", None)
if file is not None:
file.close()
|
get_entity_id
Source code in hypha/apply/funds/files.py
| def get_entity_id(self):
from hypha.apply.funds.models import ApplicationRevision
from hypha.apply.projects.models import ReportVersion
entity_id = self.instance.pk
if isinstance(self.instance, ApplicationRevision):
entity_id = self.instance.submission.pk
elif isinstance(self.instance, ReportVersion):
# Reports are project documents.
entity_id = self.instance.report.project.pk
return entity_id
|
generate_filename
Source code in hypha/apply/funds/files.py
| def generate_filename(self):
from hypha.apply.projects.models import ReportVersion
path_start = "submission"
if isinstance(self.instance, ReportVersion):
path_start = "project"
return generate_private_file_path(
self.get_entity_id(),
self.field.id,
self.name,
path_start=path_start,
)
|