From c5930edd2d838b3feff2c693851347c1bf6c301b Mon Sep 17 00:00:00 2001 From: Gaetan Delannay Date: Fri, 20 Sep 2013 23:15:54 +0200 Subject: [PATCH] [gen] Added wrapper.getLastEvent, allowing to perform kinds of queries into an object's history. --- fields/computed.py | 4 ++-- gen/wrappers/__init__.py | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/fields/computed.py b/fields/computed.py index 37418e8..30fc6e9 100644 --- a/fields/computed.py +++ b/fields/computed.py @@ -49,7 +49,7 @@ class Computed(Field): maxChars=None, colspan=1, method=None, plainText=False, master=None, masterValue=None, focus=False, historized=False, sync=True, mapping=None, label=None, sdefault='', scolspan=1, - swidth=None, sheight=None, context={}): + swidth=None, sheight=None, context=None): # The Python method used for computing the field value, or a PX. self.method = method # Does field computation produce plain text or XHTML? @@ -74,7 +74,7 @@ class Computed(Field): obj = obj.appy() ctx = {'obj': obj, 'field': self, '_': obj.translate, 'tool': obj.tool} - ctx.update(self.context) + if self.context: ctx.update(self.context) return self.method(ctx) else: # self.method is a method that will return the field value diff --git a/gen/wrappers/__init__.py b/gen/wrappers/__init__.py index dc75a03..f4a0bfb 100644 --- a/gen/wrappers/__init__.py +++ b/gen/wrappers/__init__.py @@ -1026,6 +1026,25 @@ class AbstractWrapper(object): whose values are the previous field values.''' self.o.addDataChange(data) + def getLastEvent(self, transition, notBefore=''): + '''Gets, from the object history, the last occurrence of transition + named p_transition. p_transition can be a list of names: in this + case, it returns the most recent occurrence of those transitions. If + p_notBefore is given, it corresponds to a kind of start transition + for the search: we will not search in the history preceding the last + occurrence of this transition.''' + history = self.history + i = len(history)-1 + while i >= 0: + event = history[i] + if notBefore and (event['action'] == notBefore): return + if isinstance(transition, basestring): + condition = event['action'] == transition + else: + condition = event['action'] in transition + if condition: return event + i -= 1 + def formatText(self, text, format='html'): '''Produces a representation of p_text into the desired p_format, which is 'html' by default.'''