src/pyams_content/features/share/__init__.py
changeset 1138 6de2ab88b4fe
equal deleted inserted replaced
1137:b8068bb58b9e 1138:6de2ab88b4fe
       
     1 #
       
     2 # Copyright (c) 2008-2018 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 from persistent import Persistent
       
    16 from pyramid.encode import url_quote
       
    17 from zope.container.contained import Contained
       
    18 from zope.interface import implementer
       
    19 from zope.schema.fieldproperty import FieldProperty
       
    20 
       
    21 from pyams_content.features.share.interfaces import ISocialShareItem
       
    22 from pyams_content.interfaces import MANAGE_SITE_ROOT_PERMISSION
       
    23 from pyams_content.reference.pictograms import IPictogramTable
       
    24 from pyams_form.interfaces.form import IFormContextPermissionChecker
       
    25 from pyams_i18n.interfaces import II18n
       
    26 from pyams_utils.adapter import ContextAdapter, adapter_config
       
    27 from pyams_utils.registry import query_utility
       
    28 from pyams_utils.url import canonical_url
       
    29 from pyams_utils.zodb import volatile_property
       
    30 
       
    31 
       
    32 @implementer(ISocialShareItem)
       
    33 class SocialShareItem(Persistent, Contained):
       
    34     """Social network share item"""
       
    35 
       
    36     active = FieldProperty(ISocialShareItem['active'])
       
    37     label = FieldProperty(ISocialShareItem['label'])
       
    38     target_url = FieldProperty(ISocialShareItem['target_url'])
       
    39     _pictogram_name = FieldProperty(ISocialShareItem['pictogram_name'])
       
    40 
       
    41     @property
       
    42     def pictogram_name(self):
       
    43         return self._pictogram_name
       
    44 
       
    45     @pictogram_name.setter
       
    46     def pictogram_name(self, value):
       
    47         if value != self._pictogram_name:
       
    48             self._pictogram_name = value
       
    49             del self.pictogram
       
    50 
       
    51     @volatile_property
       
    52     def pictogram(self):
       
    53         table = query_utility(IPictogramTable)
       
    54         if table is not None:
       
    55             return table.get(self._pictogram_name)
       
    56 
       
    57     def get_url(self, context, request):
       
    58         url = II18n(self).query_attribute('target_url', request=request)
       
    59         return url.format(url=url_quote(canonical_url(context, request)),
       
    60                           title=url_quote(II18n(context).query_attribute('title', request=request)))
       
    61 
       
    62 
       
    63 @adapter_config(context=ISocialShareItem, provides=IFormContextPermissionChecker)
       
    64 class SocialShareItemPermissionChecker(ContextAdapter):
       
    65     """Social share item permission checker"""
       
    66 
       
    67     edit_permission = MANAGE_SITE_ROOT_PERMISSION