File wrappers¶
A view return DownloadResponse which itself carries a file wrapper. Here are file wrappers distributed by Django and django-downloadview.
Django’s builtins¶
Django itself provides some file wrappers [1] you can use within django-downloadview:
- django.core.files.File wraps a file that live on local filesystem, initialized with a path. django-downloadview uses this wrapper in PathDownloadView.
- django.db.models.fields.files.FieldFile wraps a file that is managed in a model. django-downloadview uses this wrapper in ObjectDownloadView.
- django.core.files.base.ContentFile wraps a bytes, string or unicode object. You may use it with VirtualDownloadView.
django-downloadview builtins¶
django-downloadview implements additional file wrappers:
- StorageFile wraps a file that is managed via a storage (but not necessarily via a model). StorageDownloadView uses this wrapper.
- HTTPFile wraps a file that lives at some (remote) location, initialized with an URL. HTTPDownloadView uses this wrapper.
- VirtualFile wraps a file that lives in memory, i.e. built as a string. This is a convenient wrapper to use in VirtualDownloadView subclasses.
API reference¶
StorageFile¶
- class django_downloadview.files.StorageFile(storage, name, file=None)¶
Bases: django.core.files.base.File
A file in a Django storage.
This class looks like django.db.models.fields.files.FieldFile, but unrelated to model instance.
- file¶
Required by django.core.files.utils.FileProxy.
- open(mode='rb')¶
Retrieves the specified file from storage and return open() result.
Proxy to self.storage.open(self.name, mode).
- save(content)¶
Saves new content to the file.
Proxy to self.storage.save(self.name).
The content should be a proper File object, ready to be read from the beginning.
- path¶
Return a local filesystem path which is suitable for open().
Proxy to self.storage.path(self.name).
May raise NotImplementedError if storage doesn’t support file access with Python’s built-in open() function
- delete()¶
Delete the specified file from the storage system.
Proxy to self.storage.delete(self.name).
- exists()¶
Return True if file already exists in the storage system.
If False, then the name is available for a new file.
- size¶
Return the total size, in bytes, of the file.
Proxy to self.storage.size(self.name).
- url¶
Return an absolute URL where the file’s contents can be accessed.
Proxy to self.storage.url(self.name).
- accessed_time¶
Return the last accessed time (as datetime object) of the file.
Proxy to self.storage.accessed_time(self.name).
- created_time¶
Return the creation time (as datetime object) of the file.
Proxy to self.storage.created_time(self.name).
- modified_time¶
Return the last modification time (as datetime object) of the file.
Proxy to self.storage.modified_time(self.name).
HTTPFile¶
- class django_downloadview.files.HTTPFile(request_factory=<function get at 0x2ef02a8>, url='', name=u'', **kwargs)¶
Bases: django.core.files.base.File
Wrapper for files that live on remote HTTP servers.
Acts as a proxy.
Uses https://pypi.python.org/pypi/requests.
Always sets “stream=True” in requests kwargs.
- request¶
- file¶
- size¶
Return the total size, in bytes, of the file.
Reads response’s “content-length” header.
VirtualFile¶
- class django_downloadview.files.VirtualFile(file=None, name=u'', url='', size=None)¶
Bases: django.core.files.base.File
Wrapper for files that live in memory.
- size¶
Notes & references
[1] | https://docs.djangoproject.com/en/1.5/ref/files/file/ |