Skip to content

Files

hypha.apply.stream_forms.files

StreamFieldDataEncoder

Bases: DjangoJSONEncoder

default

default(o)
Source code in hypha/apply/stream_forms/files.py
def default(self, o):
    if isinstance(o, StreamFieldFile):
        return {
            "name": o.name,
            "filename": o.filename,
        }
    return super().default(o)

StreamFieldFile

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

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

url property

url

size property

size

modification_time property

modification_time

closed property

closed

is_placeholder class-attribute instance-attribute

is_placeholder = False

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

generate_filename

generate_filename()
Source code in hypha/apply/stream_forms/files.py
def generate_filename(self):
    return self.name

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