--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/pyams_content/component/paragraph/keynumber.py Thu Apr 05 23:05:37 2018 +0200
@@ -0,0 +1,271 @@
+#
+# Copyright (c) 2008-2015 Thierry Florac <tflorac AT ulthar.net>
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+
+__docformat__ = 'restructuredtext'
+
+
+# import standard library
+from persistent import Persistent
+
+# import interfaces
+from pyams_content.component.paragraph.interfaces import IParagraphFactory
+from pyams_content.component.paragraph.interfaces.keynumber import IKeyNumber, IKeyNumberContainer, \
+ IKeyNumberContainerTarget, KEYNUMBER_CONTAINER_KEY, IKeyNumberParagraph, KEYNUMBER_PARAGRAPH_TYPE, \
+ KEYNUMBER_PARAGRAPH_RENDERERS
+from pyams_content.features.checker.interfaces import IContentChecker, MISSING_VALUE, MISSING_LANG_VALUE, ERROR_VALUE
+from pyams_form.interfaces.form import IFormContextPermissionChecker
+from pyams_i18n.interfaces import II18n, II18nManager, INegotiator
+from zope.annotation.interfaces import IAnnotations
+from zope.lifecycleevent.interfaces import IObjectAddedEvent, IObjectModifiedEvent, IObjectRemovedEvent
+from zope.location.interfaces import ISublocations
+from zope.traversing.interfaces import ITraversable
+
+# import packages
+from pyams_catalog.utils import index_object
+from pyams_content.component.paragraph import BaseParagraph, BaseParagraphFactory, BaseParagraphContentChecker
+from pyams_content.features.checker import BaseContentChecker
+from pyams_content.features.renderer import RenderedContentMixin, IContentRenderer
+from pyams_utils.adapter import adapter_config, ContextAdapter
+from pyams_utils.registry import get_current_registry, get_utility, utility_config
+from pyams_utils.request import check_request
+from pyams_utils.traversing import get_parent
+from pyams_utils.vocabulary import vocabulary_config
+from pyramid.events import subscriber
+from zope.container.contained import Contained
+from zope.container.ordered import OrderedContainer
+from zope.interface import implementer
+from zope.lifecycleevent import ObjectCreatedEvent, ObjectModifiedEvent
+from zope.location import locate
+from zope.schema.fieldproperty import FieldProperty
+from zope.schema.vocabulary import SimpleVocabulary, SimpleTerm
+
+from pyams_content import _
+
+
+#
+# Key number class and adapters
+#
+
+@implementer(IKeyNumber)
+class KeyNumber(Persistent, Contained):
+ """Key number persistent class"""
+
+ visible = FieldProperty(IKeyNumber['visible'])
+ number = FieldProperty(IKeyNumber['number'])
+ label = FieldProperty(IKeyNumber['label'])
+ text = FieldProperty(IKeyNumber['text'])
+
+
+@adapter_config(context=IKeyNumber, provides=IFormContextPermissionChecker)
+class KeyNumberPermissionChecker(ContextAdapter):
+ """Key number permission checker"""
+
+ @property
+ def edit_permission(self):
+ content = get_parent(self.context, IKeyNumberContainerTarget)
+ return IFormContextPermissionChecker(content).edit_permission
+
+
+@subscriber(IObjectAddedEvent, context_selector=IKeyNumber)
+def handle_added_keynumber(event):
+ """Handle added key number"""
+ content = get_parent(event.object, IKeyNumberContainerTarget)
+ if content is not None:
+ get_current_registry().notify(ObjectModifiedEvent(content))
+
+
+@subscriber(IObjectModifiedEvent, context_selector=IKeyNumber)
+def handle_modified_keynumber(event):
+ """Handle modified key number"""
+ content = get_parent(event.object, IKeyNumberContainerTarget)
+ if content is not None:
+ get_current_registry().notify(ObjectModifiedEvent(content))
+
+
+@subscriber(IObjectRemovedEvent, context_selector=IKeyNumber)
+def handle_removed_keynumber(event):
+ """Handle removed key number"""
+ content = get_parent(event.object, IKeyNumberContainerTarget)
+ if content is not None:
+ get_current_registry().notify(ObjectModifiedEvent(content))
+
+
+@adapter_config(context=IKeyNumber, provides=IContentChecker)
+class KeyNumberContentChecker(BaseContentChecker):
+ """Key number content checker"""
+
+ @property
+ def label(self):
+ request = check_request()
+ return II18n(self.context).query_attribute('title', request=request)
+
+ def inner_check(self, request):
+ output = []
+ translate = request.localizer.translate
+ manager = get_parent(self.context, II18nManager)
+ if manager is not None:
+ langs = manager.get_languages()
+ else:
+ negotiator = get_utility(INegotiator)
+ langs = (negotiator.server_language, )
+ i18n = II18n(self.context)
+ for lang in langs:
+ for attr in ('label', 'text'):
+ value = i18n.get_attribute(attr, lang, request)
+ if not value:
+ field_title = translate(IKeyNumber[attr].title)
+ if len(langs) == 1:
+ output.append(translate(MISSING_VALUE).format(field=field_title))
+ else:
+ output.append(translate(MISSING_LANG_VALUE).format(field=field_title, lang=lang))
+ return output
+
+
+#
+# Key numbers container classes and adapters
+#
+
+@implementer(IKeyNumberContainer)
+class KeyNumberContainer(OrderedContainer):
+ """Key numbers container"""
+
+ last_id = 1
+
+ def append(self, value, notify=True):
+ key = str(self.last_id)
+ if not notify:
+ # pre-locate key number item to avoid multiple notifications
+ locate(value, self, key)
+ self[key] = value
+ self.last_id += 1
+ if not notify:
+ # make sure that key number item is correctly indexed
+ index_object(value)
+
+ def get_visible_items(self):
+ return filter(lambda x: IKeyNumber(x).visible, self.values())
+
+
+@adapter_config(context=IKeyNumberContainerTarget, provides=IKeyNumberContainer)
+def keynumber_container_factory(target):
+ """Key number container factory"""
+ annotations = IAnnotations(target)
+ container = annotations.get(KEYNUMBER_CONTAINER_KEY)
+ if container is None:
+ container = annotations[KEYNUMBER_CONTAINER_KEY] = KeyNumberContainer()
+ get_current_registry().notify(ObjectCreatedEvent(container))
+ locate(container, target, '++keynumbers++')
+ return container
+
+
+@adapter_config(name='keynumbers', context=IKeyNumberContainerTarget, provides=ITraversable)
+class KeyNumberContainerNamespace(ContextAdapter):
+ """Key numbers container ++keynumbers++ namespace"""
+
+ def traverse(self, name, furtherpaath=None):
+ return IKeyNumberContainer(self.context)
+
+
+@adapter_config(name='keynumbers', context=IKeyNumberContainerTarget, provides=ISublocations)
+class KeyNumberContainerSublocations(ContextAdapter):
+ """Key numbers container sub-locations adapter"""
+
+ def sublocations(self):
+ return IKeyNumberContainer(self.context).values()
+
+
+@adapter_config(name='keynumbers', context=IKeyNumberContainerTarget, provides=IContentChecker)
+class KeyNumberContainerContentChecker(BaseContentChecker):
+ """Key numbers container content checker"""
+
+ label = _("Key numbers")
+ sep = '\n'
+ weight = 200
+
+ def inner_check(self, request):
+ output = []
+ registry = request.registry
+ for keynumber in IKeyNumberContainer(self.context).values():
+ if not keynumber.visible:
+ continue
+ for name, checker in sorted(registry.getAdapters((keynumber, ), IContentChecker),
+ key=lambda x: x[1].weight):
+ output.append('- {0} ({1}):'.format(keynumber.number,
+ II18n(keynumber).query_attribute('label', request=request) or '--'))
+ output.append(checker.get_check_output(request))
+ return output
+
+
+@implementer(IKeyNumberParagraph)
+class KeyNumberParagraph(RenderedContentMixin, BaseParagraph):
+ """Key numbers paragraph"""
+
+ icon_class = 'fa-list-ol'
+ icon_hint = _("Key numbers")
+
+ renderer = FieldProperty(IKeyNumberParagraph['renderer'])
+
+
+@utility_config(name=KEYNUMBER_PARAGRAPH_TYPE, provides=IParagraphFactory)
+class KeyNumberParagraphFactory(BaseParagraphFactory):
+ """Key numbers paragraph factory"""
+
+ name = _("Key numbers paragraph")
+ content_type = KeyNumberParagraph
+
+
+@adapter_config(context=IKeyNumberParagraph, provides=IContentChecker)
+class KeyNumberParagraphContentChecker(BaseParagraphContentChecker):
+ """Key numbers paragraph content checker"""
+
+ @property
+ def label(self):
+ request = check_request()
+ translate = request.localizer.translate
+ return II18n(self.context).query_attribute('title', request) or \
+ '({0})'.format(translate(self.context.icon_hint).lower())
+
+ def inner_check(self, request):
+ output = []
+ translate = request.localizer.translate
+ manager = get_parent(self.context, II18nManager)
+ if manager is not None:
+ langs = manager.get_languages()
+ else:
+ negotiator = get_utility(INegotiator)
+ langs = (negotiator.server_language, )
+ i18n = II18n(self.context)
+ for lang in langs:
+ value = i18n.get_attribute('title', lang, request)
+ if not value:
+ field_title = translate(IKeyNumberParagraph['title'].title)
+ if len(langs) == 1:
+ output.append(translate(MISSING_VALUE).format(field=field_title))
+ else:
+ output.append(translate(MISSING_LANG_VALUE).format(field=field_title, lang=lang))
+ return output
+
+
+@vocabulary_config(name=KEYNUMBER_PARAGRAPH_RENDERERS)
+class KeyNumberParagraphRendererVocabulary(SimpleVocabulary):
+ """Key numbers paragraph renderers vocabulary"""
+
+ def __init__(self, context=None):
+ request = check_request()
+ translate = request.localizer.translate
+ registry = request.registry
+ if not IKeyNumberParagraph.providedBy(context):
+ context = KeyNumberParagraph()
+ terms = [SimpleTerm(name, title=translate(adapter.label))
+ for name, adapter in sorted(registry.getAdapters((context, request), IContentRenderer),
+ key=lambda x: x[1].weight)]
+ super(KeyNumberParagraphRendererVocabulary, self).__init__(terms)
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/pyams_content/component/paragraph/zmi/keynumber.py Thu Apr 05 23:05:37 2018 +0200
@@ -0,0 +1,398 @@
+#
+# Copyright (c) 2008-2015 Thierry Florac <tflorac AT ulthar.net>
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+
+__docformat__ = 'restructuredtext'
+
+
+# import standard library
+import json
+
+# import interfaces
+from pyams_content.component.paragraph.interfaces import IParagraphContainerTarget, IParagraphContainer, \
+ IParagraphRenderer
+from pyams_content.component.paragraph.interfaces.keynumber import KEYNUMBER_PARAGRAPH_TYPE, IKeyNumberParagraph, \
+ IKeyNumberContainer, IKeyNumberContainerTarget, IKeyNumber
+from pyams_content.component.paragraph.zmi import IParagraphContainerView
+from pyams_content.component.paragraph.zmi.interfaces import IParagraphInnerEditor
+from pyams_content.interfaces import MANAGE_CONTENT_PERMISSION
+from pyams_content.shared.common import IWfSharedContent
+from pyams_form.interfaces.form import IInnerForm, IEditFormButtons, IInnerSubForm
+from pyams_i18n.interfaces import II18n
+from pyams_skin.interfaces.viewlet import IToolbarAddingMenu, IWidgetTitleViewletManager
+from pyams_skin.layer import IPyAMSLayer
+from z3c.form.interfaces import INPUT_MODE
+from z3c.table.interfaces import IValues, IColumn
+
+# import packages
+from pyams_content.component.paragraph.keynumber import KeyNumberParagraph, KeyNumber
+from pyams_content.component.paragraph.zmi import BaseParagraphAddMenu, BaseParagraphAJAXAddForm, \
+ BaseParagraphPropertiesEditForm, BaseParagraphAJAXEditForm
+from pyams_content.features.renderer.zmi import BaseRenderedContentRenderer
+from pyams_content.features.renderer.zmi.widget import RendererFieldWidget
+from pyams_form.form import AJAXAddForm, AJAXEditForm
+from pyams_form.security import ProtectedFormObjectMixin
+from pyams_i18n.column import I18nAttrColumn
+from pyams_pagelet.pagelet import pagelet_config
+from pyams_skin.container import set_element_visibility
+from pyams_skin.table import BaseTable, SorterColumn, I18nColumn, VisibilitySwitcherColumn, TrashColumn
+from pyams_skin.viewlet.toolbar import ToolbarAction
+from pyams_utils.adapter import adapter_config, ContextRequestViewAdapter
+from pyams_utils.traversing import get_parent
+from pyams_utils.url import absolute_url
+from pyams_viewlet.viewlet import viewlet_config
+from pyams_zmi.form import AdminDialogAddForm, AdminDialogEditForm
+from pyams_zmi.zmi.table import InnerTableView
+from pyramid.decorator import reify
+from pyramid.view import view_config
+from z3c.form import field, button
+from z3c.table.column import GetAttrColumn
+from zope.interface import implementer, Interface
+
+from pyams_content import _
+
+
+class IKeyNumbersView(Interface):
+ """Key numbers view marker interface"""
+
+
+class IKeyNumbersParentForm(Interface):
+ """Key numbers parent form marker interface"""
+
+
+@viewlet_config(name='add-keynumber-paragraph.menu', context=IParagraphContainerTarget, view=IParagraphContainerView,
+ layer=IPyAMSLayer, manager=IToolbarAddingMenu, weight=600)
+class KeyNumberParagraphAddMenu(BaseParagraphAddMenu):
+ """Key number paragraph add menu"""
+
+ label = _("Key numbers...")
+ label_css_class = 'fa fa-fw fa-list-ol'
+ url = 'add-keynumber-paragraph.html'
+ paragraph_type = KEYNUMBER_PARAGRAPH_TYPE
+
+
+@pagelet_config(name='add-keynumber-paragraph.html', context=IParagraphContainerTarget, layer=IPyAMSLayer,
+ permission=MANAGE_CONTENT_PERMISSION)
+class KeyNumberParagraphAddForm(AdminDialogAddForm):
+ """Key number paragraph add form"""
+
+ legend = _("Add new key number paragraph")
+ icon_css_class = 'fa fa-fw fa-list-ol'
+
+ fields = field.Fields(IKeyNumberParagraph).select('title', 'renderer')
+ ajax_handler = 'add-keynumber-paragraph.json'
+ edit_permission = MANAGE_CONTENT_PERMISSION
+
+ def create(self, data):
+ return KeyNumberParagraph()
+
+ def add(self, object):
+ IParagraphContainer(self.context).append(object)
+
+
+@view_config(name='add-keynumber-paragraph.json', context=IParagraphContainerTarget, request_type=IPyAMSLayer,
+ permission=MANAGE_CONTENT_PERMISSION, renderer='json', xhr=True)
+class KeyNumberParagraphAJAXAddForm(BaseParagraphAJAXAddForm, KeyNumberParagraphAddForm):
+ """Key number paragraph add form, JSON renderer"""
+
+
+@pagelet_config(name='properties.html', context=IKeyNumberParagraph, layer=IPyAMSLayer,
+ permission=MANAGE_CONTENT_PERMISSION)
+class KeyNumberParagraphPropertiesEditForm(BaseParagraphPropertiesEditForm):
+ """Key number paragraph properties edit form"""
+
+ @property
+ def title(self):
+ content = get_parent(self.context, IWfSharedContent)
+ return II18n(content).query_attribute('title', request=self.request)
+
+ legend = _("Edit key number paragraph properties")
+ icon_css_class = 'fa fa-fw fa-list-ol'
+
+ fields = field.Fields(IKeyNumberParagraph).select('title', 'renderer')
+ fields['renderer'].widgetFactory = RendererFieldWidget
+
+ ajax_handler = 'properties.json'
+ edit_permission = MANAGE_CONTENT_PERMISSION
+
+
+@view_config(name='properties.json', context=IKeyNumberParagraph, request_type=IPyAMSLayer,
+ permission=MANAGE_CONTENT_PERMISSION, renderer='json', xhr=True)
+class KeyNumberParagraphPropertiesAJAXEditForm(BaseParagraphAJAXEditForm, KeyNumberParagraphPropertiesEditForm):
+ """Key number paragraph properties edit form, JSON renderer"""
+
+
+@adapter_config(context=(IKeyNumberParagraph, IPyAMSLayer), provides=IParagraphInnerEditor)
+@implementer(IInnerForm, IKeyNumbersParentForm)
+class KeyNumberParagraphInnerEditForm(KeyNumberParagraphPropertiesEditForm):
+ """Key number paragraph inner edit form"""
+
+ legend = None
+ ajax_handler = 'inner-properties.json'
+
+ @property
+ def buttons(self):
+ if self.mode == INPUT_MODE:
+ return button.Buttons(IEditFormButtons)
+ else:
+ return button.Buttons()
+
+
+@view_config(name='inner-properties.json', context=IKeyNumberParagraph, request_type=IPyAMSLayer,
+ permission=MANAGE_CONTENT_PERMISSION, renderer='json', xhr=True)
+class KeyNumberParagraphInnerAJAXEditForm(BaseParagraphAJAXEditForm, KeyNumberParagraphInnerEditForm):
+ """Key numbers paragraph inner edit form, JSON renderer"""
+
+ def get_ajax_output(self, changes):
+ output = super(KeyNumberParagraphInnerAJAXEditForm, self).get_ajax_output(changes)
+ updated = changes.get(IKeyNumberParagraph, ())
+ if 'renderer' in updated:
+ form = KeyNumberParagraphInnerEditForm(self.context, self.request)
+ form.update()
+ output.setdefault('events', []).append({
+ 'event': 'myams.refresh',
+ 'options': {
+ 'handler': 'MyAMS.skin.refreshWidget',
+ 'parent_id': '{0}_{1}_{2}'.format(
+ self.context.__class__.__name__,
+ getattr(form.getContent(), '__name__', 'noname').replace('++', ''),
+ form.id),
+ 'widget_name': form.widgets['renderer'].name,
+ 'content': form.widgets['renderer'].render()
+ }
+ })
+ return output
+
+
+#
+# Key number paragraph renderer
+#
+
+@adapter_config(context=(IKeyNumberParagraph, IPyAMSLayer), provides=IParagraphRenderer)
+class KeyNumberParagraphRenderer(BaseRenderedContentRenderer):
+ """Key number paragraph renderer"""
+
+
+#
+# Key number items table view
+#
+
+class KeyNumbersTable(ProtectedFormObjectMixin, BaseTable):
+ """Key numbers view inner table"""
+
+ @property
+ def id(self):
+ return 'keynumbers_{0}_list'.format(self.context.__name__)
+
+ hide_header = True
+ sortOn = None
+
+ @property
+ def cssClasses(self):
+ classes = ['table', 'table-bordered', 'table-striped', 'table-hover', 'table-tight']
+ permission = self.permission
+ if (not permission) or self.request.has_permission(permission, self.context):
+ classes.append('table-dnd')
+ return {'table': ' '.join(classes)}
+
+ @property
+ def data_attributes(self):
+ attributes = super(KeyNumbersTable, self).data_attributes
+ attributes['table'] = {
+ 'id': self.id,
+ 'data-ams-location': absolute_url(IKeyNumberContainer(self.context), self.request),
+ 'data-ams-tablednd-drag-handle': 'td.sorter',
+ 'data-ams-tablednd-drop-target': 'set-keynumbers-order.json',
+ 'data-ams-visibility-target': 'set-keynumber-visibility.json'
+ }
+ attributes.setdefault('tr', {}).update({
+ 'id': lambda x, col: 'keynumber_{0}::{1}'.format(get_parent(x, IKeyNumberContainerTarget).__name__,
+ x.__name__),
+ 'data-ams-delete-target': 'delete-element.json'
+ })
+ return attributes
+
+ @reify
+ def values(self):
+ return list(super(KeyNumbersTable, self).values)
+
+
+@adapter_config(context=(IKeyNumberContainerTarget, IPyAMSLayer, KeyNumbersTable), provides=IValues)
+class KeyNumbersTableValuesAdapter(ContextRequestViewAdapter):
+ """Key numbers table values adapter"""
+
+ @property
+ def values(self):
+ return IKeyNumberContainer(self.context).values()
+
+
+@adapter_config(name='sorter', context=(IKeyNumberContainerTarget, IPyAMSLayer, KeyNumbersTable), provides=IColumn)
+class KeyNumbersTableSorterColumn(ProtectedFormObjectMixin, SorterColumn):
+ """Key numbers table sorter column"""
+
+
+@view_config(name='set-keynumbers-order.json', context=IKeyNumberContainer, request_type=IPyAMSLayer,
+ permission=MANAGE_CONTENT_PERMISSION, renderer='json', xhr=True)
+def set_keynumbers_order(request):
+ """Update key numbers order"""
+ order = list(map(str, json.loads(request.params.get('names'))))
+ request.context.updateOrder(order)
+ return {'status': 'success'}
+
+
+@adapter_config(name='show-hide', context=(IKeyNumberContainerTarget, IPyAMSLayer, KeyNumbersTable),
+ provides=IColumn)
+class KeyNumbersTableShowHideColumn(ProtectedFormObjectMixin, VisibilitySwitcherColumn):
+ """Key numbers container visibility switcher column"""
+
+
+@view_config(name='set-keynumber-visibility.json', context=IKeyNumberContainer, request_type=IPyAMSLayer,
+ permission=MANAGE_CONTENT_PERMISSION, renderer='json', xhr=True)
+def set_keynumber_visibility(request):
+ """Set key number visibility"""
+ return set_element_visibility(request, IKeyNumberContainer)
+
+
+@adapter_config(name='name', context=(IKeyNumberContainerTarget, IPyAMSLayer, KeyNumbersTable), provides=IColumn)
+class KeyNumbersTableNameColumn(I18nColumn, GetAttrColumn):
+ """Key numbers table number column"""
+
+ _header = _("Number")
+ attrName = 'number'
+ weight = 10
+
+
+@adapter_config(name='label', context=(IKeyNumberContainerTarget, IPyAMSLayer, KeyNumbersTable), provides=IColumn)
+class KeyNumbersTableLabelColumn(I18nColumn, I18nAttrColumn):
+ """Key numbers table label column"""
+
+ _header = _("Label")
+ attrName = 'label'
+ weight = 20
+
+
+@adapter_config(name='text', context=(IKeyNumberContainerTarget, IPyAMSLayer, KeyNumbersTable), provides=IColumn)
+class KeyNumbersTableTextColumn(I18nColumn, I18nAttrColumn):
+ """Key numbers table text column"""
+
+ _header = _("Associated text")
+ attrName = 'text'
+ weight = 30
+
+
+@adapter_config(name='trash', context=(IKeyNumberContainerTarget, IPyAMSLayer, KeyNumbersTable), provides=IColumn)
+class KeyNumbersTableTrashColumn(ProtectedFormObjectMixin, TrashColumn):
+ """Key numbers table trash column"""
+
+
+@adapter_config(name='keynumbers', context=(IKeyNumberContainerTarget, IPyAMSLayer, IKeyNumbersParentForm),
+ provides=IInnerSubForm)
+@implementer(IKeyNumbersView)
+class KeyNumbersView(InnerTableView):
+ """Key numbers view"""
+
+ title = _("Key numbers")
+
+ table_class = KeyNumbersTable
+ weight = 110
+
+
+#
+# Key numbers forms
+#
+
+@viewlet_config(name='add-keynumber.action', context=IKeyNumberContainerTarget, layer=IPyAMSLayer, view=IKeyNumbersView,
+ manager=IWidgetTitleViewletManager, permission=MANAGE_CONTENT_PERMISSION, weight=1)
+class KeyNumberAddAction(ToolbarAction):
+ """Key number add action"""
+
+ label = _("Add keynumber")
+ label_css_class = 'fa fa-fw fa-plus'
+ url = 'add-keynumber.html'
+ modal_target = True
+
+
+@pagelet_config(name='add-keynumber.html', context=IKeyNumberContainerTarget, layer=IPyAMSLayer,
+ permission=MANAGE_CONTENT_PERMISSION)
+class KeyNumberAddForm(AdminDialogAddForm):
+ """Key number add form"""
+
+ legend = _("Add new keynumber")
+ icon_css_class = 'fa fa-fw fa-list-ol'
+
+ fields = field.Fields(IKeyNumber).omit('__parent__', '__name__', 'visible')
+ ajax_handler = 'add-keynumber.json'
+ edit_permission = MANAGE_CONTENT_PERMISSION
+
+ def create(self, data):
+ return KeyNumber()
+
+ def add(self, object):
+ IKeyNumberContainer(self.context).append(object)
+
+
+@view_config(name='add-keynumber.json', context=IKeyNumberContainerTarget, request_type=IPyAMSLayer,
+ permission=MANAGE_CONTENT_PERMISSION, renderer='json', xhr=True)
+class KeyNumberAJAXAddForm(AJAXAddForm, KeyNumberAddForm):
+ """Key number add form, JSON renderer"""
+
+ def get_ajax_output(self, changes):
+ table = KeyNumbersTable(self.context, self.request)
+ table.update()
+ return {
+ 'status': 'success',
+ 'message': self.request.localizer.translate(_("Key number was correctly added")),
+ 'events': [{
+ 'event': 'myams.refresh',
+ 'options': {
+ 'handler': 'MyAMS.skin.refreshSwitchedTable',
+ 'object_id': table.id,
+ 'table': table.render()
+ }
+ }]
+ }
+
+
+@pagelet_config(name='properties.html', context=IKeyNumber, layer=IPyAMSLayer, permission=MANAGE_CONTENT_PERMISSION)
+class KeyNumberPropertiesEditForm(AdminDialogEditForm):
+ """Key number properties edit form"""
+
+ legend = _("Edit keynumber properties")
+ icon_css_class = 'fa fa-fw fa-list-ol'
+
+ fields = field.Fields(IKeyNumber).omit('__parent__', '__name__', 'visible')
+ ajax_handler = 'properties.json'
+ edit_permission = MANAGE_CONTENT_PERMISSION
+
+
+@view_config(name='properties.json', context=IKeyNumber, request_type=IPyAMSLayer,
+ permission=MANAGE_CONTENT_PERMISSION, renderer='json', xhr=True)
+class KeyNumberPropertiesAJAXEditForm(AJAXEditForm, KeyNumberPropertiesEditForm):
+ """Key number properties edit form, JSON renderer"""
+
+ def get_ajax_output(self, changes):
+ output = super(KeyNumberPropertiesAJAXEditForm, self).get_ajax_output(changes)
+ updated = changes.get(IKeyNumber, ())
+ if updated:
+ target = get_parent(self.context, IKeyNumberContainerTarget)
+ table = KeyNumbersTable(target, self.request)
+ table.update()
+ row = table.setUpRow(self.context)
+ output.setdefault('events', []).append({
+ 'event': 'myams.refresh',
+ 'options': {
+ 'handler': 'MyAMS.skin.refreshRow',
+ 'object_id': 'keynumber_{0}::{1}'.format(target.__name__,
+ self.context.__name__),
+ 'row': table.renderRow(row)
+ }
+ })
+ return output