CPSDocumentPatch.py 8.67 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):

Sebastien Robin's avatar
Sebastien Robin committed
31 32 33 34 35 36 37 38
    security = ClassSecurityInfo()

    security.declareProtected( View, '_propertyMap' )
    def _propertyMap(self):
        """
          Returns fake property sheet
        """
        property_sheet = []
39
        property_sheet.append(
Sebastien Robin's avatar
Sebastien Robin committed
40 41 42 43 44
            {
              'id'    :   'layout_and_schema',
              'type'  :   'object'
            }
            )
45 46 47 48 49 50
        property_sheet.append(
            {
              'id'    :   'cps_frozen',
              'type'  :   'int'
            }
            )
Sebastien Robin's avatar
Sebastien Robin committed
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
        type_info = self.getTypeInfo()
        field_list = []
        if type_info is not None:
            if hasattr(type_info,'getDataModel'):
              data_model = type_info.getDataModel(self)
              if data_model is not None:
                    field_list = data_model._fields.items()
        field_list.sort()
        for (prop_id,field) in field_list:
            f_type = None
            if isinstance(field,CPSImageField):
                f_type = 'object'
            elif isinstance(field,CPSStringField):
                f_type = 'string'
            elif isinstance(field,CPSDateTimeField):
                f_type = 'date'
            elif isinstance(field,CPSFileField):
                f_type = 'object'
            elif isinstance(field,CPSIntField):
                f_type = 'int'
            elif isinstance(field,CPSDocument):
                pass
            if prop_id.find('attachedFile')==0:
                f_type='object'  # In a flexible content, we do have some attachedFile_f1
                                 # which are CPStringFiels with some binary data
                                 # XXX This should NOT BE NEEDED!!
            if f_type is not None:
                property_sheet.append(
                    {
                        'id'    :   prop_id,
                        'type'  :   f_type
                    }
                    )
        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
        """
        accessor_name = 'get' + UpperCase(key)
        base = aq_base(self)
96 97 98 99 100 101
        data_model = None
        if hasattr(self,'getTypeInfo'):
          type_info =  self.getTypeInfo()
          if hasattr(type_info,'getDataModel'):
            data_model = self.getTypeInfo().getDataModel(self)
        if data_model is not None and data_model.has_key(key):
Sebastien Robin's avatar
Sebastien Robin committed
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
            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):
        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

    security.declarePrivate('setLayoutAndSchema' )
    def setLayoutAndSchema(self, data):
        """
        data must be : (layout,schema)
        """
        if data is not None:
            self._setOb(".cps_layouts",data[0])
            self._setOb(".cps_schemas",data[1])

    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)))
        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)
            # This solution below doesn't works well, it is better
            # to just set the attribute.
            #data_model = self.getTypeInfo().getDataModel(self)
            #type_info = self.getTypeInfo()
            #kw = {key:value}
            #type_info.editObject(self,kw)

    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 = []
        #if kw.has_key('layout_and_schema'):
        #  self.setLayoutAndSchema(kw['layout_and_schema'])
        for key in kw.keys():
            accessor = 'get' + UpperCase(key)
            #if key in categoryIds:
            #  self._setCategoryMembership(key, kw[key])
            #if key != 'id' and key!= 'layout_and_schema':
            if key != 'id' :
                # We only change if the value is different
                # This may be very long.... 
                self._setProperty(key, kw[key])
175

Sebastien Robin's avatar
Sebastien Robin committed
176
def getCoverage(self):
177 178
    """
    """
Sebastien Robin's avatar
Sebastien Robin committed
179 180
    if hasattr(self,'coverage'):
        return self.coverage
181 182
    return None

Sebastien Robin's avatar
Sebastien Robin committed
183
def getCreator(self):
Sebastien Robin's avatar
Sebastien Robin committed
184 185 186 187 188
    """
    """
    #if hasattr(self,'coverage'):
    #  return self.coverage
    return None
Sebastien Robin's avatar
Sebastien Robin committed
189 190

def getRelation(self):
Sebastien Robin's avatar
Sebastien Robin committed
191 192 193 194 195
    """
    """
    if hasattr(self,'relation'):
        return self.relation
    return None
Sebastien Robin's avatar
Sebastien Robin committed
196

Kevin Deldycke's avatar
Kevin Deldycke committed
197
def setRelation(self,value):
Sebastien Robin's avatar
Sebastien Robin committed
198 199 200
    """
    """
    setattr(self,'relation',value)
Kevin Deldycke's avatar
Kevin Deldycke committed
201

Sebastien Robin's avatar
Sebastien Robin committed
202
def getSource(self):
Sebastien Robin's avatar
Sebastien Robin committed
203 204 205 206 207
    """
    """
    if hasattr(self,'source'):
        return self.source
    return None
Sebastien Robin's avatar
Sebastien Robin committed
208 209

def getPreview(self):
Sebastien Robin's avatar
Sebastien Robin committed
210 211 212 213 214
    """
    """
    if hasattr(self,'preview'):
        return self.preview
    return None
Sebastien Robin's avatar
Sebastien Robin committed
215 216

def setCreator(self,value):
Sebastien Robin's avatar
Sebastien Robin committed
217 218 219
    """
    """
    setattr(self,'creator',value)
Sebastien Robin's avatar
Sebastien Robin committed
220 221

def setCreationDate(self,value):
Sebastien Robin's avatar
Sebastien Robin committed
222 223 224
    """
    """
    setattr(self,'creation_date',value)
225

226 227 228 229 230 231 232 233 234 235 236 237 238 239
def setCpsFrozen(self, data):
    """
    setter for cps frozen property in order to now
    if an object is frozen or not
    """
    setattr(self,'_cps_frozen',data)

def getCpsFrozen(self):
    """
    getter for cps frozen property in order to now
    if an object is frozen or not
    """
    return getattr(self,'_cps_frozen',0)

Sebastien Robin's avatar
Sebastien Robin committed
240 241 242 243
CPSDocument.getCoverage = getCoverage
CPSDocument.getCreator = getCreator
CPSDocument.getRelation = getRelation
CPSDocument.setCreator = setCreator
Kevin Deldycke's avatar
Kevin Deldycke committed
244
CPSDocument.setRelation = setRelation
Sebastien Robin's avatar
Sebastien Robin committed
245
CPSDocument.getSource = getSource
246 247
CPSDocument.getCpsFrozen = getCpsFrozen
CPSDocument.setCpsFrozen = setCpsFrozen
Sebastien Robin's avatar
Sebastien Robin committed
248 249
CPSDocument.getPreview = getPreview
CPSDocument.setCreationDate = setCreationDate
250
CPSDocument.getProperty = PatchedCPSDocument.getProperty
251 252
CPSDocument.getLayoutAndSchema = PatchedCPSDocument.getLayoutAndSchema
CPSDocument.setLayoutAndSchema = PatchedCPSDocument.setLayoutAndSchema
Sebastien Robin's avatar
Sebastien Robin committed
253 254
CPSDocument._propertyMap = PatchedCPSDocument._propertyMap
CPSDocument.setProperty = Base.setProperty
255
CPSDocument._setProperty = PatchedCPSDocument._setProperty
256
CPSDocument.get_local_permissions = Base.get_local_permissions
257
CPSDocument.asXML = Base.asXML
258
CPSDocument.manage_setLocalPermissions = Base.manage_setLocalPermissions
259
CPSDocument._edit = PatchedCPSDocument._edit