Skip to content

Files

hypha.apply.funds.files

PrivateStreamFieldFile

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

field instance-attribute

field = field

instance instance-attribute

instance = instance

storage instance-attribute

storage = storage

filename instance-attribute

filename = filename or basename

basename property

basename

file class-attribute instance-attribute

file = property(_get_file, _set_file, _del_file)

path property

path

size property

size

modification_time property

modification_time

closed property

closed

is_placeholder class-attribute instance-attribute

is_placeholder = False

url property

url

read

read(chunk_size=None)
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

serialize()
Source code in hypha/apply/stream_forms/files.py
def serialize(self):
    return {
        "url": self.url,
        "filename": self.filename,
    }

open

open(mode='rb')
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

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

delete(save=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

close

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

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

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,
    )

generate_private_file_path

generate_private_file_path(entity_id, field_id, file_name, path_start='submission')
Source code in hypha/apply/funds/files.py
def generate_private_file_path(entity_id, field_id, file_name, path_start="submission"):
    path = os.path.join(path_start, str(entity_id), str(field_id))
    if file_name.startswith(path):
        return file_name

    return os.path.join(path, file_name)