Added checks in slot adding form
authorThierry Florac <thierry.florac@onf.fr>
Fri, 21 Sep 2018 18:09:43 +0200
changeset 188 2f0a1b20cf0b
parent 187 a50dc986331b
child 189 de06dad91889
Added checks in slot adding form
src/pyams_portal/zmi/layout.py
--- a/src/pyams_portal/zmi/layout.py	Fri Sep 21 18:09:11 2018 +0200
+++ b/src/pyams_portal/zmi/layout.py	Fri Sep 21 18:09:43 2018 +0200
@@ -21,10 +21,10 @@
 from pyramid.exceptions import NotFound
 from pyramid.view import view_config
 from transaction.interfaces import ITransactionManager
-from z3c.form import field, button
-from z3c.form.interfaces import IDataExtractedEvent, HIDDEN_MODE
+from z3c.form import button, field
+from z3c.form.interfaces import HIDDEN_MODE, IDataExtractedEvent
 from zope.copy import copy
-from zope.interface import implementer, Invalid, Interface
+from zope.interface import Interface, Invalid, implementer
 
 # import packages
 from pyams_cache.beaker import get_cache
@@ -34,27 +34,26 @@
 from pyams_pagelet.interfaces import IPagelet, PageletCreatedEvent
 from pyams_pagelet.pagelet import pagelet_config
 from pyams_portal import _
-from pyams_portal.interfaces import IPortalTemplate, IPortalTemplateConfiguration, ISlot, \
-    IPortletAddingInfo, IPortlet, ISlotConfiguration, IPortletPreviewer, IPortalTemplateContainer, \
-    IPortalTemplateContainerConfiguration, IPortalPortletsConfiguration, IPortalContext, IPortalPage, \
-    MANAGE_TEMPLATE_PERMISSION, LOCAL_TEMPLATE_NAME
-from pyams_portal.portlet import PORTLETS_CACHE_REGION, PORTLETS_CACHE_NAME
+from pyams_portal.interfaces import IPortalContext, IPortalPage, IPortalPortletsConfiguration, IPortalTemplate, \
+    IPortalTemplateConfiguration, IPortalTemplateContainer, IPortalTemplateContainerConfiguration, IPortlet, \
+    IPortletAddingInfo, IPortletPreviewer, ISlot, ISlotConfiguration, LOCAL_TEMPLATE_NAME, MANAGE_TEMPLATE_PERMISSION
+from pyams_portal.portlet import PORTLETS_CACHE_NAME, PORTLETS_CACHE_REGION
 from pyams_portal.zmi.template import PortalTemplateHeaderAdapter
 from pyams_skin.interfaces import IInnerPage, IPageHeader
-from pyams_skin.interfaces.viewlet import IToolbarAddingMenu, IMenuHeader, IContextActions
+from pyams_skin.interfaces.viewlet import IContextActions, IMenuHeader, IToolbarAddingMenu
 from pyams_skin.layer import IPyAMSLayer
 from pyams_skin.viewlet.menu import MenuItem
 from pyams_skin.viewlet.toolbar import JsToolbarMenuItem, ToolbarMenuDivider, ToolbarMenuItem
 from pyams_template.template import template_config
 from pyams_utils.adapter import adapter_config
-from pyams_utils.registry import query_utility, get_utility
+from pyams_utils.registry import get_utility, query_utility
 from pyams_utils.traversing import get_parent
 from pyams_utils.unicode import translate_string
 from pyams_utils.url import absolute_url
 from pyams_viewlet.manager import viewletmanager_config
 from pyams_viewlet.viewlet import viewlet_config
 from pyams_zmi.form import AdminDialogAddForm, AdminDialogEditForm
-from pyams_zmi.interfaces.menu import IPropertiesMenu, IContentManagementMenu
+from pyams_zmi.interfaces.menu import IContentManagementMenu, IPropertiesMenu
 from pyams_zmi.layer import IAdminLayer
 from pyams_zmi.view import AdminView
 
@@ -255,7 +254,10 @@
     def createAndAdd(self, data):
         data = data.get(self, data)
         config = IPortalTemplateConfiguration(self.context)
-        return config.add_slot(data.get('name'), data.get('row_id'))
+        row_id = data.get('row_id')
+        if row_id is not None:
+            row_id = row_id - 1
+        return config.add_slot(data.get('name'), row_id)
 
     def get_ajax_output(self, changes):
         return {
@@ -272,9 +274,21 @@
 def handle_new_slot_data_extraction(event):
     """Handle new slot form data extraction"""
     config = IPortalTemplateConfiguration(event.form.context)
-    name = event.data.get('name')
+    data = event.data
+    name = data.get('name')
     if name in config.slot_names:
         event.form.widgets.errors += (Invalid(_("Specified name is already used!")),)
+    row_id = data.get('row_id')
+    if row_id is not None:
+        try:
+            row_id = int(row_id)
+        except ValueError:
+            event.form.widgets.errors += (Invalid(_("Row ID must be an integer value!")),)
+        else:
+            if not 0 < row_id <= config.rows:
+                translate = event.form.request.localizer.translate
+                event.form.widgets.errors += (Invalid(translate(_("Row ID must be between 1 and {0}!")).format(
+                    config.rows)),)
 
 
 @view_config(name='set-template-slot-order.json', context=IPortalTemplate, request_type=IPyAMSLayer,