--- /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)