show better error message when recipe deletion breaks due to constraints

This commit is contained in:
vabene1111 2021-10-19 18:22:18 +02:00
parent 63b3887760
commit 70df8a5ffd
3 changed files with 59 additions and 6 deletions

View File

@ -1,4 +1,6 @@
{% extends "base.html" %}
{% load custom_tags %}
{% load custom_tags %}
{% load crispy_forms_tags %}
{% load i18n %}
@ -12,16 +14,40 @@
<h3>{% trans 'Delete' %} {{ title }}</h3>
<form action="." method="post">
{% csrf_token %}
<div class="alert alert-danger" role="alert">
<div class="alert alert-warning" role="alert">
{% blocktrans %}Are you sure you want to delete the {{ title }}: <b>{{ object }}</b> {% endblocktrans %}
</div>
{{ form|crispy }}
<button class="btn btn-success" type="submit" href="{{ success_url }}"><i class="fas fa-trash-alt"></i> {% trans 'Confirm' %}</button>
<a href="javascript:history.back()" class="btn btn-danger"><i class="fas fa-undo-alt"></i> {% trans 'Cancel' %}</a>
{% if related_objects %}
{% blocktrans %} <i>{{ object }}</i> could not be deleted because it is still referenced by the following instances: {% endblocktrans %}
<br/>
<br/>
{% for o in related_objects %}
{% class_name o.model as name %}
<h5>{{ name }}</h5>
<ul>
{% for e in o %}
<li>
<span class="badge badge-info">#{{ e.id }}</span> {{ e }}
</li>
{% endfor %}
</ul>
{% endfor %}
<br/>
{% endif %}
<button class="btn btn-success" type="submit" href="{{ success_url }}"><i
class="fas fa-trash-alt"></i> {% trans 'Confirm' %}</button>
<a href="javascript:history.back()" class="btn btn-danger"><i class="fas fa-undo-alt"></i> {% trans 'Cancel' %}
</a>
</form>
{% endblock %}

View File

@ -26,6 +26,11 @@ def get_class(value):
return value.__class__
@register.simple_tag
def class_name(value):
return value.__class__.__name__
@register.simple_tag
def delete_url(model, pk):
try:

View File

@ -1,7 +1,7 @@
from django.contrib import messages
from django.db.models import ProtectedError
from django.http import HttpResponseRedirect
from django.shortcuts import get_object_or_404
from django.shortcuts import get_object_or_404, render
from django.urls import reverse, reverse_lazy
from django.utils.translation import gettext as _
from django.views.generic import DeleteView
@ -23,9 +23,31 @@ class RecipeDelete(GroupRequiredMixin, DeleteView):
model = Recipe
success_url = reverse_lazy('index')
def delete(self, request, *args, **kwargs):
obj = self.get_object()
related_objects = []
for x in obj._meta.get_fields():
try:
related = x.related_model.objects.filter(**{x.field.name: obj})
if related.exists():
related_objects.append(related)
except AttributeError:
pass
if related_objects:
self.object = obj
return render(request, template_name=self.template_name, context=self.get_context_data(related_objects=related_objects))
success_url = self.get_success_url()
obj.delete()
return HttpResponseRedirect(success_url)
def get_context_data(self, **kwargs):
context = super(RecipeDelete, self).get_context_data(**kwargs)
context['title'] = _("Recipe")
if 'related_objects' in kwargs:
context['related_objects'] = kwargs.pop('related_objects')
return context