[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.

This commit is contained in:
Gaetan Delannay 2014-10-15 09:39:01 +02:00
parent ecb970e519
commit e72f53c0d9
3 changed files with 31 additions and 6 deletions

View file

@ -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

View file

@ -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
# ------------------------------------------------------------------------------

View file

@ -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)