Reputation: 311
I am serving my Django-Wagtail media files trough Amazon S3 and now I reached a point where I need to define a customized Document Class which creates "restricted" documents (only accesible if you are logged). This documents will have a special access that will say to my S3 bucket "hey! just deliver this files If they are requested from this "foo_url" but not from anywhere else", since they will be shown to logged users only. I thought of this to prevent restricted urls to spread out.
That's why I am trying to define this Wagtail Document class to be stored in a subfolder of /documents/ and just tell Amazon what to do with that subfolder.
dummy_code_that_doesnt_work:
class RestrictedDocument(Document):
def get_upload_to(self, filename):
folder_name = 'restricted'
filename = self.file.field.storage.get_valid_name(filename)
return os.path.join(folder_name, filename)
"""
Snippet containing restricted documents
"""
@register_snippet
@python_2_unicode_compatible # provide equivalent __unicode__ and __str__ methods on Python 2
class FooSnipet(models.Model):
rectricted_document_1 = models.ForeignKey(
'RestrictedDocument',
null=True,
blank=True,
on_delete=models.SET_NULL,
related_name='+',
)
rectricted_document_2 = models.ForeignKey(
'RestrictedDocument',
null=True,
blank=True,
on_delete=models.SET_NULL,
related_name='+',
)
....
and many more
....
Maybe I am overcomplicating all this and there is another way of doing. Any suggestion will be super welcome! Thank you veeery much :)
Upvotes: 3
Views: 1618
Reputation: 25292
The Document
model doesn't support a get_upload_to
method like images do. However, as of Django 1.10 it's possible to override the file
field of AbstractDocument
:
from wagtail.wagtaildocs.models import AbstractDocument
class RestrictedDocument(AbstractDocument):
file = models.FileField(upload_to='restricted', verbose_name=_('file'))
I'm not sure that this will help much, though - the views in the Wagtail admin that handle document uploads have no way of knowing that they should save the document through the RestrictedDocument
model, rather than the default Document class.
Implementing view restrictions on documents within Wagtail is currently a work in progress (https://github.com/wagtail/wagtail/pull/3245, https://github.com/wagtail/wagtail/issues/1420).
Upvotes: 2