[gen] field.history can now be a method. This way, historization can be enabled/disabled, field by field, according to any custom condition.

This commit is contained in:
Gaetan Delannay 2013-01-10 10:56:22 +01:00
parent f091b25c98
commit c863b47db5
2 changed files with 24 additions and 10 deletions

View file

@ -1215,6 +1215,9 @@ class String(Type):
return {'view': 'l-f', 'edit': 'lrv-d-f'} return {'view': 'l-f', 'edit': 'lrv-d-f'}
elif self.format == String.XHTML: elif self.format == String.XHTML:
if self.historized: if self.historized:
# self.historized can be a method or a boolean. If it is a
# method, it means that under some condition, historization will
# be enabled. So we come here also in this case.
view = 'lc-f' view = 'lc-f'
else: else:
view = 'l-f' view = 'l-f'

View file

@ -65,14 +65,19 @@ class BaseMixin:
# Check that the user can add objects through this Ref. # Check that the user can add objects through this Ref.
initiatorField.checkAdd(initiator) initiatorField.checkAdd(initiator)
obj = createObject(folder, id, obj.portal_type, tool.getAppName()) obj = createObject(folder, id, obj.portal_type, tool.getAppName())
# Get the fields on the current page
fields = None
if rq: fields = self.getAppyTypes('edit', rq.get('page'))
# Remember the previous values of fields, for potential historization
previousData = None previousData = None
if not created: previousData = obj.rememberPreviousData() if not created and fields:
previousData = obj.rememberPreviousData(fields)
# Perform the change on the object # Perform the change on the object
if rq: if fields:
# Store in the database the new value coming from the form # Store in the database the new value coming from the form
for appyType in self.getAppyTypes('edit', rq.get('page')): for field in fields:
value = getattr(values, appyType.name, None) value = getattr(values, field.name, None)
appyType.store(obj, value) field.store(obj, value)
if previousData: if previousData:
# Keep in history potential changes on historized fields # Keep in history potential changes on historized fields
obj.historizeData(previousData) obj.historizeData(previousData)
@ -402,14 +407,20 @@ class BaseMixin:
if rq.get('appy', None) == '1': obj = obj.appy() if rq.get('appy', None) == '1': obj = obj.appy()
return getattr(obj, 'on'+action)() return getattr(obj, 'on'+action)()
def rememberPreviousData(self): def rememberPreviousData(self, fields):
'''This method is called before updating an object and remembers, for '''This method is called before updating an object and remembers, for
every historized field, the previous value. Result is a dict every historized field, the previous value. Result is a dict
~{s_fieldName: previousFieldValue}~''' ~{s_fieldName: previousFieldValue}~'''
res = {} res = {}
for appyType in self.getAllAppyTypes(): for field in fields:
if appyType.historized: if not field.historized: continue
res[appyType.name] = appyType.getValue(self) # appyType.historized can be a method or a boolean.
if callable(field.historized):
historized = field.callMethod(self, field.historized)
else:
historized = field.historized
if historized:
res[field.name] = field.getValue(self)
return res return res
def addHistoryEvent(self, action, **kw): def addHistoryEvent(self, action, **kw):
@ -514,7 +525,7 @@ class BaseMixin:
listType = self.getAppyType(listName) listType = self.getAppyType(listName)
return listType.getInnerValue(outerValue, name, int(i)) return listType.getInnerValue(outerValue, name, int(i))
def getFormattedFieldValue(self, name, value, showChanges): def getFormattedFieldValue(self, name, value, showChanges=False):
'''Gets a nice, string representation of p_value which is a value from '''Gets a nice, string representation of p_value which is a value from
field named p_name.''' field named p_name.'''
return self.getAppyType(name).getFormattedValue(self,value,showChanges) return self.getAppyType(name).getFormattedValue(self,value,showChanges)