src/pyams_content/component/illustration/__init__.py
changeset 682 d093aba47072
parent 591 b694d5667d17
child 686 43ebbd99dd62
equal deleted inserted replaced
681:bb371b8a67e4 682:d093aba47072
    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 IIllustration, IIllustrationTarget, \
    19 from pyams_content.component.illustration.interfaces import IIllustration, IIllustrationTarget, \
    20     ILLUSTRATION_KEY, ILLUSTRATION_RENDERERS
    20     ILLUSTRATION_KEY, ILLUSTRATION_RENDERERS, IBasicIllustration, IBasicIllustrationTarget, BASIC_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.lifecycleevent.interfaces import IObjectAddedEvent, IObjectModifiedEvent
    24 from zope.lifecycleevent.interfaces import IObjectAddedEvent, IObjectModifiedEvent
    25 from zope.location.interfaces import ISublocations
    25 from zope.location.interfaces import ISublocations
    44 from zope.schema.fieldproperty import FieldProperty
    44 from zope.schema.fieldproperty import FieldProperty
    45 
    45 
    46 from pyams_content import _
    46 from pyams_content import _
    47 
    47 
    48 
    48 
    49 @implementer(IIllustration)
    49 @implementer(IBasicIllustration)
    50 @factory_config(provided=IIllustration)
    50 @factory_config(provided=IBasicIllustration)
    51 class Illustration(RenderedContentMixin, Persistent, Contained):
    51 class BasicIllustration(Persistent, Contained):
    52     """Illustration persistent class"""
    52     """Illustration persistent class"""
    53 
    53 
    54     _data = I18nFileProperty(IIllustration['data'])
    54     _data = I18nFileProperty(IIllustration['data'])
    55     title = FieldProperty(IIllustration['title'])
    55     title = FieldProperty(IIllustration['title'])
    56     alt_title = FieldProperty(IIllustration['alt_title'])
    56     alt_title = FieldProperty(IIllustration['alt_title'])
    57     description = FieldProperty(IIllustration['description'])
       
    58     author = FieldProperty(IIllustration['author'])
       
    59     renderer = FieldProperty(IIllustration['renderer'])
       
    60 
    57 
    61     @property
    58     @property
    62     def data(self):
    59     def data(self):
    63         return self._data
    60         return self._data
    64 
    61 
    66     def data(self, value):
    63     def data(self, value):
    67         self._data = value
    64         self._data = value
    68         for data in (self._data or {}).values():
    65         for data in (self._data or {}).values():
    69             if IImage.providedBy(data):
    66             if IImage.providedBy(data):
    70                 alsoProvides(data, IResponsiveImage)
    67                 alsoProvides(data, IResponsiveImage)
       
    68 
       
    69 
       
    70 @adapter_config(context=IBasicIllustrationTarget, provides=IIllustration)
       
    71 def basic_illustration_factory(context):
       
    72     """Basic illustration factory"""
       
    73 
       
    74     def illustration_callback(illustration):
       
    75         get_current_registry().notify(ObjectAddedEvent(illustration, context, illustration.__name__))
       
    76 
       
    77     return get_annotation_adapter(context, BASIC_ILLUSTRATION_KEY, BasicIllustration,
       
    78                                   name='++illustration++',
       
    79                                   callback=illustration_callback)
       
    80 
       
    81 
       
    82 @implementer(IIllustration)
       
    83 @factory_config(provided=IIllustration)
       
    84 class Illustration(RenderedContentMixin, BasicIllustration):
       
    85     """Illustration persistent class"""
       
    86 
       
    87     description = FieldProperty(IIllustration['description'])
       
    88     author = FieldProperty(IIllustration['author'])
       
    89     renderer = FieldProperty(IIllustration['renderer'])
    71 
    90 
    72 
    91 
    73 @adapter_config(context=IIllustrationTarget, provides=IIllustration)
    92 @adapter_config(context=IIllustrationTarget, provides=IIllustration)
    74 def illustration_factory(context):
    93 def illustration_factory(context):
    75     """Illustration factory"""
    94     """Illustration factory"""
    93             info = IFileInfo(data)
   112             info = IFileInfo(data)
    94             info.title = II18n(illustration).get_attribute('title', lang, request)
   113             info.title = II18n(illustration).get_attribute('title', lang, request)
    95             info.description = II18n(illustration).get_attribute('alt_title', lang, request)
   114             info.description = II18n(illustration).get_attribute('alt_title', lang, request)
    96 
   115 
    97 
   116 
    98 @subscriber(IObjectAddedEvent, context_selector=IIllustration)
   117 @subscriber(IObjectAddedEvent, context_selector=IBasicIllustration)
    99 def handle_added_illustration(event):
   118 def handle_added_illustration(event):
   100     """Handle added illustration"""
   119     """Handle added illustration"""
   101     illustration = event.object
   120     illustration = event.object
   102     update_illustration_properties(illustration)
   121     update_illustration_properties(illustration)
   103 
   122 
   104 
   123 
   105 @subscriber(IObjectModifiedEvent, context_selector=IIllustration)
   124 @subscriber(IObjectModifiedEvent, context_selector=IBasicIllustration)
   106 def handle_modified_illustration(event):
   125 def handle_modified_illustration(event):
   107     """Handle modified illustration"""
   126     """Handle modified illustration"""
   108     illustration = event.object
   127     illustration = event.object
   109     update_illustration_properties(illustration)
   128     update_illustration_properties(illustration)
   110 
   129 
   111 
   130 
   112 @adapter_config(name='illustration', context=IIllustrationTarget, provides=ITraversable)
   131 @adapter_config(name='illustration', context=IBasicIllustrationTarget, provides=ITraversable)
   113 class IllustrationNamespace(ContextAdapter):
   132 class IllustrationNamespace(ContextAdapter):
   114     """++illustration++ namespace adapter"""
   133     """++illustration++ namespace adapter"""
   115 
   134 
   116     def traverse(self, name, furtherpath=None):
   135     def traverse(self, name, furtherpath=None):
   117         registry = get_global_registry()
   136         registry = get_global_registry()
   118         return registry.queryAdapter(self.context, IIllustration, name=name)
   137         return registry.queryAdapter(self.context, IIllustration, name=name)
   119 
   138 
   120 
   139 
   121 @adapter_config(name='illustration', context=IIllustrationTarget, provides=ISublocations)
   140 @adapter_config(name='illustration', context=IBasicIllustrationTarget, provides=ISublocations)
   122 class IllustrationSublocations(ContextAdapter):
   141 class IllustrationSublocations(ContextAdapter):
   123     """Illustration sub-locations adapter"""
   142     """Illustration sub-locations adapter"""
   124 
   143 
   125     def sublocations(self):
   144     def sublocations(self):
   126         return IIllustration(self.context),
   145         registry = get_global_registry()
       
   146         for name, adapter in registry.getAdapters((self, ), IBasicIllustration):
       
   147             yield adapter
   127 
   148 
   128 
   149 
   129 @adapter_config(context=IIllustration, provides=IContentChecker)
   150 @adapter_config(context=IIllustration, provides=IContentChecker)
   130 class IllustrationContentChecker(BaseContentChecker):
   151 class IllustrationContentChecker(BaseContentChecker):
   131     """Illustration content checker"""
   152     """Illustration content checker"""