src/pyams_content/component/illustration/__init__.py
changeset 395 2a39b333a585
parent 369 7dc78749caa2
child 409 6ad2cc0eab55
equal deleted inserted replaced
394:1ebcb03e9bff 395:2a39b333a585
    14 
    14 
    15 
    15 
    16 # import standard library
    16 # import standard library
    17 
    17 
    18 # import interfaces
    18 # import interfaces
    19 from pyams_content.component.illustration.interfaces import IIllustrationRenderer, IIllustration, IIllustrationTarget, \
    19 from pyams_content.component.illustration.interfaces import IIllustration, IIllustrationTarget, \
    20     ILLUSTRATION_KEY
    20     ILLUSTRATION_KEY
    21 from pyams_content.features.checker.interfaces import IContentChecker, MISSING_VALUE, MISSING_LANG_VALUE
    21 from pyams_content.features.checker.interfaces import IContentChecker, MISSING_VALUE, MISSING_LANG_VALUE
    22 from pyams_file.interfaces import IFileInfo, IImage, IResponsiveImage
    22 from pyams_file.interfaces import IFileInfo, IImage, IResponsiveImage
    23 from pyams_i18n.interfaces import INegotiator, II18n, II18nManager
    23 from pyams_i18n.interfaces import INegotiator, II18n, II18nManager
    24 from zope.annotation.interfaces import IAnnotations
    24 from zope.annotation.interfaces import IAnnotations
    27 from zope.traversing.interfaces import ITraversable
    27 from zope.traversing.interfaces import ITraversable
    28 
    28 
    29 # import packages
    29 # import packages
    30 from persistent import Persistent
    30 from persistent import Persistent
    31 from pyams_content.features.checker import BaseContentChecker
    31 from pyams_content.features.checker import BaseContentChecker
       
    32 from pyams_content.features.renderer import RenderedContentMixin
    32 from pyams_i18n.property import I18nFileProperty
    33 from pyams_i18n.property import I18nFileProperty
    33 from pyams_utils.adapter import adapter_config, ContextAdapter
    34 from pyams_utils.adapter import adapter_config, ContextAdapter
    34 from pyams_utils.registry import query_utility, get_utility
    35 from pyams_utils.registry import query_utility, get_utility
    35 from pyams_utils.request import check_request
    36 from pyams_utils.request import check_request
    36 from pyams_utils.traversing import get_parent
    37 from pyams_utils.traversing import get_parent
    37 from pyams_utils.vocabulary import vocabulary_config
       
    38 from pyramid.events import subscriber
    38 from pyramid.events import subscriber
    39 from pyramid.threadlocal import get_current_registry
    39 from pyramid.threadlocal import get_current_registry
    40 from zope.container.contained import Contained
    40 from zope.container.contained import Contained
    41 from zope.interface import implementer, alsoProvides
    41 from zope.interface import implementer, alsoProvides
    42 from zope.lifecycleevent import ObjectCreatedEvent, ObjectAddedEvent
    42 from zope.lifecycleevent import ObjectCreatedEvent, ObjectAddedEvent
    43 from zope.location import locate
    43 from zope.location import locate
    44 from zope.schema.fieldproperty import FieldProperty
    44 from zope.schema.fieldproperty import FieldProperty
    45 from zope.schema.vocabulary import SimpleVocabulary, SimpleTerm
       
    46 
    45 
    47 from pyams_content import _
    46 from pyams_content import _
    48 
    47 
    49 
    48 
    50 @implementer(IIllustration)
    49 @implementer(IIllustration)
    51 class Illustration(Persistent, Contained):
    50 class Illustration(Persistent, Contained, RenderedContentMixin):
    52     """Illustration persistent class"""
    51     """Illustration persistent class"""
    53 
    52 
    54     title = FieldProperty(IIllustration['title'])
    53     title = FieldProperty(IIllustration['title'])
    55     alt_title = FieldProperty(IIllustration['alt_title'])
    54     alt_title = FieldProperty(IIllustration['alt_title'])
    56     description = FieldProperty(IIllustration['description'])
    55     description = FieldProperty(IIllustration['description'])
    65         return self._data
    64         return self._data
    66 
    65 
    67     @data.setter
    66     @data.setter
    68     def data(self, value):
    67     def data(self, value):
    69         self._data = value
    68         self._data = value
    70         for data in self._data.values():
    69         for data in (self._data or {}).values():
    71             if IImage.providedBy(data):
    70             if IImage.providedBy(data):
    72                 alsoProvides(data, IResponsiveImage)
    71                 alsoProvides(data, IResponsiveImage)
    73 
    72 
    74 
    73 
    75 @adapter_config(context=IIllustrationTarget, provides=IIllustration)
    74 @adapter_config(context=IIllustrationTarget, provides=IIllustration)
   179 def IllustrationTargetContentChecker(context):
   178 def IllustrationTargetContentChecker(context):
   180     """Illustration target content checker"""
   179     """Illustration target content checker"""
   181     illustration = IIllustration(context, None)
   180     illustration = IIllustration(context, None)
   182     if illustration is not None:
   181     if illustration is not None:
   183         return IContentChecker(illustration)
   182         return IContentChecker(illustration)
   184 
       
   185 
       
   186 @vocabulary_config(name='PyAMS illustration renderers')
       
   187 class IllustrationRendererVocabulary(SimpleVocabulary):
       
   188     """Illustration renderer utilities vocabulary"""
       
   189 
       
   190     def __init__(self, context=None):
       
   191         request = check_request()
       
   192         translate = request.localizer.translate
       
   193         registry = request.registry
       
   194         context = Illustration()
       
   195         terms = [SimpleTerm(name, title=translate(adapter.label))
       
   196                  for name, adapter in sorted(registry.getAdapters((context, request), IIllustrationRenderer),
       
   197                                              key=lambda x: x[1].weight)]
       
   198         super(IllustrationRendererVocabulary, self).__init__(terms)