Commit 6df4d0d7 authored by Julien Muchembled's avatar Julien Muchembled

Small optimisations

git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@33677 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent 2ce28390
...@@ -1581,21 +1581,16 @@ class Base( CopyContainer, ...@@ -1581,21 +1581,16 @@ class Base( CopyContainer,
returns False if the first name was never defined and returns False if the first name was never defined and
even if self.getProperty('first_name') returns '' even if self.getProperty('first_name') returns ''
""" """
accessor_name = 'has' + UpperCase(key) method = getattr(self, 'has' + UpperCase(key), _MARKER)
if hasattr(self, accessor_name): if method is _MARKER:
method = getattr(self, accessor_name) # Check in local properties (which obviously were defined at some point)
return key in self.propertyIds()
try: try:
return method() return method()
except ConflictError: except ConflictError:
raise raise
except: except:
return 0 return 0
else:
# Check in local properties (which obviously were defined at some point)
for p_id in self.propertyIds():
if key == p_id:
return 1
return 0
security.declareProtected(Permissions.AccessContentsInformation, security.declareProtected(Permissions.AccessContentsInformation,
'hasCategory') 'hasCategory')
...@@ -2552,7 +2547,6 @@ class Base( CopyContainer, ...@@ -2552,7 +2547,6 @@ class Base( CopyContainer,
if id_ in ptype_translation_dict: if id_ in ptype_translation_dict:
return str(Message('erp5_ui', ptype_translation_dict[id_])) return str(Message('erp5_ui', ptype_translation_dict[id_]))
security.declareProtected(Permissions.AccessContentsInformation, security.declareProtected(Permissions.AccessContentsInformation,
'getCompactTitle') 'getCompactTitle')
def getCompactTitle(self): def getCompactTitle(self):
...@@ -2571,12 +2565,11 @@ class Base( CopyContainer, ...@@ -2571,12 +2565,11 @@ class Base( CopyContainer,
if self.hasShortTitle(): if self.hasShortTitle():
r = self.getShortTitle() r = self.getShortTitle()
if r: return r if r: return r
if self.getProperty('reference'): return (self.getProperty('reference') or
r = self.getProperty('reference') # No need to test existence since all Base instances have this method
if r: return r # Also useful whenever title is calculated
r = self._baseGetTitle() # No need to test existence since all Base instances have this method self._baseGetTitle() or
if r: return r # Also useful whenever title is calculated self.getId())
return self.getId()
security.declareProtected(Permissions.AccessContentsInformation, security.declareProtected(Permissions.AccessContentsInformation,
'getCompactTranslatedTitle') 'getCompactTranslatedTitle')
...@@ -2605,12 +2598,11 @@ class Base( CopyContainer, ...@@ -2605,12 +2598,11 @@ class Base( CopyContainer,
if r: return r if r: return r
r = self.getShortTitle() r = self.getShortTitle()
if r: return r if r: return r
if self.getProperty('reference'): return (self.getProperty('reference') or
r = self.getReference() # No need to test existence since all Base instances have this method
if r: return r # Also useful whenever title is calculated
r = self._baseGetTranslatedTitle() # No need to test existence since all Base instances have this method self._baseGetTranslatedTitle() or
if r: return r # Also useful whenever title is calculated self.getId())
return self.getId()
# This method allows to sort objects in list is a more reasonable way # This method allows to sort objects in list is a more reasonable way
security.declareProtected(Permissions.AccessContentsInformation, 'getIntId') security.declareProtected(Permissions.AccessContentsInformation, 'getIntId')
...@@ -3057,16 +3049,6 @@ class Base( CopyContainer, ...@@ -3057,16 +3049,6 @@ class Base( CopyContainer,
fallback_script_id : the script to use if nothing is found fallback_script_id : the script to use if nothing is found
""" """
def getScriptName(portal_type, method_id):
from Products.ERP5Type.Base import Base
class_name_list = [base_class.__name__ for base_class in self.__class__.mro() if issubclass(base_class, Base)]
script_name_end = '_%s' % method_id
for script_name_begin in [portal_type, self.getMetaType()] + class_name_list:
name = ''.join([script_name_begin.replace(' ',''), script_name_end])
script = getattr(self, name, None)
if script is not None:
return name
# script_id should not be used any more, keep compatibility # script_id should not be used any more, keep compatibility
if script_id is not None: if script_id is not None:
LOG('ERP5Type/Base.getTypeBaseMethod',0, LOG('ERP5Type/Base.getTypeBaseMethod',0,
...@@ -3077,18 +3059,26 @@ class Base( CopyContainer, ...@@ -3077,18 +3059,26 @@ class Base( CopyContainer,
# transaction # transaction
portal_type = self.getPortalType() portal_type = self.getPortalType()
tv = getTransactionalVariable(self) tv = getTransactionalVariable(self)
type_base_cache = tv.setdefault( type_base_cache = tv.setdefault('Base.type_based_cache', {})
'Base.type_based_cache', {})
cache_key = (portal_type, method_id) cache_key = (portal_type, method_id)
try: try:
name = type_base_cache[cache_key] script = type_base_cache[cache_key]
except KeyError: except KeyError:
name = getScriptName(portal_type, method_id) class_name_list = [portal_type, self.getMetaType()] + \
type_base_cache[cache_key] = name [base_class.__name__ for base_class in self.__class__.mro()
if issubclass(base_class, Base)]
script_name_end = '_' + method_id
for script_name_begin in class_name_list:
script_id = script_name_begin.replace(' ','') + script_name_end
script = getattr(self, script_id, None)
if script is not None:
type_base_cache[cache_key] = aq_base(script)
return script
type_base_cache[cache_key] = None
if name is not None: if script is not None:
return getattr(self, name) return script.__of__(self)
if fallback_script_id is not None: if fallback_script_id is not None:
return getattr(self, fallback_script_id) return getattr(self, fallback_script_id)
......
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