src/pyams_content/component/illustration/zmi/renderer.py
changeset 452 7251fb345939
parent 451 8467cbd639b5
child 453 54b33f61a697
equal deleted inserted replaced
451:8467cbd639b5 452:7251fb345939
     1 #
       
     2 # Copyright (c) 2008-2015 Thierry Florac <tflorac AT ulthar.net>
       
     3 # All Rights Reserved.
       
     4 #
       
     5 # This software is subject to the provisions of the Zope Public License,
       
     6 # Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
       
     7 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
       
     8 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
       
     9 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
       
    10 # FOR A PARTICULAR PURPOSE.
       
    11 #
       
    12 
       
    13 __docformat__ = 'restructuredtext'
       
    14 
       
    15 
       
    16 # import standard library
       
    17 from persistent import Persistent
       
    18 
       
    19 # import interfaces
       
    20 from pyams_content.component.illustration.interfaces import IIllustration
       
    21 from pyams_content.component.illustration.zmi.interfaces import IIllustrationWithZoomSettings
       
    22 from pyams_i18n.interfaces import II18n
       
    23 from pyams_content.features.renderer.interfaces import IContentRenderer
       
    24 from pyams_skin.layer import IPyAMSLayer
       
    25 from zope.annotation.interfaces import IAnnotations
       
    26 
       
    27 # import packages
       
    28 from pyams_content.features.renderer.zmi import BaseContentRenderer
       
    29 from pyams_template.template import template_config
       
    30 from pyams_utils.adapter import adapter_config
       
    31 from zope.interface import implementer
       
    32 from zope.location import locate, Location
       
    33 from zope.schema.fieldproperty import FieldProperty
       
    34 
       
    35 from pyams_content import _
       
    36 
       
    37 
       
    38 #
       
    39 # Illustration renderers
       
    40 #
       
    41 
       
    42 class BaseIllustrationRenderer(BaseContentRenderer):
       
    43     """Base illustration renderer"""
       
    44 
       
    45     language = None
       
    46 
       
    47     def update(self):
       
    48         i18n = II18n(self.context)
       
    49         if self.language:
       
    50             self.legend = i18n.get_attribute('alt_title', self.language, request=self.request)
       
    51         else:
       
    52             self.legend = i18n.query_attribute('alt_title', request=self.request)
       
    53 
       
    54 
       
    55 @adapter_config(name='default', context=(IIllustration, IPyAMSLayer), provides=IContentRenderer)
       
    56 @template_config(template='templates/renderer-default.pt', layer=IPyAMSLayer)
       
    57 class DefaultIllustrationRenderer(BaseIllustrationRenderer):
       
    58     """Default illustration renderer"""
       
    59 
       
    60     label = _("Centered illustration")
       
    61     weight = 1
       
    62 
       
    63 
       
    64 @adapter_config(name='left+zoom', context=(IIllustration, IPyAMSLayer), provides=IContentRenderer)
       
    65 @template_config(template='templates/renderer-left.pt', layer=IPyAMSLayer)
       
    66 class LeftIllustrationWithZoomRenderer(BaseIllustrationRenderer):
       
    67     """Illustration renderer with small image and zoom"""
       
    68 
       
    69     label = _("Small illustration on the left with zoom")
       
    70     weight = 2
       
    71 
       
    72     target_interface = IIllustrationWithZoomSettings
       
    73 
       
    74 
       
    75 @adapter_config(name='right+zoom', context=(IIllustration, IPyAMSLayer), provides=IContentRenderer)
       
    76 @template_config(template='templates/renderer-right.pt', layer=IPyAMSLayer)
       
    77 class RightIllustrationWithZoomRenderer(BaseIllustrationRenderer):
       
    78     """Illustration renderer with small image and zoom"""
       
    79 
       
    80     label = _("Small illustration on the right with zoom")
       
    81     weight = 3
       
    82 
       
    83     target_interface = IIllustrationWithZoomSettings
       
    84 
       
    85 
       
    86 #
       
    87 # Illustration renderer with zoom settings
       
    88 #
       
    89 
       
    90 ILLUSTRATION_ZOOM_RENDERER_SETTINGS_KEY = 'pyams_content.illustration.renderer:zoom'
       
    91 
       
    92 
       
    93 @implementer(IIllustrationWithZoomSettings)
       
    94 class IllustrationZoomSettings(Persistent, Location):
       
    95     """Illustration zoom renderer settings"""
       
    96 
       
    97     zoom_on_click = FieldProperty(IIllustrationWithZoomSettings['zoom_on_click'])
       
    98 
       
    99 
       
   100 @adapter_config(context=IIllustration, provides=IIllustrationWithZoomSettings)
       
   101 def IllustrationWithZoomSettingsFactory(context):
       
   102     """Illustration zoom renderer settings factory"""
       
   103     annotations = IAnnotations(context)
       
   104     settings = annotations.get(ILLUSTRATION_ZOOM_RENDERER_SETTINGS_KEY)
       
   105     if settings is None:
       
   106         settings = annotations[ILLUSTRATION_ZOOM_RENDERER_SETTINGS_KEY] = IllustrationZoomSettings()
       
   107         locate(settings, context)
       
   108     return settings