Commit 03deea25 authored by Nicolas Wavrant's avatar Nicolas Wavrant

WIP! raise if value doesn't match setter

parent 6ff0893f
...@@ -638,6 +638,9 @@ class CategoryTool(BaseTool): ...@@ -638,6 +638,9 @@ class CategoryTool(BaseTool):
category_list = (category_list, ) category_list = (category_list, )
elif category_list is None: elif category_list is None:
category_list = () category_list = ()
elif isinstance(category_list, list):
if any([not isinstance(c, str) for c in category_list]):
raise TypeError('CategoryTool.setCategoryMembership only takes string(s) as value', base_category_list, category_list)
if isinstance(base_category_list, str): if isinstance(base_category_list, str):
base_category_list = (base_category_list, ) base_category_list = (base_category_list, )
......
...@@ -26,12 +26,14 @@ ...@@ -26,12 +26,14 @@
# #
############################################################################## ##############################################################################
import types
from ZPublisher.HTTPRequest import FileUpload from ZPublisher.HTTPRequest import FileUpload
from TypeDefinition import type_definition, list_types, ATTRIBUTE_PREFIX from TypeDefinition import type_definition, list_types, ATTRIBUTE_PREFIX
from Accessor import Accessor as Method from Accessor import Accessor as Method
from Acquisition import aq_base from Acquisition import aq_base
from zLOG import LOG from zLOG import LOG
from DateTime import DateTime
from Products.ERP5Type.Cache import CachingMethod from Products.ERP5Type.Cache import CachingMethod
from Products.ERP5Type.PsycoWrapper import psyco from Products.ERP5Type.PsycoWrapper import psyco
...@@ -41,6 +43,21 @@ from AccessControl.PermissionRole import PermissionRole ...@@ -41,6 +43,21 @@ from AccessControl.PermissionRole import PermissionRole
# Creation of default constructor # Creation of default constructor
class func_code: pass class func_code: pass
builtin_type_tuple = (
types.NoneType,
types.BooleanType,
types.IntType,
types.LongType,
types.FloatType,
types.ComplexType,
types.StringType,
types.UnicodeType,
types.TupleType,
types.ListType,
types.DictType,
DateTime,
)
class Setter(Method): class Setter(Method):
""" """
Sets an attribute value. ATTRIBUTE_PREFIX and storage_id allow Sets an attribute value. ATTRIBUTE_PREFIX and storage_id allow
...@@ -72,6 +89,8 @@ class Setter(Method): ...@@ -72,6 +89,8 @@ class Setter(Method):
# Modify the property # Modify the property
if value in self._null: if value in self._null:
setattr(instance, self._storage_id, None) setattr(instance, self._storage_id, None)
elif not isinstance(value, builtin_type_tuple):
raise TypeError('Class Objects can\'t be used as property values')
elif self._property_type == 'content': elif self._property_type == 'content':
# A file object should be provided # A file object should be provided
file_upload = value file_upload = value
......
...@@ -31,6 +31,7 @@ from Base import func_code, type_definition, list_types, ATTRIBUTE_PREFIX, Sette ...@@ -31,6 +31,7 @@ from Base import func_code, type_definition, list_types, ATTRIBUTE_PREFIX, Sette
from zLOG import LOG from zLOG import LOG
from Products.ERP5Type.PsycoWrapper import psyco from Products.ERP5Type.PsycoWrapper import psyco
from Products.ERP5Type.Utils import convertToUpperCase from Products.ERP5Type.Utils import convertToUpperCase
from Products.ERP5Type.Base import Base
class SetSetter(BaseSetter): class SetSetter(BaseSetter):
""" """
......
...@@ -1288,9 +1288,16 @@ class Base( CopyContainer, ...@@ -1288,9 +1288,16 @@ class Base( CopyContainer,
**kw allows to call setProperty as a generic setter (ex. setProperty(value_uid, portal_type=)) **kw allows to call setProperty as a generic setter (ex. setProperty(value_uid, portal_type=))
""" """
LOG("NICOLAS-Base_setProperty", 0, key, value)
if type is not None: # Speed if type is not None: # Speed
if type in list_types: # Patch for OFS PropertyManager if type in list_types: # Patch for OFS PropertyManager
key += '_list' key += '_list'
if not (key.endswith('_value') or key.endswith('_value_list')):
if isinstance(value, (list, tuple)):
if any([isinstance(x, Base) for x in value]):
raise TypeError('_setProperty only accepts byte values')
elif isinstance(value, Base):
raise TypeError('_setProperty only accepts byte values')
accessor_name = '_set' + UpperCase(key) accessor_name = '_set' + UpperCase(key)
aq_self = aq_base(self) aq_self = aq_base(self)
# We must use aq_self # We must use aq_self
...@@ -1838,14 +1845,14 @@ class Base( CopyContainer, ...@@ -1838,14 +1845,14 @@ class Base( CopyContainer,
if target is None : if target is None :
path = target path = target
elif isinstance(target, str): elif isinstance(target, str):
# We have been provided a string # is Base in obj.__class__.__mro__ ?
path = target raise TypeError('Only objects should be passed as values')
elif isinstance(target, (tuple, list, set, frozenset)): elif isinstance(target, (tuple, list, set, frozenset)):
# We have been provided a list or tuple # We have been provided a list or tuple
path_list = [] path_list = []
for target_item in target: for target_item in target:
if isinstance(target_item, str): if isinstance(target_item, str):
path = target_item raise TypeError('Only objects should be passed as values')
else: else:
path = getRelativeUrl(target_item) path = getRelativeUrl(target_item)
path_list.append(cleanupCategory(path)) path_list.append(cleanupCategory(path))
......
...@@ -378,6 +378,32 @@ class TestZodbPropertySheet(ERP5TypeTestCase): ...@@ -378,6 +378,32 @@ class TestZodbPropertySheet(ERP5TypeTestCase):
""" """
XXX: WORK IN PROGRESS XXX: WORK IN PROGRESS
""" """
def testNicolas(self):
from zLOG import LOG
person = self.portal.person_module.newContent()
person.setTitle('NW')
with self.assertRaises(TypeError):
organisation = self.portal.organisation_module.newContent()
person.setFirstName(organisation)
with self.assertRaises(TypeError):
organisation = self.portal.organisation_module.newContent()
person.setSubordinationValue(organisation.getRelativeUrl())
with self.assertRaises(TypeError):
organisation = self.portal.organisation_module.newContent()
person.setSubordination(organisation)
social_title = self.portal.portal_categories.social_title.newContent(id='Mr')
with self.assertRaises(TypeError):
person.setSocialTitle(social_title)
with self.assertRaises(TypeError):
person.setSocialTitleValue(social_title.getRelativeUrl())
def getBusinessTemplateList(self): def getBusinessTemplateList(self):
return 'erp5_base', return 'erp5_base',
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment