Skip to content

Commit

Permalink
Edited HTTP response in case of permission denied.
Browse files Browse the repository at this point in the history
Now I raise a django builtin permission denied instead of writing a custom HttpResponse
  • Loading branch information
OrsoBruno96 authored and vdboor committed Nov 4, 2019
1 parent 78fc2c5 commit 3fb3014
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions private_storage/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
"""
import os

from django.http import Http404, HttpResponse, HttpResponseForbidden
from django.http import Http404
from django.utils.module_loading import import_string
from django.views.generic import View
from django.views.generic.detail import SingleObjectMixin
from django.core.exceptions import PermissionDenied

from . import appconfig
from .models import PrivateFile
Expand Down Expand Up @@ -39,6 +40,9 @@ class PrivateStorageView(View):
#: The filename to use when :attr:`content_disposition` is set.
content_disposition_filename = None

#: Message to be displayed when the user cannot access the requested file.
permission_denied_message = "Private storage access denied"

def get_path(self):
"""
Determine the path for the object to provide.
Expand Down Expand Up @@ -70,7 +74,7 @@ def get(self, request, *args, **kwargs):
private_file = self.get_private_file()

if not self.can_access_file(private_file):
return HttpResponseForbidden('Private storage access denied')
raise PermissionDenied(self.permission_denied_message)

if not private_file.exists():
return self.serve_file_not_found(private_file)
Expand Down

0 comments on commit 3fb3014

Please sign in to comment.