BaobabMixin.py 5.12 KB
Newer Older
Yoshinori Okuji's avatar
Yoshinori Okuji committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
##############################################################################
#
# Copyright (c) 2006 Nexedi SARL and Contributors. All Rights Reserved.
#                    Yoshinori Okuji <yo@nexedi.com>
#
# WARNING: This program as such is intended to be used by professional
# programmers who take the whole responsability of assessing all potential
# consequences resulting from its eventual inadequacies and bugs
# End users who are looking for a ready-to-use solution with commercial
# garantees and support are strongly adviced to contract a Free Software
# Service Company
#
# This program is Free Software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#
##############################################################################

import ExtensionClass
from AccessControl import ClassSecurityInfo
31
from Acquisition import aq_base
Yoshinori Okuji's avatar
Yoshinori Okuji committed
32 33 34
from Products.ERP5Type import Permissions
from Products.ERP5Type.Accessor.Base import Method, func_code
from Products.ERP5Type.Utils import convertToMixedCase, convertToUpperCase
35
from Products.ERP5Type.Globals import InitializeClass
Yoshinori Okuji's avatar
Yoshinori Okuji committed
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111

class BaobabGetter(Method):
  """Get a category differently
  """
  _need__name__ = 1

  # Generic Definition of Method Object
  # This is required to call the method form the Web
  func_code = func_code()
  func_code.co_varnames = ('self', )
  func_code.co_argcount = 1
  func_defaults = ()

  def __init__(self, id, method_id):
    self._id = id
    self.__name__ = id
    self._method_id = method_id

  def __call__(self, instance, *args, **kw):
    portal_type = instance.getPortalType()
    skin_id = '%s_%s' % (portal_type.replace(' ', ''), self._id)
    skin = getattr(instance, skin_id, None)
    if skin is not None:
      return skin(*args, **kw)
    return getattr(instance, self._method_id)(*args, **kw)

class BaobabValueGetter(Method):
  """Get the value of a baobab category
  """
  _need__name__ = 1

  # Generic Definition of Method Object
  # This is required to call the method form the Web
  func_code = func_code()
  func_code.co_varnames = ('self', )
  func_code.co_argcount = 1
  func_defaults = ()

  def __init__(self, id, method_id):
    self._id = id
    self.__name__ = id
    self._method_id = method_id

  def __call__(self, instance, *args, **kw):
    category = getattr(instance, self._method_id)(*args, **kw)
    if category is not None:
      return instance.portal_categories.resolveCategory(category)

class BaobabPropertyGetter(Method):
  """Get the property of a baobab category
  """
  _need__name__ = 1

  # Generic Definition of Method Object
  # This is required to call the method form the Web
  func_code = func_code()
  func_code.co_varnames = ('self', )
  func_code.co_argcount = 1
  func_defaults = ()

  def __init__(self, id, method_id, property_id):
    self._id = id
    self.__name__ = id
    self._method_id = method_id
    self._property_id = property_id

  def __call__(self, instance, *args, **kw):
    value = getattr(instance, self._method_id)(*args, **kw)
    if value is not None:
      return value.getProperty(self._property_id, *args, **kw)

class BaobabMixin(ExtensionClass.Base):
  """This class provides different ways to access source, destination, etc.
  """
  security = ClassSecurityInfo()

112 113 114 115 116
  security.declareProtected(Permissions.AccessContentsInformation,
                            "getTitle")
  def getTitle(self, default=''):
    return getattr(aq_base(self), 'title', default)
    
Yoshinori Okuji's avatar
Yoshinori Okuji committed
117 118 119 120
for category in ('source', 'destination',
                 'source_section', 'destination_section',
                 'source_payment', 'destination_payment',
                 'source_function', 'destination_function',
121 122
                 'source_project', 'destination_project',
                 'source_variation_text', 'destination_variation_text',):
Yoshinori Okuji's avatar
Yoshinori Okuji committed
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
  getter_id = 'getBaobab%s' % (convertToUpperCase(category))
  original_getter_id = 'get%s' % (convertToUpperCase(category))
  method = BaobabGetter(getter_id, original_getter_id)
  setattr(BaobabMixin, getter_id, method)
  BaobabMixin.security.declareProtected(Permissions.View, getter_id)

  value_getter_id = getter_id + 'Value'
  method = BaobabValueGetter(value_getter_id, getter_id)
  setattr(BaobabMixin, value_getter_id, method)
  BaobabMixin.security.declareProtected(Permissions.View, value_getter_id)

  for prop in ('uid', 'title', 'id'):
    prop_getter_id = getter_id + convertToUpperCase(prop)
    method = BaobabPropertyGetter(prop_getter_id, value_getter_id, prop)
    setattr(BaobabMixin, prop_getter_id, method)
    BaobabMixin.security.declareProtected(Permissions.View, prop_getter_id)

140
InitializeClass(BaobabMixin)