Added property to get milestone anchor target
authorThierry Florac <thierry.florac@onf.fr>
Thu, 15 Nov 2018 17:51:43 +0100
changeset 1094 465b273c312b
parent 1093 f95d237cbd68
child 1095 6bccc6acea9d
Added property to get milestone anchor target
src/pyams_content/component/paragraph/interfaces/milestone.py
src/pyams_content/component/paragraph/milestone.py
--- a/src/pyams_content/component/paragraph/interfaces/milestone.py	Thu Nov 15 14:03:07 2018 +0100
+++ b/src/pyams_content/component/paragraph/interfaces/milestone.py	Thu Nov 15 17:51:43 2018 +0100
@@ -14,7 +14,7 @@
 
 from zope.annotation.interfaces import IAttributeAnnotatable
 from zope.container.constraints import containers, contains
-from zope.interface import Interface
+from zope.interface import Attribute, Interface
 from zope.schema import Bool, Choice
 
 from pyams_content.component.paragraph import IBaseParagraph
@@ -50,6 +50,8 @@
                     vocabulary='PyAMS content paragraphs',
                     required=False)
 
+    target = Attribute("Anchor target")
+
 
 class IMilestoneContainer(IOrderedContainer):
     """Milestones container interface"""
--- a/src/pyams_content/component/paragraph/milestone.py	Thu Nov 15 14:03:07 2018 +0100
+++ b/src/pyams_content/component/paragraph/milestone.py	Thu Nov 15 17:51:43 2018 +0100
@@ -12,8 +12,6 @@
 
 __docformat__ = 'restructuredtext'
 
-
-# import standard library
 from persistent import Persistent
 from pyramid.events import subscriber
 from zope.container.contained import Contained
@@ -26,28 +24,27 @@
 from zope.schema.fieldproperty import FieldProperty
 from zope.traversing.interfaces import ITraversable
 
-# import interfaces
 from pyams_catalog.utils import index_object
-from pyams_content import _
-from pyams_content.component.paragraph import BaseParagraph, BaseParagraphFactory, BaseParagraphContentChecker
-# import packages
-from pyams_content.component.paragraph.interfaces import IParagraphContainerTarget, IParagraphContainer, \
+from pyams_content.component.paragraph import BaseParagraph, BaseParagraphContentChecker, BaseParagraphFactory
+from pyams_content.component.paragraph.interfaces import IParagraphContainer, IParagraphContainerTarget, \
     IParagraphFactory
 from pyams_content.component.paragraph.interfaces.milestone import IMilestone, IMilestoneContainer, \
-    IMilestoneContainerTarget, MILESTONE_CONTAINER_KEY, IMilestoneParagraph, MILESTONE_PARAGRAPH_TYPE, \
-    MILESTONE_PARAGRAPH_RENDERERS, MILESTONE_PARAGRAPH_NAME
+    IMilestoneContainerTarget, IMilestoneParagraph, MILESTONE_CONTAINER_KEY, MILESTONE_PARAGRAPH_NAME, \
+    MILESTONE_PARAGRAPH_RENDERERS, MILESTONE_PARAGRAPH_TYPE
 from pyams_content.features.checker import BaseContentChecker
-from pyams_content.features.checker.interfaces import IContentChecker, MISSING_VALUE, MISSING_LANG_VALUE, ERROR_VALUE
+from pyams_content.features.checker.interfaces import ERROR_VALUE, IContentChecker, MISSING_LANG_VALUE, MISSING_VALUE
 from pyams_content.features.renderer import RenderersVocabulary
 from pyams_form.interfaces.form import IFormContextPermissionChecker
 from pyams_i18n.interfaces import II18n, II18nManager, INegotiator
-from pyams_utils.adapter import adapter_config, ContextAdapter, get_annotation_adapter
+from pyams_utils.adapter import ContextAdapter, adapter_config, get_annotation_adapter
 from pyams_utils.factory import factory_config
 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 pyams_content import _
+
 
 #
 # Milestone class and adapters
@@ -62,6 +59,12 @@
     label = FieldProperty(IMilestone['label'])
     anchor = FieldProperty(IMilestone['anchor'])
 
+    @property
+    def target(self):
+        container = get_parent(self, IParagraphContainer)
+        if container is not None:
+            return container.get(self.anchor)
+
 
 @adapter_config(context=IMilestone, provides=IFormContextPermissionChecker)
 class MilestonePermissionChecker(ContextAdapter):
@@ -114,7 +117,7 @@
             langs = manager.get_languages()
         else:
             negotiator = get_utility(INegotiator)
-            langs = (negotiator.server_language, )
+            langs = (negotiator.server_language,)
         i18n = II18n(self.context)
         for lang in langs:
             for attr in ('title', 'label'):
@@ -204,7 +207,7 @@
         for milestone in IMilestoneContainer(self.context).values():
             if not milestone.visible:
                 continue
-            for name, checker in sorted(registry.getAdapters((milestone, ), IContentChecker),
+            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))
@@ -240,7 +243,7 @@
         request = check_request()
         translate = request.localizer.translate
         return II18n(self.context).query_attribute('title', request) or \
-            '({0})'.format(translate(self.context.icon_hint).lower())
+               '({0})'.format(translate(self.context.icon_hint).lower())
 
     def inner_check(self, request):
         output = []
@@ -250,7 +253,7 @@
             langs = manager.get_languages()
         else:
             negotiator = get_utility(INegotiator)
-            langs = (negotiator.server_language, )
+            langs = (negotiator.server_language,)
         i18n = II18n(self.context)
         for lang in langs:
             value = i18n.get_attribute('title', lang, request)