def destroy(self, request, pk, format=None): """ Delete object """ try: try: obj = self.model.objects.get(pk=pk) except ValueError: return Response(status=status.HTTP_400_BAD_REQUEST, data={"extra": "Invalid id"}) except self.model.DoesNotExist: return Response(status=status.HTTP_204_NO_CONTENT) if nsp.has_perms(request.user, obj, "delete"): with reversion.create_revision(): if request.user: reversion.set_user(request.user) obj.delete() return Response(status=status.HTTP_204_NO_CONTENT) else: return Response(status=status.HTTP_403_FORBIDDEN) except ProtectedAction as exc: exc_message = f"{exc} - " + _( "Please contact {} to help with the deletion of this object" ).format(settings.DEFAULT_FROM_EMAIL) ticket_queue_deletion_prevented(request.user, exc.protected_object) return Response(status=status.HTTP_403_FORBIDDEN, data={"detail": exc_message}) finally: self.get_serializer().finalize_delete(request)
def destroy(self, request, pk, format=None): """ Delete object """ try: try: obj = self.model.objects.get(pk=pk) except ValueError: return Response(status=status.HTTP_400_BAD_REQUEST, data={"extra": "Invalid id"}) except self.model.DoesNotExist: return Response(status=status.HTTP_204_NO_CONTENT) user_key = get_user_key_from_request(request) org_key = get_org_key_from_request(request) if check_permissions_from_request(request, obj, "d"): with reversion.create_revision(): if request.user and request.user.is_authenticated: reversion.set_user(request.user) if org_key: reversion.set_comment(f"API-key: {org_key.prefix}") if user_key: reversion.set_comment(f"API-key: {user_key.prefix}") obj.delete() return Response(status=status.HTTP_204_NO_CONTENT) else: return Response(status=status.HTTP_403_FORBIDDEN) except ProtectedAction as exc: exc_message = f"{exc} - " + _( "Please contact {} to help with the deletion of this object" ).format(settings.DEFAULT_FROM_EMAIL) ticket_queue_deletion_prevented(request, exc.protected_object) return Response(status=status.HTTP_403_FORBIDDEN, data={"detail": exc_message}) finally: self.get_serializer().finalize_delete(request)