Commit f4fcbec4 authored by wenjie.zheng's avatar wenjie.zheng Committed by Sebastien Robin

id_as_reference.py: now allow to select between adding prefix or suffix.

parent 0f3124f2
...@@ -33,26 +33,25 @@ from Products.CMFActivity.Errors import ActivityPendingError ...@@ -33,26 +33,25 @@ from Products.CMFActivity.Errors import ActivityPendingError
from zLOG import LOG, WARNING from zLOG import LOG, WARNING
from Acquisition import aq_base from Acquisition import aq_base
def IdAsReferenceMixin(suffix): def IdAsReferenceMixin(extra_string, string_type="suffix"):
suffix_index = - len(suffix)
extra_string_index = -len(extra_string)
class IdAsReferenceMixin(object): class IdAsReferenceMixin(object):
# Declarative security # Declarative security
security = ClassSecurityInfo() security = ClassSecurityInfo()
security.declareObjectProtected(Permissions.AccessContentsInformation) security.declareObjectProtected(Permissions.AccessContentsInformation)
def cb_isMoveable(self): def cb_isMoveable(self):
return self.cb_userHasCopyOrMovePermission() return self.cb_userHasCopyOrMovePermission()
security.declareProtected(Permissions.AccessContentsInformation, security.declareProtected(Permissions.AccessContentsInformation,
'getIdAsReferenceSuffix') 'getIdAsReferenceSuffix')
@staticmethod @staticmethod
def getIdAsReferenceSuffix(): def getIdAsReferenceSuffix():
return suffix return extra_string
def __migrate(self): def __migrate(self):
if self.id[suffix_index:] != suffix: if self.id[extra_string_index:] != extra_string:
new_id = self.__dict__.get('default_reference') + suffix new_id = self.__dict__.get('default_reference') + extra_string
parent = self.getParentValue() parent = self.getParentValue()
if parent.has_key(new_id): if parent.has_key(new_id):
LOG("IdAsReferenceMixin", WARNING, "Skipping migration of %r in %r" LOG("IdAsReferenceMixin", WARNING, "Skipping migration of %r in %r"
...@@ -70,16 +69,27 @@ def IdAsReferenceMixin(suffix): ...@@ -70,16 +69,27 @@ def IdAsReferenceMixin(suffix):
'getReference') 'getReference')
def getReference(self, *args): def getReference(self, *args):
id = self.id id = self.id
if id[suffix_index:] == suffix: if string_type == "suffix":
return id[:suffix_index] if id[extra_string_index:] == extra_string:
try: return id[:extra_string_index]
return self._baseGetReference(*args) try:
except AttributeError: return self._baseGetReference(*args)
return getattr(aq_base(self), 'default_reference', (args or [None])[0]) except AttributeError:
return getattr(aq_base(self), 'default_reference', (args or [None])[0])
elif string_type == "prefix":
if id[:extra_string_index] == extra_string:
return id[extra_string_index:]
try:
return self._baseGetReference(*args)
except AttributeError:
return getattr(aq_base(self), 'default_reference', (args or [None])[0])
def _setReference(self, value): def _setReference(self, value):
self.__dict__.pop('default_reference', None) self.__dict__.pop('default_reference', None)
self.setId(value + suffix) if string_type == "prefix":
self.setId(extra_string + value)
elif string_type == "suffix":
self.setId(value + extra_string)
security.declareProtected(Permissions.ModifyPortalContent, 'setReference') security.declareProtected(Permissions.ModifyPortalContent, 'setReference')
setReference = _setReference setReference = _setReference
......
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