CPSDocumentPatch.py 7.28 KB
Newer Older
Sebastien Robin's avatar
Sebastien Robin committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
# (C) Copyright 2004 Nexedi SARL <http://nexedi.com>
# Authors: Sebastien Robin <seb@nexedi.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as published
# by the Free Software Foundation.
#
# 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 Products.CPSDocument.CPSDocument import CPSDocument
from Products.CPSSchemas.BasicFields import CPSImageField, CPSFileField, CPSDateTimeField
Sebastien Robin's avatar
Sebastien Robin committed
21
from Products.CPSSchemas.BasicFields import CPSStringField, CPSIntField
Sebastien Robin's avatar
Sebastien Robin committed
22
from Products.ERP5Type.Base import Base
23 24 25 26
from Products.ERP5Type.Utils import UpperCase
from Acquisition import aq_base, aq_inner
from AccessControl import ClassSecurityInfo
from Products.CMFCore.CMFCorePermissions import View
27
from zLOG import LOG
Sebastien Robin's avatar
Sebastien Robin committed
28 29 30

class PatchedCPSDocument(CPSDocument):

31
  security = ClassSecurityInfo()
Sebastien Robin's avatar
Sebastien Robin committed
32

33
  security.declareProtected( View, '_propertyMap' )
34 35 36 37 38
  def _propertyMap(self):
    """
      Returns fake property sheet
    """
    property_sheet = []
39 40 41 42 43 44
    property_sheet.append(
      {
        'id'    :   'layout_and_schema',
        'type'  :   'object'
      }
      )
Sebastien Robin's avatar
Sebastien Robin committed
45 46 47 48 49 50
    type_info = self.getTypeInfo()
    field_list = []
    if type_info is not None:
      data_model = type_info.getDataModel(self)
      if data_model is not None:
        field_list = data_model._fields.items()
51 52 53 54 55 56 57 58 59 60 61 62 63
    field_list.sort()
    for (prop_id,field) in field_list:
      #for field in schema.objectValues():
      #LOG('testjp',0,'field: %s' % str(field))
      f_type = None
      #for p in field._properties:
      #  if p['id'] == 'default':
      #    f_type = p['type']
      if isinstance(field,CPSImageField):
        f_type = 'object'
      elif isinstance(field,CPSStringField):
        f_type = 'string'
      elif isinstance(field,CPSDateTimeField):
Sebastien Robin's avatar
Sebastien Robin committed
64
        f_type = 'date'
65 66
      elif isinstance(field,CPSFileField):
        f_type = 'object'
Sebastien Robin's avatar
Sebastien Robin committed
67 68
      elif isinstance(field,CPSIntField):
        f_type = 'int'
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
      elif isinstance(field,CPSDocument):
        pass
      #prop_id = schema.getIdUnprefixed(field.id)
      #if prop_id in ('file_text','content','attachedFile',
      #                      'attachedFile_html','attachedFile_text', 'content'):
      #  f_type = 'object' # this should be string, but this strings
                          # do so bad xml
      #if not (prop_id in ('file_text','content','attachedFile','attachedFile_html','attachedFile_text')):
      #if not (prop_id in ('content',)):
      if f_type is not None:
        property_sheet.append(
          {
            'id'    :   prop_id,
            'type'  :   f_type
          }
          )
85 86 87 88 89 90 91 92 93 94
    return tuple(property_sheet + list(getattr(self, '_local_properties', ())))


  security.declareProtected( View, 'getProperty' )
  def getProperty(self, key, d=None):
    """
      Previous Name: getValue

      Generic accessor. Calls the real accessor
    """
95
    data_model = self.getTypeInfo().getDataModel(self)
96
    accessor_name = 'get' + UpperCase(key)
97 98 99 100 101 102 103 104 105 106
    base = aq_base(self)
    if data_model.has_key(key):
      return data_model.get(key)
    elif hasattr(base,accessor_name):
      method = getattr(base,accessor_name)
      return method()
    return None

  security.declarePrivate('getLayoutAndSchema' )
  def getLayoutAndSchema(self):
107 108 109
    if hasattr(self,'.cps_layouts') and hasattr(self,'.cps_schemas'):
      return (aq_base(self._getOb(".cps_layouts")),aq_base(self._getOb(".cps_schemas")))
    return None
110 111 112 113 114 115

  security.declarePrivate('setLayoutAndSchema' )
  def setLayoutAndSchema(self, data):
    """
    data must be : (layout,schema)
    """
116 117 118
    if data is not None:
      self._setOb(".cps_layouts",data[0])
      self._setOb(".cps_schemas",data[1])
119 120 121 122 123 124 125

  security.declarePrivate('_setProperty' )
  def _setProperty(self, key, value, type='string'):
    """
      Set the property for cps objects
    """
    LOG('PatchCPSDoc._setProperty',0,'key: %s, value: %s' % (repr(key),repr(value)))
Sebastien Robin's avatar
Sebastien Robin committed
126 127 128 129 130 131 132 133 134 135
    accessor_name = 'set' + UpperCase(key)
    if hasattr(aq_base(self),accessor_name):
      method = getattr(self, accessor_name)
      return method(value)
    else:
      setattr(self,key,value)
      #data_model = self.getTypeInfo().getDataModel(self)
      #type_info = self.getTypeInfo()
      #kw = {key:value}
      #type_info.editObject(self,kw)
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158

  security.declarePrivate('edit' )
  def edit(self, REQUEST=None, force_update = 0, reindex_object = 0, **kw):
    return self._edit(REQUEST=REQUEST, force_update=force_update, reindex_object=reindex_object, **kw)


  # Object attributes update method
  security.declarePrivate( '_edit' )
  def _edit(self, REQUEST=None, force_update = 0, reindex_object = 0, **kw):
    """
      Generic edit Method for all ERP5 object
      The purpose of this method is to update attributed, eventually do
      some kind of type checking according to the property sheet and index
      the object.
  
      Each time attributes of an object are updated, they should
      be updated through this generic edit method
    """
    LOG('PatchCPSDoc._edit, kw: ',0,kw)
    try:
      categoryIds = self._getCategoryTool().getBaseCategoryIds()
    except:
      categoryIds = []
Sebastien Robin's avatar
Sebastien Robin committed
159 160
    #if kw.has_key('layout_and_schema'):
    #  self.setLayoutAndSchema(kw['layout_and_schema'])
161
    for key in kw.keys():
Sebastien Robin's avatar
Sebastien Robin committed
162
      accessor = 'get' + UpperCase(key)
163 164
      #if key in categoryIds:
      #  self._setCategoryMembership(key, kw[key])
Sebastien Robin's avatar
Sebastien Robin committed
165 166
      #if key != 'id' and key!= 'layout_and_schema':
      if key != 'id' :
167 168 169 170
        # We only change if the value is different
        # This may be very long.... 
        self._setProperty(key, kw[key])

Sebastien Robin's avatar
Sebastien Robin committed
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
def getCoverage(self):
  """
  """
  if hasattr(self,'coverage'):
    return self.coverage
  return None

def getCreator(self):
  """
  """
  #if hasattr(self,'coverage'):
  #  return self.coverage
  return None

def getRelation(self):
  """
  """
  if hasattr(self,'relation'):
    return self.relation
  return None

def getSource(self):
  """
  """
  if hasattr(self,'source'):
    return self.source
  return None

def getPreview(self):
  """
  """
  if hasattr(self,'preview'):
    return self.preview
  return None

def setCreator(self,value):
  """
  """
  setattr(self,'creator',value)

def setCreationDate(self,value):
  """
  """
  setattr(self,'creation_date',value)
215

Sebastien Robin's avatar
Sebastien Robin committed
216 217 218 219 220 221 222
CPSDocument.getCoverage = getCoverage
CPSDocument.getCreator = getCreator
CPSDocument.getRelation = getRelation
CPSDocument.setCreator = setCreator
CPSDocument.getSource = getSource
CPSDocument.getPreview = getPreview
CPSDocument.setCreationDate = setCreationDate
223
CPSDocument.getProperty = PatchedCPSDocument.getProperty
224 225
CPSDocument.getLayoutAndSchema = PatchedCPSDocument.getLayoutAndSchema
CPSDocument.setLayoutAndSchema = PatchedCPSDocument.setLayoutAndSchema
Sebastien Robin's avatar
Sebastien Robin committed
226 227
CPSDocument._propertyMap = PatchedCPSDocument._propertyMap
CPSDocument.setProperty = Base.setProperty
228
CPSDocument._setProperty = PatchedCPSDocument._setProperty
229
CPSDocument.asXML = Base.asXML
230 231
CPSDocument._edit = PatchedCPSDocument._edit
CPSDocument.edit = PatchedCPSDocument._edit