Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
Websites UFRPE
Django Private Storage
Commits
d0c50d68
Commit
d0c50d68
authored
Oct 01, 2017
by
Diederik van der Boor
Browse files
document how to use PrivateStorageDetailView, add `parent_object` field.
parent
67e087aa
Changes
3
Show whitespace changes
Inline
Side-by-side
README.rst
View file @
d0c50d68
...
...
@@ -133,6 +133,32 @@ which has the following fields:
*
``
full_path
``:
the
full
file
system
path
.
*
``
exists
()``:
whether
the
file
exists
.
*
``
content_type
``:
the
HTTP
content
type
.
*
``
parent_object
``:
only
set
when
``
PrivateStorageDetailView
``
was
used
.
Retrieving
files
by
object
ID
-----------------------------
To
implement
more
object
-
based
access
permissions
,
create
a
custom
view
that
provides
the
download
.
..
code
-
block
::
python
from
private_storage
.
views
import
PrivateStorageDetailView
class
MyDocumentDownloadView
(
PrivateStorageDetailView
):
model
=
MyModel
model_file_field
=
'file'
def
get_queryset
(
self
):
#
Make
sure
only
certain
objects
can
be
accessed
.
return
super
().
get_queryset
().
filter
(...)
def
can_access_file
(
self
,
private_file
):
#
When
the
object
can
be
accessed
,
the
file
may
be
downloaded
.
#
This
overrides
PRIVATE_STORAGE_AUTH_FUNCTION
return
True
Optimizing
large
file
transfers
-------------------------------
...
...
@@ -230,6 +256,7 @@ And expose that URL:
url
(
'^private-documents2/(?P<path>.*)$'
,
views
.
MyStorageView
.
as_view
()),
]
Contributing
------------
...
...
private_storage/models.py
View file @
d0c50d68
...
...
@@ -10,10 +10,11 @@ class PrivateFile(object):
A wrapper object that describes the file that is being accessed.
"""
def
__init__
(
self
,
request
,
storage
,
relative_name
):
def
__init__
(
self
,
request
,
storage
,
relative_name
,
parent_object
=
None
):
self
.
request
=
request
self
.
storage
=
storage
# type: Storage
self
.
relative_name
=
relative_name
self
.
parent_object
=
parent_object
@
cached_property
def
full_path
(
self
):
...
...
private_storage/views.py
View file @
d0c50d68
...
...
@@ -136,6 +136,15 @@ class PrivateStorageDetailView(SingleObjectMixin, PrivateStorageView):
file
=
getattr
(
self
.
object
,
self
.
model_file_field
)
return
file
.
name
def
get_private_file
(
self
):
# Provide the parent object as well.
return
PrivateFile
(
request
=
self
.
request
,
storage
=
self
.
storage
,
relative_name
=
self
.
get_path
(),
parent_object
=
self
.
object
)
def
can_access_file
(
self
,
private_file
):
"""
The authorization rule for this view.
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment