ox.apps.files
This application provides file management, including: upload, thumbnailing, access rights.
Models
- class ox.apps.files.models.file.File(*args, **kwargs)[source]
This class represent a file.
Files are looked up for a matching
FileProcessor(reading mime-type). A processors handles different tasks such as creating preview (thumbnails) or getting metadata. In order to do so, it usesregistrythat provides helpers to read mime types and register file processors.A file can be nested under a
parentfolder. IfNoneis provided, then it will be at the root of the filesystem.Each file is also attached to an
ownerthat specifies who has access to the object (usingdjango-capspermission system).At deletion, related files and previews can be deleted based on
.conf.ox_files_settings(CLEAR_FILES_ON_DELETE=Trueoption).- Access
alias of
FileAccess
- exception DoesNotExist
- exception MultipleObjectsReturned
- clear_files()[source]
Delete files from storage.
This method is used to clear storage when the model instance is deleted. It updates fields without saving model instance.
- delete(*args, clear_files=None, **kwargs)[source]
Ensure file deletion if
OX_FILES['CLEAR_FILES_ON_DELETE']orclear_filesis True.- Parameters:
*args –
forward to super’s
delete()clear_files (
bool|None) – if True or False, overrides default settings.**kwargs –
forward to super’s
delete()
- get_processor(save=True, registry=<ox.apps.files.processors.registry.Registry object>)[source]
Return processor to use for this file type.
Read mime-type from file if not already set.
- Parameters:
save – save model if mime type is updated (default is True)
registry – use this processors instead of
processors.processors
- parent_attr = 'folder'
Field name used for parenting.
- class ox.apps.files.models.file.FileQuerySet(model=None, query=None, using=None, hints=None)[source]
- ox.apps.files.models.file.file_upload_to(instance, filename)[source]
Return target upload file depending on whether the folder is synchronized or not.
- Return type:
str
- ox.apps.files.models.file.get_obfuscated_path(ext)[source]
Return an obfuscated file path with provided extension.
There is no base dir prefix such as
conf.Settings.UPLOAD_DIR, only sub-dir and path.- Return type:
str
- class ox.apps.files.models.folder.Folder(*args, **kwargs)[source]
Represent a folder in which files are stored.
There are two kind of folders:
virtual folder (default): the folder structure is only stored in the database, while file names and storage are obfuscated.
The files are saved under
conf.Settings.UPLOAD_DIRdirectory.synchronized folder: the directory structure and file name are synchronized on the file system (to be used by external tools, not for direct user’s decision).
The folders and files are saved and synchronized under
conf.Settings.SYNC_DIR.
Once a folder is a assigned a type, all its children and files will be impacted. To avoid filesystem synchronisation issues this value can not be changed afterward.
Important Notes
Updating
parent,nameandpathshould not be done manually. Instead userename()andmove_to()methods to ensure that these values are correctly set.When thoses values raise a ValidationError, user should assume that new values of the model are invalid.
- Access
alias of
FolderAccess
- exception DoesNotExist
- exception MultipleObjectsReturned
- get_sync_path(path)[source]
Return synchronization directory for the provided folder path.
- Return type:
Path
- is_sync
This attribute allows to synchronize files and folder on the file system instead of being obfuscated.
Once set, this attribute should never be changed in order to avoid desynchronization.
The folders will be put under directory following this convention:
{ox_files_settings}/{owner__uuid}/{path}.
- move_to(parent=None, name=None, save=True)[source]
Move folder into provided parent folder or root.
- Parameters:
parent (
Folder|None) – parent foldername (
str|None) – if provided rename foldersave (
bool) – save node
- on_save(fields=None)[source]
Ensure tree id, level and path are set.
It also
validate_node()and update children’s values.
- root_grants = {'ox_files.add_file': 1, 'ox_files.add_folder': 1, 'ox_files.change_file': 1, 'ox_files.change_folder': 1, 'ox_files.delete_file': 1, 'ox_files.delete_folder': 1, 'ox_files.view_file': 3, 'ox_files.view_folder': 3}
This class attribute provide the default value for grant object. It should follows the structure of
grantsfield, such as:root_grants = { "auth.view_user": 1, "app.change_mymodel": 2 }
- class ox.apps.files.models.folder.FolderQuerySet(model=None, query=None, using=None, hints=None)[source]
File processors
Processors are used to handle file manipulation such as generating thumbnails. They are registered using the ox.apps.files.processors.registry Registry instance.
- class ox.apps.files.processors.Processor[source]
A Processor handles reading file metadata, providing preview among other things.
- create_preview(path, out, size=(600, 800), force=False, **kwargs)[source]
Create a preview for the file at provided path.
- Parameters:
path (
Path) – path of the file to create a preview ofout (
Path) – output path to save previewsize (thumbnail) – defaults to configured settings.
force (
bool) – create file even if it exists
- Return type:
bool
:return boolean indicating wether file has been created
- class ox.apps.files.processors.Registry(default_processor, processors=[])[source]
Registry of all file processor classes.
This is used to determine file type and provide a processor based on it.
- default_processor = None
Default processor to use when file type is not supported by any other processor.
- populate(paths=['ox.apps.files.processors.ImageProcessor', 'ox.apps.files.processors.PDFProcessor', 'ox.apps.files.processors.LibreOfficeProcessor'])[source]
Fullfill registry using provided list of processors class path. :type paths:
list[str] :param paths: class paths (defaults toPROCESSORS)
- ox.apps.files.processors.registry = <ox.apps.files.processors.registry.Registry object>
Default registry of file processors.
ox.apps.files.apps
- class ox.apps.files.apps.AppConfig(*args, **kwargs)[source]
- icon: str = 'mdi-document-multiple-outline'
Material design icon class.
- npm_package: str | None = '@oxylus/files'
Name of the corresponding NPM package to look up for.
Defaults to app label.
- root_url: str = 'ox/files'
Provide an alternative to app label when we target application in paths.
For example Oxylus will nest template directories as
ox/core/instead ofox_core. The same happens for urls.
ox.apps.files.conf
- class ox.apps.files.conf.Settings(key, source=<LazySettings "ox.settings.dev">)[source]
- CLEAR_FILES_ON_DELETE = True
When a File is deleted from database, remove it from storage.
- FILE_SIZE_LIMIT = 15728640
Set maximum file size.
- MAGIC_BUFFER = 2048
Buffer size used by Python-Magic to read mime types.
- PREVIEW_DIR = 'ox_files/previews'
Directory in which to save previews.
- PROCESSORS = ['ox.apps.files.processors.ImageProcessor', 'ox.apps.files.processors.PDFProcessor', 'ox.apps.files.processors.LibreOfficeProcessor']
List of file processors.
- SYNC_DIR = 'ox_files/store'
Base directory in which unobfuscated files and folder are synchronized.
By default uploaded files names and directories are obfuscated on file system. There might however be some cases with wan’t them to be synchronized with it.
This settings defines the root directory for thoses folders and files, in order to synchronize with external tools (eg. FTP).
- THUMBNAIL_SIZE = (600, 800)
Size for thumbnails
- UPLOAD_DIR = 'odir/uploads'
Subdirectory in media where to upload files.
- property preview_dir: Path
Return full path to preview dir.
- property sync_dir: Path
Return full path to sync dir.
- property upload_dir: Path
Return full path to upload dir.
ox.apps.files.exceptions
ox.apps.files.filters
ox.apps.files.serializers
ox.apps.files.tasks
ox.apps.files.views
- class ox.apps.files.views.FileViewSet(**kwargs)[source]
- serializer_class
alias of
FileSerializer
- class ox.apps.files.views.FolderViewSet(**kwargs)[source]
- serializer_class
alias of
FolderSerializer