Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to retrieve files by object id? #60

Open
Myzel394 opened this issue Oct 4, 2020 · 3 comments
Open

How to retrieve files by object id? #60

Myzel394 opened this issue Oct 4, 2020 · 3 comments

Comments

@Myzel394
Copy link

Myzel394 commented Oct 4, 2020

Sadly the documentation doesn't cover how to actually implement your own retrieve class.

I tried many solutions, but nothing worked. It would be nice if someone could add a more detailed explanation to the readme or explain it at least here.

Here are my tries, so you don't have to waste your time by trying out none working solutions:

    path("private-media/", MaterialDownloadView.as_view(), name="serve_private_file"),
    path("private-media/", include(private_storage.urls)),
    url(r'(?P<path>.*)', MaterialDownloadView.as_view(), name="serve_private_file"),
    path("private-media/", include(private_storage.urls)),
    url(r'(.*)(?P<path>.*)', MaterialDownloadView.as_view(), name="serve_private_file"),
    path("private-media/", include(private_storage.urls)),
    path("private-media/", MaterialDownloadView.as_view()),
    path("private-media/", include(private_storage.urls)),
@digitalw
Copy link

digitalw commented Apr 6, 2021

This is what I got.
in URLS: path('file/<int:pk>/', FileView.as_view(), name='file-view'),
and the view:

   class FileView(PrivateStorageDetailView):
       model = SecuredFile
       model_file_field = 'file'
       def can_access_file(self, private_file):
           user =private_file.request.user 
           ...

the model is:

    class SecuredFile(models.Model):
        uploaded_at = models.DateTimeField(auto_now_add=True)
        file = PrivateFileField("File")

Hope that helps

@Myzel394
Copy link
Author

Myzel394 commented Apr 6, 2021

Thank you @digitalw! Totally forgot I opened this issue. :D I ended up using this:

settings.py

PRIVATE_STORAGE_AUTH_FUNCTION = "path.to.private_storage_access_check"

my storage check function

storage_models = [
    Material, Submission
]


def private_storage_access_check(private_file: PrivateFile) -> bool:
    user = private_file.request.user
    
    if not user.is_authenticated:
        return False
    
    relative_path = Path(private_file.full_path).relative_to(settings.MEDIA_ROOT)
    
    for model in storage_models:
        for instance in model.objects.only("file").filter(file=relative_path):
            if instance.can_user_access_file(user):
                return True
    
    return False

and on the models I defined a can_user_access_file(user: User) -> bool method, here an example:

def can_user_access_file(self, user: "User") -> bool:
    return user == self.user

@aayardev
Copy link

aayardev commented Sep 15, 2021

Thanks a lot, it worked.
For windows users, replace '\' with '/', use this :

      relative_path = Path(private_file.full_path).relative_to(settings.MEDIA_ROOT)
      relative_path = str(relative_path).replace("\\","/")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants