core: don't use try/except to catch type of view in permissions mixins

This commit is contained in:
Antoine Bartuccio 2019-10-16 19:28:32 +02:00
parent 42f5773f51
commit fe9164bfef
Signed by: klmp200
GPG Key ID: E7245548C53F904B

View File

@ -172,13 +172,13 @@ class CanEditPropMixin(View):
""" """
def dispatch(self, request, *arg, **kwargs): def dispatch(self, request, *arg, **kwargs):
try:
if hasattr(self, "get_object"):
self.object = self.get_object() self.object = self.get_object()
if can_edit_prop(self.object, request.user): if not can_edit_prop(self.object, request.user):
return super(CanEditPropMixin, self).dispatch(request, *arg, **kwargs) raise PermissionDenied
return forbidden(request) return super(CanEditPropMixin, self).dispatch(request, *arg, **kwargs)
except:
pass
# If we get here, it's a ListView # If we get here, it's a ListView
l_id = [o.id for o in self.get_queryset() if can_edit_prop(o, request.user)] l_id = [o.id for o in self.get_queryset() if can_edit_prop(o, request.user)]
if not l_id and self.get_queryset().count() != 0: if not l_id and self.get_queryset().count() != 0:
@ -201,13 +201,13 @@ class CanEditMixin(View):
""" """
def dispatch(self, request, *arg, **kwargs): def dispatch(self, request, *arg, **kwargs):
try:
if hasattr(self, "get_object"):
self.object = self.get_object() self.object = self.get_object()
if can_edit(self.object, request.user): if not can_edit(self.object, request.user):
return super(CanEditMixin, self).dispatch(request, *arg, **kwargs) raise PermissionDenied
return forbidden(request) return super(CanEditMixin, self).dispatch(request, *arg, **kwargs)
except:
pass
# If we get here, it's a ListView # If we get here, it's a ListView
l_id = [o.id for o in self.get_queryset() if can_edit(o, request.user)] l_id = [o.id for o in self.get_queryset() if can_edit(o, request.user)]
if not l_id and self.get_queryset().count() != 0: if not l_id and self.get_queryset().count() != 0:
@ -231,13 +231,12 @@ class CanViewMixin(View):
def dispatch(self, request, *arg, **kwargs): def dispatch(self, request, *arg, **kwargs):
try: if hasattr(self, "get_object"):
self.object = self.get_object() self.object = self.get_object()
if can_view(self.object, request.user): if not can_view(self.object, request.user):
return super(CanViewMixin, self).dispatch(request, *arg, **kwargs) raise PermissionDenied
return forbidden(request) return super(CanViewMixin, self).dispatch(request, *arg, **kwargs)
except:
pass
# If we get here, it's a ListView # If we get here, it's a ListView
queryset = self.get_queryset() queryset = self.get_queryset()