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
Comments
This is what I got. 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 |
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 def can_user_access_file(self, user: "User") -> bool:
return user == self.user |
Thanks a lot, it worked.
|
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:
The text was updated successfully, but these errors were encountered: