From 40c60405d4eb8ef123b5a4f5570ab8f7a23cdb8a Mon Sep 17 00:00:00 2001 From: Kagamino Date: Tue, 29 May 2018 20:34:45 +0200 Subject: [PATCH] define function that adds error to messages, to use with protectederror --- re2o/utils.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/re2o/utils.py b/re2o/utils.py index 75304369..b16bf3d5 100644 --- a/re2o/utils.py +++ b/re2o/utils.py @@ -39,6 +39,7 @@ from __future__ import unicode_literals from django.utils import timezone from django.db.models import Q from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger +from django.utils.translation import ugettext as _ from cotisations.models import Cotisation, Facture, Vente from machines.models import Interface, Machine @@ -377,3 +378,17 @@ def get_input_formats_help_text(input_formats): ) ) return help_text + + +def messages_protected_error(messages, request, item, error): + """Add an error message, specific to ProtectedError exception.""" + messages.error( + request, + _("[%(item_class_name)s] %(item_name)s can't be deleted \ + because it is used by [%(foreign_class_name)s] %(foreign_name)s.", % { + 'item_class_name': item.__class__.__name__, + 'item_name': item.name, + 'foreign_class_name': error.protected_objects[0].__class__.__name__, + 'foreign_name': error.protected_objects[0].name, + }) + )