Prevent selection of a term as it's own parent
authorThierry Florac <tflorac@ulthar.net>
Tue, 24 Nov 2020 15:00:04 +0100
changeset 125 53d8795a704d
parent 124 db6aae9e0b8c
child 126 e76c8179e1a6
Prevent selection of a term as it's own parent
src/pyams_thesaurus/zmi/term.py
--- a/src/pyams_thesaurus/zmi/term.py	Wed Sep 02 17:34:54 2020 +0200
+++ b/src/pyams_thesaurus/zmi/term.py	Tue Nov 24 15:00:04 2020 +0100
@@ -43,7 +43,8 @@
 
 
 @viewlet_config(name='thesaurus.term.adding', context=IThesaurus, view=ThesaurusTermsView,
-                layer=IAdminLayer, manager=IWidgetTitleViewletManager, permission=MANAGE_THESAURUS_CONTENT_PERMISSION)
+                layer=IAdminLayer, manager=IWidgetTitleViewletManager,
+                permission=MANAGE_THESAURUS_CONTENT_PERMISSION)
 class ThesaurusTermAddAction(ToolbarAction):
     """Thesaurus term add action"""
 
@@ -66,8 +67,9 @@
     legend = _("Add new term")
     icon_css_class = 'fa fa-fw fa-tag'
 
-    fields = field.Fields(IThesaurusTerm).select('label', 'alt', 'definition', 'note', 'generic', 'order',
-                                                 'associations', 'usage', 'extensions', 'status', 'created')
+    fields = field.Fields(IThesaurusTerm).select('label', 'alt', 'definition', 'note', 'generic',
+                                                 'order', 'associations', 'usage', 'extensions',
+                                                 'status', 'created')
 
     ajax_handler = 'add-term.json'
     edit_permission = None
@@ -124,7 +126,8 @@
         event.form.widgets.errors += (Invalid(_("Specified label is already used!")), )
 
 
-@pagelet_config(name='properties.html', context=IThesaurusTerm, layer=IPyAMSLayer, permission=VIEW_PERMISSION)
+@pagelet_config(name='properties.html', context=IThesaurusTerm, layer=IPyAMSLayer,
+                permission=VIEW_PERMISSION)
 @ajax_config(name='properties.json', context=IThesaurusTerm, layer=IPyAMSLayer)
 class ThesaurusTermEditForm(AdminDialogEditForm):
     """Thesaurus term edit form"""
@@ -138,9 +141,10 @@
     legend = _("Edit term properties")
     icon_css_class = 'fa fa-fw fa-tag'
 
-    fields = field.Fields(IThesaurusTerm).select('label', 'alt', 'definition', 'note', 'generic', 'order', 'specifics',
-                                                 'associations', 'usage', 'used_for', 'extracts', 'extensions',
-                                                 'status', 'level', 'created', 'modified')
+    fields = field.Fields(IThesaurusTerm).select('label', 'alt', 'definition', 'note', 'generic',
+                                                 'order', 'specifics', 'associations', 'usage',
+                                                 'used_for', 'extracts', 'extensions', 'status',
+                                                 'level', 'created', 'modified')
 
     edit_permission = MANAGE_THESAURUS_CONTENT_PERMISSION
 
@@ -241,7 +245,8 @@
                 }
             }
         else:
-            if ('status' in term_changes) or ('label' in term_changes) or ('extensions' in term_changes):
+            if ('status' in term_changes) or ('label' in term_changes) or \
+                    ('extensions' in term_changes):
                 label = (self.context.generic or self.context).label.replace("'", "&#039;")
                 return {
                     'status': 'callback',
@@ -262,3 +267,7 @@
     label = event.data.get('label')
     if (label != context.label) and (label in thesaurus.terms):
         event.form.widgets.errors += (Invalid(_("Specified new label is already used!")), )
+    generic = event.data.get('generic')
+    if generic is context:
+        event.form.widgets.errors += (Invalid(_("A term can't be selected as it's own generic "
+                                                "term!")), )