[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.px import Px
from appy.shared import utils as sutils from appy.shared import utils as sutils
from group import Group from group import Group
from search import Search
from page import Page from page import Page
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------
@ -588,6 +589,18 @@ class Field:
res = str(res) res = str(res)
return 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): def valueIsInRequest(self, obj, request, name):
'''Is there a value corresponding to this field in the request? p_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 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, layouts=None, move=0, indexed=False, searchable=False,
specificReadPermission=False, specificWritePermission=False, specificReadPermission=False, specificWritePermission=False,
width=None, height=None, maxChars=None, colspan=1, method=None, width=None, height=None, maxChars=None, colspan=1, method=None,
plainText=False, master=None, masterValue=None, focus=False, formatMethod=None, plainText=False, master=None,
historized=False, mapping=None, label=None, sdefault='', masterValue=None, focus=False, historized=False, mapping=None,
scolspan=1, swidth=None, sheight=None, context=None): label=None, sdefault='', scolspan=1, swidth=None, sheight=None,
context=None):
# The Python method used for computing the field value, or a PX. # The Python method used for computing the field value, or a PX.
self.method = method 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): if isinstance(self.method, basestring):
# A legacy macro identifier. Raise an exception # A legacy macro identifier. Raise an exception
raise Exception(self.WRONG_METHOD % self.method) raise Exception(self.WRONG_METHOD % self.method)
@ -80,6 +87,10 @@ class Computed(Field):
return self.callMethod(obj, self.method, cache=False) return self.callMethod(obj, self.method, cache=False)
def getFormattedValue(self, obj, value, showChanges=False, language=None): def getFormattedValue(self, obj, value, showChanges=False, language=None):
if not isinstance(value, basestring): return str(value) if self.formatMethod:
return value 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, podContext = {'tool': tool, 'user': obj.user, 'self': obj, 'field':self,
'now': obj.o.getProductConfig().DateTime(), 'now': obj.o.getProductConfig().DateTime(),
'_': obj.translate, 'projectFolder': diskFolder, '_': 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 # If the pod document is related to a query, re-trigger it and put the
# result in the pod context. # result in the pod context.
if queryData: if queryData:
@ -528,6 +528,7 @@ class Pod(Field):
sortBy=sortKey, sortOrder=sortOrder, filterKey=filterKey, sortBy=sortKey, sortOrder=sortOrder, filterKey=filterKey,
filterValue=filterValue, maxResults='NO_LIMIT') filterValue=filterValue, maxResults='NO_LIMIT')
podContext['objects'] = [o.appy() for o in objs.objects] podContext['objects'] = [o.appy() for o in objs.objects]
podContext['queryData'] = queryData.split(';')
# Add the field-specific and custom contexts if present. # Add the field-specific and custom contexts if present.
if specificContext: podContext.update(specificContext) if specificContext: podContext.update(specificContext)
if customContext: podContext.update(customContext) if customContext: podContext.update(customContext)