--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/pyams_content/component/paragraph/milestone.py Fri Mar 09 16:53:46 2018 +0100
@@ -0,0 +1,286 @@
+#
+# 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.
+#
+from pyramid.events import subscriber
+
+__docformat__ = 'restructuredtext'
+
+
+# import standard library
+from persistent import Persistent
+
+# import interfaces
+from pyams_content.component.paragraph.interfaces import IParagraphContainerTarget, IParagraphContainer, \
+ IParagraphFactory
+from pyams_content.component.paragraph.interfaces.milestone import IMilestone, IMilestoneContainer, \
+ IMilestoneContainerTarget, MILESTONE_CONTAINER_KEY, IMilestoneParagraph, MILESTONE_PARAGRAPH_TYPE, \
+ MILESTONE_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 import IAnnotations
+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 zope.container.contained import Contained
+from zope.container.ordered import OrderedContainer
+from zope.lifecycleevent import ObjectCreatedEvent, IObjectAddedEvent, ObjectModifiedEvent, IObjectModifiedEvent, \
+ IObjectRemovedEvent
+from zope.location import locate
+from zope.interface import implementer
+from zope.schema.fieldproperty import FieldProperty
+from zope.schema.vocabulary import SimpleVocabulary, SimpleTerm
+
+from pyams_content import _
+
+
+#
+# Milestone class and adapters
+#
+
+@implementer(IMilestone)
+class Milestone(Persistent, Contained):
+ """Milestone persistent class"""
+
+ visible = FieldProperty(IMilestone['visible'])
+ title = FieldProperty(IMilestone['title'])
+ label = FieldProperty(IMilestone['label'])
+ anchor = FieldProperty(IMilestone['anchor'])
+
+
+@adapter_config(context=IMilestone, provides=IFormContextPermissionChecker)
+class MilestonePermissionChecker(ContextAdapter):
+ """Milestone permission checker"""
+
+ @property
+ def edit_permission(self):
+ content = get_parent(self.context, IMilestoneContainerTarget)
+ return IFormContextPermissionChecker(content).edit_permission
+
+
+@subscriber(IObjectAddedEvent, context_selector=IMilestone)
+def handle_added_milestone(event):
+ """Handle added milestone"""
+ content = get_parent(event.object, IMilestoneContainerTarget)
+ if content is not None:
+ get_current_registry().notify(ObjectModifiedEvent(content))
+
+
+@subscriber(IObjectModifiedEvent, context_selector=IMilestone)
+def handle_modified_milestone(event):
+ """Handle modified milestone"""
+ content = get_parent(event.object, IMilestoneContainerTarget)
+ if content is not None:
+ get_current_registry().notify(ObjectModifiedEvent(content))
+
+
+@subscriber(IObjectRemovedEvent, context_selector=IMilestone)
+def handle_removed_milestone(event):
+ """Handle removed milestone"""
+ content = get_parent(event.object, IMilestoneContainerTarget)
+ if content is not None:
+ get_current_registry().notify(ObjectModifiedEvent(content))
+
+
+@adapter_config(context=IMilestone, provides=IContentChecker)
+class MilestoneContentChecker(BaseContentChecker):
+ """Milestone 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 ('title', 'label'):
+ value = i18n.get_attribute(attr, lang, request)
+ if not value:
+ field_title = translate(IMilestone[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))
+ field_title = translate(IMilestone['anchor'].title)
+ if not self.context.anchor:
+ output.append(translate(MISSING_VALUE).format(field=field_title))
+ else:
+ target = get_parent(self.context, IParagraphContainerTarget)
+ if target is not None:
+ container = IParagraphContainer(target)
+ paragraph = container.get(self.context.anchor)
+ if paragraph is None:
+ output.append(translate(ERROR_VALUE).format(field=field_title,
+ message=translate(_("Selected paragraph is missing"))))
+ elif not paragraph.visible:
+ output.append(translate(ERROR_VALUE).format(field=field_title,
+ message=translate(_("Selected paragraph is not "
+ "visible"))))
+ return output
+
+
+#
+# Milestones container classes and adapters
+#
+
+@implementer(IMilestoneContainer)
+class MilestoneContainer(OrderedContainer):
+ """Milestones container"""
+
+ last_id = 1
+
+ def append(self, value, notify=True):
+ key = str(self.last_id)
+ if not notify:
+ # pre-locate milestone item to avoid multiple notifications
+ locate(value, self, key)
+ self[key] = value
+ self.last_id += 1
+ if not notify:
+ # make sure that milestone item is correctly indexed
+ index_object(value)
+
+ def get_visible_items(self):
+ return filter(lambda x: IMilestone(x).visible, self.values())
+
+
+@adapter_config(context=IMilestoneContainerTarget, provides=IMilestoneContainer)
+def milestone_container_factory(target):
+ """Milestone container factory"""
+ annotations = IAnnotations(target)
+ container = annotations.get(MILESTONE_CONTAINER_KEY)
+ if container is None:
+ container = annotations[MILESTONE_CONTAINER_KEY] = MilestoneContainer()
+ get_current_registry().notify(ObjectCreatedEvent(container))
+ locate(container, target, '++milestones++')
+ return container
+
+
+@adapter_config(name='milestones', context=IMilestoneContainerTarget, provides=ITraversable)
+class MilestoneContainerNamespace(ContextAdapter):
+ """Milestones container ++milestones++ namespace"""
+
+ def traverse(self, name, furtherpaath=None):
+ return IMilestoneContainer(self.context)
+
+
+@adapter_config(name='milestones', context=IMilestoneContainerTarget, provides=ISublocations)
+class MilestoneContainerSublocations(ContextAdapter):
+ """Milestones container sub-locations adapter"""
+
+ def sublocations(self):
+ return IMilestoneContainer(self.context).values()
+
+
+@adapter_config(name='milestones', context=IMilestoneContainerTarget, provides=IContentChecker)
+class MilestoneContainerContentChecker(BaseContentChecker):
+ """Milestones container content checker"""
+
+ label = _("Milestones")
+ sep = '\n'
+ weight = 200
+
+ def inner_check(self, request):
+ output = []
+ registry = request.registry
+ for milestone in IMilestoneContainer(self.context).values():
+ if not milestone.visible:
+ continue
+ for name, checker in sorted(registry.getAdapters((milestone, ), IContentChecker),
+ key=lambda x: x[1].weight):
+ output.append('- {0} :'.format(II18n(milestone).query_attribute('title', request=request)))
+ output.append(checker.get_check_output(request))
+ return output
+
+
+@implementer(IMilestoneParagraph)
+class MilestoneParagraph(RenderedContentMixin, BaseParagraph):
+ """Milestones paragraph"""
+
+ icon_class = 'fa-arrows-h'
+ icon_hint = _("Milestones")
+
+ renderer = FieldProperty(IMilestoneParagraph['renderer'])
+
+
+@utility_config(name=MILESTONE_PARAGRAPH_TYPE, provides=IParagraphFactory)
+class MilestoneParagraphFactory(BaseParagraphFactory):
+ """Milestones paragraph factory"""
+
+ name = _("Milestones paragraph")
+ content_type = MilestoneParagraph
+
+
+@adapter_config(context=IMilestoneParagraph, provides=IContentChecker)
+class MilestoneParagraphContentChecker(BaseParagraphContentChecker):
+ """Milestones 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(IMilestoneParagraph['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=MILESTONE_PARAGRAPH_RENDERERS)
+class MilestoneParagraphRendererVocabulary(SimpleVocabulary):
+ """Milestones paragraph renderers vocabulary"""
+
+ def __init__(self, context=None):
+ request = check_request()
+ translate = request.localizer.translate
+ registry = request.registry
+ if not IMilestoneParagraph.providedBy(context):
+ context = MilestoneParagraph()
+ terms = [SimpleTerm(name, title=translate(adapter.label))
+ for name, adapter in sorted(registry.getAdapters((context, request), IContentRenderer),
+ key=lambda x: x[1].weight)]
+ super(MilestoneParagraphRendererVocabulary, self).__init__(terms)
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/pyams_content/component/paragraph/zmi/milestone.py Fri Mar 09 16:53:46 2018 +0100
@@ -0,0 +1,472 @@
+#
+# 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, \
+ IParagraphPreview
+from pyams_content.component.paragraph.interfaces.milestone import MILESTONE_PARAGRAPH_TYPE, IMilestoneParagraph, \
+ IMilestoneContainer, IMilestoneContainerTarget, IMilestone
+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 pyams_utils.interfaces import MANAGE_PERMISSION
+from z3c.form.interfaces import INPUT_MODE
+from z3c.table.interfaces import IValues, IColumn
+
+# import packages
+from pyams_content.component.paragraph.milestone import MilestoneParagraph, Milestone
+from pyams_content.component.paragraph.zmi import BaseParagraphAddMenu, BaseParagraphAJAXAddForm, \
+ BaseParagraphPropertiesEditForm, BaseParagraphAJAXEditForm
+from pyams_content.features.renderer.zmi import BaseRenderedContentPreview
+from pyams_content.features.renderer.zmi.widget import RendererFieldWidget
+from pyams_content.skin import pyams_content
+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.table import BaseTable, SorterColumn, JsActionColumn, I18nColumn, TrashColumn
+from pyams_skin.viewlet.toolbar import ToolbarAction
+from pyams_template.template import template_config
+from pyams_utils.adapter import adapter_config, ContextRequestViewAdapter
+from pyams_utils.fanstatic import get_resource_path
+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, InnerAdminDisplayForm, AdminDialogEditForm
+from pyramid.decorator import reify
+from pyramid.exceptions import NotFound
+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 IMilestonesView(Interface):
+ """Milestones view marker interface"""
+
+
+class IMilestonesParentForm(Interface):
+ """Milestones parent form marker interface"""
+
+
+@viewlet_config(name='add-milestone-paragraph.menu', context=IParagraphContainerTarget, view=IParagraphContainerView,
+ layer=IPyAMSLayer, manager=IToolbarAddingMenu, weight=590)
+class MilestoneParagraphAddMenu(BaseParagraphAddMenu):
+ """Milestone paragraph add menu"""
+
+ label = _("Milestones...")
+ label_css_class = 'fa fa-fw fa-arrows-h'
+ url = 'add-milestone-paragraph.html'
+ paragraph_type = MILESTONE_PARAGRAPH_TYPE
+
+
+@pagelet_config(name='add-milestone-paragraph.html', context=IParagraphContainerTarget, layer=IPyAMSLayer,
+ permission=MANAGE_CONTENT_PERMISSION)
+class MilestoneParagraphAddForm(AdminDialogAddForm):
+ """Milestone paragraph add form"""
+
+ legend = _("Add new milestone paragraph")
+ icon_css_class = 'fa fa-fw fa-arrows-h'
+
+ fields = field.Fields(IMilestoneParagraph).select('title', 'renderer')
+ ajax_handler = 'add-milestone-paragraph.json'
+ edit_permission = MANAGE_CONTENT_PERMISSION
+
+ def create(self, data):
+ return MilestoneParagraph()
+
+ def add(self, object):
+ IParagraphContainer(self.context).append(object)
+
+
+@view_config(name='add-milestone-paragraph.json', context=IParagraphContainerTarget, request_type=IPyAMSLayer,
+ permission=MANAGE_CONTENT_PERMISSION, renderer='json', xhr=True)
+class MilestoneParagraphAJAXAddForm(BaseParagraphAJAXAddForm, MilestoneParagraphAddForm):
+ """Milestone paragraph add form, JSON renderer"""
+
+
+@pagelet_config(name='properties.html', context=IMilestoneParagraph, layer=IPyAMSLayer,
+ permission=MANAGE_CONTENT_PERMISSION)
+class MilestoneParagraphPropertiesEditForm(BaseParagraphPropertiesEditForm):
+ """Milestone 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 milestone paragraph properties")
+ icon_css_class = 'fa fa-fw fa-arrows-h'
+
+ fields = field.Fields(IMilestoneParagraph).select('title', 'renderer')
+ fields['renderer'].widgetFactory = RendererFieldWidget
+
+ ajax_handler = 'properties.json'
+ edit_permission = MANAGE_CONTENT_PERMISSION
+
+
+@view_config(name='properties.json', context=IMilestoneParagraph, request_type=IPyAMSLayer,
+ permission=MANAGE_CONTENT_PERMISSION, renderer='json', xhr=True)
+class MilestoneParagraphPropertiesAJAXEditForm(BaseParagraphAJAXEditForm, MilestoneParagraphPropertiesEditForm):
+ """Milestone paragraph properties edit form, JSON renderer"""
+
+
+@adapter_config(context=(IMilestoneParagraph, IPyAMSLayer), provides=IParagraphInnerEditor)
+@implementer(IInnerForm, IMilestonesParentForm)
+class MilestoneParagraphInnerEditForm(MilestoneParagraphPropertiesEditForm):
+ """Milestone 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=IMilestoneParagraph, request_type=IPyAMSLayer,
+ permission=MANAGE_CONTENT_PERMISSION, renderer='json', xhr=True)
+class MilestoneParagraphInnerAJAXEditForm(BaseParagraphAJAXEditForm, MilestoneParagraphInnerEditForm):
+ """Milestones paragraph inner edit form, JSON renderer"""
+
+ def get_ajax_output(self, changes):
+ output = super(MilestoneParagraphInnerAJAXEditForm, self).get_ajax_output(changes)
+ updated = changes.get(IMilestoneParagraph, ())
+ if 'renderer' in updated:
+ form = MilestoneParagraphInnerEditForm(self.context, self.request)
+ form.update()
+ output.setdefault('events', []).append({
+ 'event': 'myams.refresh',
+ 'options': {
+ 'object_id': '{0}_{1}_{2}'.format(
+ self.context.__class__.__name__,
+ getattr(form.getContent(), '__name__', 'noname').replace('++', ''),
+ form.id),
+ 'content': form.render()
+ }
+ })
+ return output
+
+
+#
+# Milestone paragraph preview
+#
+
+@adapter_config(context=(IMilestoneParagraph, IPyAMSLayer), provides=IParagraphPreview)
+class MilestoneParagraphPreview(BaseRenderedContentPreview):
+ """Milestone paragraph preview"""
+
+
+#
+# Milestone items table view
+#
+
+class MilestonesTable(ProtectedFormObjectMixin, BaseTable):
+ """Milestones view inner table"""
+
+ @property
+ def id(self):
+ return 'milestones_{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(MilestonesTable, self).data_attributes
+ attributes['table'] = {
+ 'id': self.id,
+ 'data-ams-plugins': 'pyams_content',
+ 'data-ams-plugin-pyams_content-src': get_resource_path(pyams_content),
+ 'data-ams-location': absolute_url(IMilestoneContainer(self.context), self.request),
+ 'data-ams-tablednd-drag-handle': 'td.sorter',
+ 'data-ams-tablednd-drop-target': 'set-milestones-order.json'
+ }
+ attributes.setdefault('tr', {}).update({
+ 'id': lambda x, col: 'milestone_{0}::{1}'.format(get_parent(x, IMilestoneContainerTarget).__name__,
+ x.__name__),
+ 'data-ams-delete-target': 'delete-milestone.json'
+ })
+ return attributes
+
+ @reify
+ def values(self):
+ return list(super(MilestonesTable, self).values)
+
+
+@adapter_config(context=(IMilestoneContainerTarget, IPyAMSLayer, MilestonesTable), provides=IValues)
+class MilestonesTableValuesAdapter(ContextRequestViewAdapter):
+ """Milestones table values adapter"""
+
+ @property
+ def values(self):
+ return IMilestoneContainer(self.context).values()
+
+
+@adapter_config(name='sorter', context=(IMilestoneContainerTarget, IPyAMSLayer, MilestonesTable), provides=IColumn)
+class MilestonesTableSorterColumn(ProtectedFormObjectMixin, SorterColumn):
+ """Milestones table sorter column"""
+
+
+@view_config(name='set-milestones-order.json', context=IMilestoneContainer, request_type=IPyAMSLayer,
+ permission=MANAGE_CONTENT_PERMISSION, renderer='json', xhr=True)
+def set_milestones_order(request):
+ """Update milestones order"""
+ order = list(map(str, json.loads(request.params.get('names'))))
+ request.context.updateOrder(order)
+ return {'status': 'success'}
+
+
+@adapter_config(name='show-hide', context=(IMilestoneContainerTarget, IPyAMSLayer, MilestonesTable),
+ provides=IColumn)
+class MilestonesTableShowHideColumn(ProtectedFormObjectMixin, JsActionColumn):
+ """Milestones container visibility switcher column"""
+
+ cssClasses = {'th': 'action',
+ 'td': 'action switcher'}
+
+ icon_class = 'fa fa-fw fa-eye'
+ icon_hint = _("Switch milestone visibility")
+
+ url = 'PyAMS_content.milestones.switchVisibility'
+
+ weight = 5
+
+ def get_icon(self, item):
+ if item.visible:
+ icon_class = 'fa fa-fw fa-eye'
+ else:
+ icon_class = 'fa fa-fw fa-eye-slash text-danger'
+ return '<i class="{icon_class}"></i>'.format(icon_class=icon_class)
+
+ def renderCell(self, item):
+ if self.permission and not self.request.has_permission(self.permission, context=item):
+ return self.get_icon(item)
+ else:
+ return super(MilestonesTableShowHideColumn, self).renderCell(item)
+
+
+@view_config(name='set-milestone-visibility.json', context=IMilestoneContainer, request_type=IPyAMSLayer,
+ permission=MANAGE_CONTENT_PERMISSION, renderer='json', xhr=True)
+def set_milestone_visibility(request):
+ """Set milestone visibility"""
+ container = IMilestoneContainer(request.context)
+ milestone = container.get(str(request.params.get('object_name')))
+ if milestone is None:
+ raise NotFound()
+ milestone.visible = not milestone.visible
+ return {'visible': milestone.visible}
+
+
+@adapter_config(name='name', context=(IMilestoneContainerTarget, IPyAMSLayer, MilestonesTable), provides=IColumn)
+class MilestonesTableNameColumn(I18nColumn, I18nAttrColumn):
+ """Milestones table name column"""
+
+ _header = _("Title")
+ attrName = 'title'
+ weight = 10
+
+
+@adapter_config(name='info', context=(IMilestoneContainerTarget, IPyAMSLayer, MilestonesTable), provides=IColumn)
+class MilestonesTableInfoColumn(I18nColumn, I18nAttrColumn):
+ """Milestones table information column"""
+
+ _header = _("Associated label")
+ attrName = 'label'
+ weight = 20
+
+
+@adapter_config(name='anchor', context=(IMilestoneContainerTarget, IPyAMSLayer, MilestonesTable), provides=IColumn)
+class MilestonesTableAnchorColumn(I18nColumn, GetAttrColumn):
+ """Milestones table anchor column"""
+
+ _header = _("Anchor")
+ weight = 30
+
+ def getValue(self, obj):
+ if not obj.anchor:
+ return '--'
+ target = get_parent(self.context, IParagraphContainerTarget)
+ if target is None:
+ return '--'
+ paragraph= IParagraphContainer(target).get(obj.anchor)
+ if paragraph is None:
+ return '--'
+ return II18n(paragraph).query_attribute('title', request=self.request) or '--'
+
+
+@adapter_config(name='trash', context=(IMilestoneContainerTarget, IPyAMSLayer, MilestonesTable), provides=IColumn)
+class MilestonesTableTrashColumn(ProtectedFormObjectMixin, TrashColumn):
+ """Milestones table trash column"""
+
+
+@view_config(name='delete-milestone.json', context=IMilestoneContainer, request_type=IPyAMSLayer,
+ permission=MANAGE_PERMISSION, renderer='json', xhr=True)
+def delete_milestone(request):
+ """Delete milestone"""
+ translate = request.localizer.translate
+ name = request.params.get('object_name')
+ if not name:
+ return {
+ 'status': 'message',
+ 'messagebox': {
+ 'status': 'error',
+ 'content': translate(_("No provided object_name argument!"))
+ }
+ }
+ if name not in request.context:
+ return {
+ 'status': 'message',
+ 'messagebox': {
+ 'status': 'error',
+ 'content': translate(_("Given association name doesn't exist!"))
+ }
+ }
+ del request.context[name]
+ return {'status': 'success'}
+
+
+@adapter_config(name='milestones', context=(IMilestoneContainerTarget, IPyAMSLayer, IMilestonesParentForm),
+ provides=IInnerSubForm)
+@template_config(template='templates/milestones.pt', layer=IPyAMSLayer)
+@implementer(IMilestonesView)
+class MilestonesView(InnerAdminDisplayForm):
+ """Milestones view"""
+
+ fields = field.Fields(Interface)
+ weight = 100
+
+ def __init__(self, context, request, view):
+ super(MilestonesView, self).__init__(context, request, view)
+ self.table = MilestonesTable(context, request)
+ self.table.view = self
+
+ def update(self):
+ super(MilestonesView, self).update()
+ self.table.update()
+
+
+#
+# Milestones forms
+#
+
+@viewlet_config(name='add-milestone.action', context=IMilestoneContainerTarget, layer=IPyAMSLayer, view=IMilestonesView,
+ manager=IWidgetTitleViewletManager, permission=MANAGE_CONTENT_PERMISSION, weight=1)
+class MilestoneAddAction(ToolbarAction):
+ """Milestone add action"""
+
+ label = _("Add milestone")
+ label_css_class = 'fa fa-fw fa-plus'
+ url = 'add-milestone.html'
+ modal_target = True
+
+
+@pagelet_config(name='add-milestone.html', context=IMilestoneContainerTarget, layer=IPyAMSLayer,
+ permission=MANAGE_CONTENT_PERMISSION)
+class MilestoneAddForm(AdminDialogAddForm):
+ """Milestone add form"""
+
+ legend = _("Add new milestone")
+ icon_css_class = 'fa fa-fw fa-arrow-h'
+
+ fields = field.Fields(IMilestone).omit('__parent__', '__name__', 'visible')
+ ajax_handler = 'add-milestone.json'
+ edit_permission = MANAGE_CONTENT_PERMISSION
+
+ def create(self, data):
+ return Milestone()
+
+ def add(self, object):
+ IMilestoneContainer(self.context).append(object)
+
+
+@view_config(name='add-milestone.json', context=IMilestoneContainerTarget, request_type=IPyAMSLayer,
+ permission=MANAGE_CONTENT_PERMISSION, renderer='json', xhr=True)
+class MilestoneAJAXAddForm(AJAXAddForm, MilestoneAddForm):
+ """Milestone add form, JSON renderer"""
+
+ def get_ajax_output(self, changes):
+ table = MilestonesTable(self.context, self.request)
+ table.update()
+ return {
+ 'status': 'success',
+ 'message': self.request.localizer.translate(_("Milestone was correctly added")),
+ 'events': [{
+ 'event': 'myams.refresh',
+ 'options': {
+ 'handler': 'PyAMS_content.milestones.refreshMilestones',
+ 'object_id': table.id,
+ 'table': table.render()
+ }
+ }]
+ }
+
+
+@pagelet_config(name='properties.html', context=IMilestone, layer=IPyAMSLayer, permission=MANAGE_CONTENT_PERMISSION)
+class MilestonePropertiesEditForm(AdminDialogEditForm):
+ """Milestone properties edit form"""
+
+ legend = _("Edit milestone properties")
+ icon_css_class = 'fa fa-fw fa-arrows-h'
+
+ fields = field.Fields(IMilestone).omit('__parent__', '__name__', 'visible')
+ ajax_handler = 'properties.json'
+ edit_permission = MANAGE_CONTENT_PERMISSION
+
+
+@view_config(name='properties.json', context=IMilestone, request_type=IPyAMSLayer,
+ permission=MANAGE_CONTENT_PERMISSION, renderer='json', xhr=True)
+class MilestonePropertiesAJAXEditForm(AJAXEditForm, MilestonePropertiesEditForm):
+ """Milestone properties edit form, JSON renderer"""
+
+ def get_ajax_output(self, changes):
+ output = super(MilestonePropertiesAJAXEditForm, self).get_ajax_output(changes)
+ updated = changes.get(IMilestone, ())
+ if ('title' in updated) or ('anchor' in updated):
+ target = get_parent(self.context, IMilestoneContainerTarget)
+ table = MilestonesTable(target, self.request)
+ table.update()
+ row = table.setUpRow(self.context)
+ output.setdefault('events', []).append({
+ 'event': 'myams.refresh',
+ 'options': {
+ 'handler': 'MyAMS.skin.refreshRow',
+ 'object_id': 'milestone_{0}::{1}'.format(target.__name__,
+ self.context.__name__),
+ 'row': table.renderRow(row)
+ }
+ })
+ return output