Skip to content
This repository has been archived by the owner on Oct 18, 2022. It is now read-only.

add: handle protected error in base delete view #130

Open
wants to merge 1 commit into
base: development
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions base/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@
# standard library

# django
from django.db.models import ProtectedError
from django.conf import settings
from django.contrib import messages
from django.contrib.admin.views.decorators import staff_member_required
from django.contrib.auth.decorators import login_required
from django.contrib.auth.mixins import AccessMixin
from django.core.exceptions import ImproperlyConfigured
from django.http import HttpResponseRedirect
from django.shortcuts import get_object_or_404
from django.shortcuts import render
from django.urls import reverse
Expand Down Expand Up @@ -308,6 +311,24 @@ def get_success_url(self):
model_name = self.model.__name__.lower()
return reverse('{}_list'.format(model_name))

def post(self, request, *args, **kwargs):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

def delete, super().delete para que funcione para ambos metodos http

try:
return self.delete(request, *args, **kwargs)
except ProtectedError as error:
return self.handle_protected_error(request, error)

def handle_protected_error(self, request, error):
"""
Overwrite this method if you want to change the error message or
redirect to a different view.
"""
messages.add_message(
request,
messages.ERROR,
_("This object cannot be deleted."),
)
return HttpResponseRedirect(self.object.get_absolute_url())


class BaseRedirectView(LoginPermissionRequiredMixin, RedirectView):
login_required = True
Expand Down