Skip to content

fields#

FileNameField#

Bases: RegexField

Source code in src/apps/files/serializers/fields.py
class FileNameField(serializers.RegexField):
    default_error_messages = {"invalid": _("Expected file name to not contain slashes.")}

    def __init__(self, *args, **kwargs):
        kwargs["trim_whitespace"] = False
        super().__init__(filename_regex, *args, **kwargs)

    class Meta:
        swagger_schema_fields = {"example": "file.txt"}

FilePathField#

Bases: RegexField

Source code in src/apps/files/serializers/fields.py
class FilePathField(serializers.RegexField):
    default_error_messages = {"invalid": _("Expected file path to be in format '/path/file'.")}

    def __init__(self, *args, **kwargs):
        kwargs["trim_whitespace"] = False
        super().__init__(file_pathname_regex, *args, **kwargs)

    class Meta:
        swagger_schema_fields = {"example": "/path/file.txt"}

DirectoryPathField#

Bases: RegexField

Source code in src/apps/files/serializers/fields.py
class DirectoryPathField(serializers.RegexField):
    default_error_messages = {"invalid": _("Expected directory path to be in format /path/.")}

    def __init__(self, *args, **kwargs):
        kwargs["trim_whitespace"] = False
        super().__init__(directory_pathname_regex, *args, **kwargs)

    class Meta:
        swagger_schema_fields = {"example": "/path/"}

OptionalSlashDirectoryPathField#

Bases: RegexField

Directory path field that automatically appends slash to end if missing.

Source code in src/apps/files/serializers/fields.py
class OptionalSlashDirectoryPathField(serializers.RegexField):
    """Directory path field that automatically appends slash to end if missing."""

    default_error_messages = {
        "invalid": _("Expected directory path to be in format /path/ or /path.")
    }

    def __init__(self, *args, **kwargs):
        kwargs["trim_whitespace"] = False
        super().__init__(optional_slash_pathname_regex, *args, **kwargs)

    def to_internal_value(self, data):
        if data and not data.endswith("/"):
            data += "/"
        return super().to_internal_value(data)

StorageServiceField#

Bases: ListValidChoicesField

ChoiceField with validation for storage services.

Source code in src/apps/files/serializers/fields.py
class StorageServiceField(ListValidChoicesField):
    """ChoiceField with validation for storage services."""

    def __init__(self, *args, **kwargs):
        kwargs["choices"] = list(settings.STORAGE_SERVICE_FILE_STORAGES)
        super().__init__(*args, **kwargs)