Variated.py 11.5 KB
Newer Older
Jean-Paul Smets's avatar
Jean-Paul Smets committed
1 2 3
##############################################################################
#
# Copyright (c) 2002 Nexedi SARL and Contributors. All Rights Reserved.
Jean-Paul Smets's avatar
Jean-Paul Smets committed
4
#                    Jean-Paul Smets-Solanes <jp@nexedi.com>
Jean-Paul Smets's avatar
Jean-Paul Smets committed
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 31 32
#
# 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.
#
##############################################################################

from AccessControl import ClassSecurityInfo
from Globals import InitializeClass
from Products.ERP5Type import Context, Interface, Permissions
from Products.ERP5Type.Base import Base
33
from Products.CMFCategory.Renderer import Renderer
Jean-Paul Smets's avatar
Jean-Paul Smets committed
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57

class Variated(Base):
  """
    Variated is a mix-in class for all classes which implement
    the Variated Interface.

    A Variable object is an object which can variate
    according to multiple dimensions. Variable objects include:

    - a Resource instance

    - an Amount instance (a Movement, a DeliveryLine, etc.)

    - an Item

    - a TransformedResource instance
  """

  # Declarative security
  security = ClassSecurityInfo()

  # Declarative interfaces
  __implements__ = (Interface.Variated, )

58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
  security.declareProtected(Permissions.AccessContentsInformation, 
                            'getVariationBaseCategoryList')
  def getVariationBaseCategoryList(self, omit_option_base_category=0):
    """
      Return the list of variation base category.
      If omit_option_base_category==1, do not include base category
      considered as option (ex: industrial_phase).
    """
    vbcl = self._baseGetVariationBaseCategoryList()
    if omit_option_base_category == 1:
      # XXX First implementation
      # option base category list is a portal method, until the creation
      # of a good API.
      option_base_category_list = self.getPortalOptionBaseCategoryList()
      vbcl = [x for x in vbcl if x not in option_base_category_list]
    return vbcl

75 76
  security.declareProtected(Permissions.AccessContentsInformation, 
                            '_getVariationCategoryList')
Jean-Paul Smets's avatar
Jean-Paul Smets committed
77 78 79
  def _getVariationCategoryList(self, base_category_list = ()):
    if base_category_list is ():
      base_category_list = self.getVariationRangeBaseCategoryList()
80
    return self.getAcquiredCategoryMembershipList(base_category_list, base=1)
Jean-Paul Smets's avatar
Jean-Paul Smets committed
81

82 83
  security.declareProtected(Permissions.AccessContentsInformation, 
                            'getVariationCategoryList')
84 85
  def getVariationCategoryList(self, base_category_list=(),
                               omit_option_base_category=0):
Jean-Paul Smets's avatar
Jean-Paul Smets committed
86 87 88
    """
      Returns the list of possible variations
    """
89 90
    return self._getVariationCategoryList(
                                  base_category_list=base_category_list)
Jean-Paul Smets's avatar
Jean-Paul Smets committed
91

92 93 94 95 96
  security.declareProtected(Permissions.AccessContentsInformation, 
                            'getVariationCategoryItemList')
  def getVariationCategoryItemList(self, base_category_list=(), base=1,
                                   display_id='logical_path', 
                                   display_base_category=1,
97 98
                                   current_category=None,
                                   omit_option_base_category=0,**kw):
Jean-Paul Smets's avatar
Jean-Paul Smets committed
99
    """
100
      Returns the list of possible variations
Jean-Paul Smets's avatar
Jean-Paul Smets committed
101
    """
102
    variation_category_item_list = []
103 104
    if current_category is not None:
      variation_category_item_list.append((current_category,current_category))
105 106 107

    if base_category_list is ():
      base_category_list = self.getVariationRangeBaseCategoryList()
108 109 110
      if omit_option_base_category == 1:
        base_category_list = [x for x in base_category_list if x not in
                              self.getPortalOptionBaseCategoryList()]
111 112 113 114 115 116 117 118 119
    for base_category in base_category_list:
      variation_category_list = self._getVariationCategoryList(
                                         base_category_list=[base_category])
      variation_list = map(lambda x: self.portal_categories.resolveCategory(x),
                           variation_category_list)
      variation_category_item_list.extend(Renderer(                             
                             display_base_category=display_base_category,
                             display_none_category=0, base=base,
                             current_category=current_category,
120
                             display_id=display_id,**kw).\
121 122
                                               render(variation_list))

123
    return variation_category_item_list
124 125 126 127 128 129
  
  def getVariationCategoryTitleOrIdItemList(self, base_category_list=(), base=1, **kw):
    """
    Returns a list of tuples by parsing recursively all categories in a
    given list of base categories. Uses getTitleOrId as method
    """
130
    return self.getVariationCategoryItemList(display_id='title_or_id', base_category_list=base_category_list, base=base, **kw)
Jean-Paul Smets's avatar
Jean-Paul Smets committed
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153

  security.declareProtected(Permissions.ModifyPortalContent, '_setVariationCategoryList')
  def _setVariationCategoryList(self, node_list, base_category_list = ()):
    if base_category_list is ():
      base_category_list = self.getVariationRangeBaseCategoryList()
    self._setCategoryMembership(base_category_list,node_list,base=1)

  security.declareProtected(Permissions.ModifyPortalContent, 'setVariationCategoryList')
  def setVariationCategoryList(self, node_list, base_category_list = () ):
    self._setVariationCategoryList(node_list, base_category_list = base_category_list)
    self.reindexObject()


  # Range
  security.declareProtected(Permissions.AccessContentsInformation,
                                        'getVariationRangeBaseCategoryList')
  def getVariationRangeBaseCategoryList(self):
      """
        Returns possible variation base_category ids of the
        default resource of this transformation
      """
      try:
        resource = self.getDefaultResourceValue()
Yoshinori Okuji's avatar
Yoshinori Okuji committed
154
      except AttributeError:
Jean-Paul Smets's avatar
Jean-Paul Smets committed
155 156 157 158
        resource = None
      if resource is not None:
        result = resource.getVariationBaseCategoryList()
      else:
159
        result = self.portal_categories.getBaseCategoryList()
Jean-Paul Smets's avatar
Jean-Paul Smets committed
160 161 162 163
      return result

  security.declareProtected(Permissions.AccessContentsInformation,
                                    'getVariationRangeBaseCategoryItemList')
164
  def getVariationRangeBaseCategoryItemList(self, base=1, display_id='getTitle', current_category=None):
Jean-Paul Smets's avatar
Jean-Paul Smets committed
165 166 167 168 169 170 171 172
      """
        Returns possible variations of the resource
        as a list of tuples (id, title). This is mostly
        useful in ERP5Form instances to generate selection
        menus.
      """
      return self.portal_categories.getItemList(self.getVariationRangeBaseCategoryList())

173 174
  security.declareProtected(Permissions.AccessContentsInformation,
                                    'getVariationBaseCategoryItemList')
175 176
  def getVariationBaseCategoryItemList(self, display_id='title_or_id',
                                       omit_option_base_category=0):
177 178 179 180 181 182
      """
        Returns base category of the resource
        as a list of tuples (title, id). This is mostly
        useful in ERP5Form instances to generate selection
        menus.
      """
183 184
      variation_base_category_list = self.getVariationBaseCategoryList(
          omit_option_base_category=omit_option_base_category)
185 186 187 188 189 190 191 192
      result = []
      for base_category in variation_base_category_list:
        bc = self.portal_categories.resolveCategory(base_category)
        result.extend(Renderer(display_base_category=0, 
                               display_none_category=0, base=1,
                               display_id=display_id).render([bc]))
      return result

Jean-Paul Smets's avatar
Jean-Paul Smets committed
193 194 195 196 197 198 199 200 201
  # Methods for matrix UI widgets
  security.declareProtected(Permissions.AccessContentsInformation,
                                               'getLineVariationRangeCategoryItemList')
  def getLineVariationRangeCategoryItemList(self):
    """
      Returns possible variations in line
    """
    try:
      resource = self.getDefaultResourceValue()
Yoshinori Okuji's avatar
Yoshinori Okuji committed
202
    except AttributeError:
Jean-Paul Smets's avatar
Jean-Paul Smets committed
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
      resource = None
    if resource is not None:
      clist = resource.getVariationRangeCategoryItemList(base_category_list =
                                       self.getVariationBaseCategoryLine(), root=0)
    else:
      clist = [(None,None)]
    return clist

  security.declareProtected(Permissions.AccessContentsInformation,
                                       'getColumnVariationRangeCategoryItemList')
  def getColumnVariationRangeCategoryItemList(self):
    """
      Returns possible variations in column
    """
    try:
      resource = self.getDefaultResourceValue()
Yoshinori Okuji's avatar
Yoshinori Okuji committed
219
    except AttributeError:
Jean-Paul Smets's avatar
Jean-Paul Smets committed
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
      resource = None
    if resource is not None:
      clist = resource.getVariationRangeCategoryItemList(base_category_list =
                                       self.getVariationBaseCategoryColumn(), root=0)
    else:
      clist = [(None,None)]
    return clist

  security.declareProtected(Permissions.AccessContentsInformation,
                               'getTabVariationRangeCategoryItemList')
  def getTabVariationRangeCategoryItemList(self):
    """
      Returns possible variations in tab
    """
    try:
      resource = self.getDefaultResourceValue()
Yoshinori Okuji's avatar
Yoshinori Okuji committed
236
    except AttributeError:
Jean-Paul Smets's avatar
Jean-Paul Smets committed
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
      resource = None
    if resource is not None:
      clist = resource.getVariationRangeCategoryItemList(base_category_list =
                                       self.getVariationBaseCategoryTabList(), root=0)
    else:
      clist = [(None,None)]
    return clist

  # Help
  security.declareProtected(Permissions.AccessContentsInformation,
                                        'getMatrixVariationRangeBaseCategoryList')
  def getMatrixVariationRangeBaseCategoryList(self):
    """
      Return base categories used in the matrix
    """
    line_bc= self.getVariationBaseCategoryLine()
    column_bc = self.getVariationBaseCategoryColumn()
    # We need to copy values first
    tab_bc = list(self.getVariationBaseCategoryTabList())
    result = tab_bc
    if line_bc is not None and line_bc is not '':
      result += [line_bc]
    if column_bc is not None and column_bc is not '':
      result += [column_bc]
    return result

  # Context related methods
  security.declarePublic('newVariationValue')
  def newVariationValue(self, context=None, REQUEST=None, **kw):
    # PERFORMANCE ISSUE
    from Products.ERP5.VariationValue import newVariationValue
    if context is None:
      return newVariationValue(REQUEST=REQUEST, **kw)
    else:
      return newVariationValue(context=context, REQUEST=REQUEST, **kw)

  # Provide a string representation of variations
  security.declarePublic('getVariationText')
  def getVariationText(self):
    """
      Provide a string representation of variation
    """
    category_list = list(self.getVariationCategoryList())
    category_list.sort()
    return '\n'.join(category_list)

InitializeClass(Variated)