From c863b47db5373b525af573d4d59b998d91cd136d Mon Sep 17 00:00:00 2001 From: Gaetan Delannay Date: Thu, 10 Jan 2013 10:56:22 +0100 Subject: [PATCH] [gen] field.history can now be a method. This way, historization can be enabled/disabled, field by field, according to any custom condition. --- gen/__init__.py | 3 +++ gen/mixins/__init__.py | 31 +++++++++++++++++++++---------- 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/gen/__init__.py b/gen/__init__.py index e05b8ee..59e2ff9 100644 --- a/gen/__init__.py +++ b/gen/__init__.py @@ -1215,6 +1215,9 @@ class String(Type): return {'view': 'l-f', 'edit': 'lrv-d-f'} elif self.format == String.XHTML: 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' else: view = 'l-f' diff --git a/gen/mixins/__init__.py b/gen/mixins/__init__.py index cfbcf6e..e24d791 100644 --- a/gen/mixins/__init__.py +++ b/gen/mixins/__init__.py @@ -65,14 +65,19 @@ class BaseMixin: # Check that the user can add objects through this Ref. initiatorField.checkAdd(initiator) 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 - if not created: previousData = obj.rememberPreviousData() + if not created and fields: + previousData = obj.rememberPreviousData(fields) # Perform the change on the object - if rq: + if fields: # Store in the database the new value coming from the form - for appyType in self.getAppyTypes('edit', rq.get('page')): - value = getattr(values, appyType.name, None) - appyType.store(obj, value) + for field in fields: + value = getattr(values, field.name, None) + field.store(obj, value) if previousData: # Keep in history potential changes on historized fields obj.historizeData(previousData) @@ -402,14 +407,20 @@ class BaseMixin: if rq.get('appy', None) == '1': obj = obj.appy() return getattr(obj, 'on'+action)() - def rememberPreviousData(self): + def rememberPreviousData(self, fields): '''This method is called before updating an object and remembers, for every historized field, the previous value. Result is a dict ~{s_fieldName: previousFieldValue}~''' res = {} - for appyType in self.getAllAppyTypes(): - if appyType.historized: - res[appyType.name] = appyType.getValue(self) + for field in fields: + if not field.historized: continue + # 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 def addHistoryEvent(self, action, **kw): @@ -514,7 +525,7 @@ class BaseMixin: listType = self.getAppyType(listName) 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 field named p_name.''' return self.getAppyType(name).getFormattedValue(self,value,showChanges)