From e72f53c0d99cae90e0ed7192bf6063a6b3ec3439 Mon Sep 17 00:00:00 2001 From: Gaetan Delannay Date: Wed, 15 Oct 2014 09:39:01 +0200 Subject: [PATCH] [gen] Added field.getCatalogValue allowing, for indexed values, to get the value as indexed in the Zope catalog. [gen] field Computed: added param formatMethod allowing to define the way to format the computed value (ie: the formatted value can then be different from the 'raw' value: this can be interesting if the raw value must be indexed. --- fields/__init__.py | 13 +++++++++++++ fields/computed.py | 21 ++++++++++++++++----- fields/pod.py | 3 ++- 3 files changed, 31 insertions(+), 6 deletions(-) diff --git a/fields/__init__.py b/fields/__init__.py index 8e82907..61be700 100644 --- a/fields/__init__.py +++ b/fields/__init__.py @@ -22,6 +22,7 @@ from appy.gen import utils as gutils from appy.px import Px from appy.shared import utils as sutils from group import Group +from search import Search from page import Page # ------------------------------------------------------------------------------ @@ -588,6 +589,18 @@ class Field: res = str(res) return res + def getCatalogValue(self, obj, usage='search'): + '''This method returns the index value that is currently stored in the + catalog for this field on p_obj.''' + if not self.indexed: + raise Exception('Field %s: cannot retrieve catalog version of ' \ + 'unindexed field.' % self.name) + tool = obj.getTool() + indexName = Search.getIndexName(self.name, usage=usage) + catalogBrain = tool.getObject(obj.id, brain=True) + index = tool.getApp().catalog.Indexes[indexName] + return index.getEntryForObject(catalogBrain.getRID()) + def valueIsInRequest(self, obj, request, name): '''Is there a value corresponding to this field in the request? p_name can be different from self.name (ie, if it is a field within another diff --git a/fields/computed.py b/fields/computed.py index 1f16726..6a33d29 100644 --- a/fields/computed.py +++ b/fields/computed.py @@ -35,11 +35,18 @@ class Computed(Field): layouts=None, move=0, indexed=False, searchable=False, specificReadPermission=False, specificWritePermission=False, width=None, height=None, maxChars=None, colspan=1, method=None, - plainText=False, master=None, masterValue=None, focus=False, - historized=False, mapping=None, label=None, sdefault='', - scolspan=1, swidth=None, sheight=None, context=None): + formatMethod=None, plainText=False, master=None, + masterValue=None, focus=False, historized=False, mapping=None, + label=None, sdefault='', scolspan=1, swidth=None, sheight=None, + context=None): # The Python method used for computing the field value, or a PX. self.method = method + # A specific method for producing the formatted value of this field. + # This way, if, for example, the value is a DateTime instance which is + # indexed, you can specify in m_formatMethod the way to format it in + # the user interface while m_method computes the value stored in the + # catalog. + self.formatMethod = formatMethod if isinstance(self.method, basestring): # A legacy macro identifier. Raise an exception raise Exception(self.WRONG_METHOD % self.method) @@ -80,6 +87,10 @@ class Computed(Field): return self.callMethod(obj, self.method, cache=False) def getFormattedValue(self, obj, value, showChanges=False, language=None): - if not isinstance(value, basestring): return str(value) - return value + if self.formatMethod: + res = self.formatMethod(obj, value) + else: + res = value + if not isinstance(res, basestring): res = str(res) + return res # ------------------------------------------------------------------------------ diff --git a/fields/pod.py b/fields/pod.py index 77039f6..11349e3 100644 --- a/fields/pod.py +++ b/fields/pod.py @@ -514,7 +514,7 @@ class Pod(Field): podContext = {'tool': tool, 'user': obj.user, 'self': obj, 'field':self, 'now': obj.o.getProductConfig().DateTime(), '_': obj.translate, 'projectFolder': diskFolder, - 'template': template} + 'template': template, 'request': tool.request} # If the pod document is related to a query, re-trigger it and put the # result in the pod context. if queryData: @@ -528,6 +528,7 @@ class Pod(Field): sortBy=sortKey, sortOrder=sortOrder, filterKey=filterKey, filterValue=filterValue, maxResults='NO_LIMIT') podContext['objects'] = [o.appy() for o in objs.objects] + podContext['queryData'] = queryData.split(';') # Add the field-specific and custom contexts if present. if specificContext: podContext.update(specificContext) if customContext: podContext.update(customContext)