show better error message when recipe deletion breaks due to constraints
This commit is contained in:
parent
63b3887760
commit
70df8a5ffd
@ -1,4 +1,6 @@
|
|||||||
{% extends "base.html" %}
|
{% extends "base.html" %}
|
||||||
|
{% load custom_tags %}
|
||||||
|
{% load custom_tags %}
|
||||||
{% load crispy_forms_tags %}
|
{% load crispy_forms_tags %}
|
||||||
{% load i18n %}
|
{% load i18n %}
|
||||||
|
|
||||||
@ -15,12 +17,36 @@
|
|||||||
|
|
||||||
<form action="." method="post">
|
<form action="." method="post">
|
||||||
{% csrf_token %}
|
{% 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 %}
|
{% blocktrans %}Are you sure you want to delete the {{ title }}: <b>{{ object }}</b> {% endblocktrans %}
|
||||||
</div>
|
</div>
|
||||||
{{ form|crispy }}
|
{{ 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>
|
</form>
|
||||||
|
|
||||||
|
|
||||||
|
@ -26,6 +26,11 @@ def get_class(value):
|
|||||||
return value.__class__
|
return value.__class__
|
||||||
|
|
||||||
|
|
||||||
|
@register.simple_tag
|
||||||
|
def class_name(value):
|
||||||
|
return value.__class__.__name__
|
||||||
|
|
||||||
|
|
||||||
@register.simple_tag
|
@register.simple_tag
|
||||||
def delete_url(model, pk):
|
def delete_url(model, pk):
|
||||||
try:
|
try:
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
from django.contrib import messages
|
from django.contrib import messages
|
||||||
from django.db.models import ProtectedError
|
from django.db.models import ProtectedError
|
||||||
from django.http import HttpResponseRedirect
|
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.urls import reverse, reverse_lazy
|
||||||
from django.utils.translation import gettext as _
|
from django.utils.translation import gettext as _
|
||||||
from django.views.generic import DeleteView
|
from django.views.generic import DeleteView
|
||||||
@ -23,9 +23,31 @@ class RecipeDelete(GroupRequiredMixin, DeleteView):
|
|||||||
model = Recipe
|
model = Recipe
|
||||||
success_url = reverse_lazy('index')
|
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):
|
def get_context_data(self, **kwargs):
|
||||||
context = super(RecipeDelete, self).get_context_data(**kwargs)
|
context = super(RecipeDelete, self).get_context_data(**kwargs)
|
||||||
context['title'] = _("Recipe")
|
context['title'] = _("Recipe")
|
||||||
|
if 'related_objects' in kwargs:
|
||||||
|
context['related_objects'] = kwargs.pop('related_objects')
|
||||||
return context
|
return context
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user