[gen] Added the possibility to display the content of indexes for a given object on its view layout (param 'indexed' in the request).

This commit is contained in:
Gaetan Delannay 2014-12-19 11:21:43 +01:00
parent 982ae08997
commit 06c656d278
8 changed files with 66 additions and 8 deletions

View file

@ -648,11 +648,7 @@ class Field:
if not self.indexed:
raise Exception('Field %s: cannot retrieve catalog version of ' \
'unindexed field.' % self.name)
tool = obj.getTool()
indexName = self.getIndexName(usage)
catalogBrain = tool.getObject(obj.id, brain=True)
index = tool.getApp().catalog.Indexes[indexName]
return index.getEntryForObject(catalogBrain.getRID())
return obj.getTool().getCatalogValue(obj, self.getIndexName(usage))
def valueIsInRequest(self, obj, request, name, layoutType):
'''Is there a value corresponding to this field in the request? p_name

View file

@ -131,6 +131,6 @@ try:
ef.registerFactory('Text indexer', 'Text indexer', TextIndexer)
ef.registerFactory('List indexer', 'List indexer', ListIndexer)
except ImportError:
# May occur at generation time.
# May occur at generation time
pass
# ------------------------------------------------------------------------------

View file

@ -98,6 +98,23 @@ class ToolMixin(BaseMixin):
'''Returns the catalog object.'''
return self.getParentNode().catalog
def getCatalogValue(self, obj, indexName):
'''Get, for p_obj, the value stored in the catalog for the index
named p_indexName.'''
catalogBrain = self.getObject(obj.id, brain=True)
catalog = self.getApp().catalog
index = catalog.Indexes[indexName]
indexType = index.getTagName()
if indexType == 'ZCTextIndex':
# Zope bug: the lexicon can't be retrieved correctly
index._v_lexicon = getattr(catalog, index.lexicon_id)
res = index.getEntryForObject(catalogBrain.getRID())
if indexType == 'DateIndex':
# The index value is a number. Add a DateTime representation too.
from DateTime import DateTime
res = '%d (%s)' % (res, DateTime(res))
return res
def getApp(self):
'''Returns the root Zope object.'''
return self.getPhysicalRoot()

View file

@ -1730,4 +1730,18 @@ class BaseMixin:
method = self.REQUEST['method']
obj = self.appy()
return getattr(obj, method)()
def onReindex(self):
'''Called for reindexing an index or all indexes on the currently shown
object.'''
if not self.getTool().getUser().has_role('Manager'):
self.raiseUnauthorized()
rq = self.REQUEST
indexName = rq['indexName']
if indexName == '_all_':
self.reindex()
else:
self.reindex(indexes=(indexName,))
self.say(self.translate('action_done'))
self.goto(self.getUrl(rq['HTTP_REFERER']))
# ------------------------------------------------------------------------------

View file

@ -112,6 +112,8 @@ td.search { padding-top: 8px }
.list td, .list th { border: 3px solid #ededed; color: grey;
padding: 3px 5px 3px 5px }
.list th { background-color: #e5e5e5; font-style: italic; font-weight: normal }
.compact { font-size: 90%; width: 100% }
.compact th, .compact td { padding: 0px 3px 0px 3px }
.grid th { font-style: italic; font-weight: normal;
border-bottom: 5px solid #fdfdfd; padding: 3px 5px 0 5px }
.grid td { padding: 3px 3px 0 3px }

View file

@ -1119,3 +1119,9 @@ function initFocus(pageId){
var elem = document.getElementById(id);
if (elem) elem.focus();
}
function reindexObject(indexName){
var f = document.forms['reindexForm'];
f.indexName.value = indexName;
f.submit();
}

BIN
gen/ui/reindex.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 632 B

View file

@ -562,6 +562,24 @@ class AbstractWrapper(object):
</tr>
</table>''')
pxIndexedContent = Px('''
<form name="reindexForm" method="post" action=":'%s/onReindex' % obj.url">
<input type="hidden" name="indexName"/>
<table var="indexes=obj.getIndexes(asList=True)" class="list compact">
<tr><th>Index name</th><th>Type</th><th>Content
<img src=":url('reindex')" class="clickable" title="Reindex all indexes"
onclick="reindexObject(\'_all_\')"/></th></tr>
<tr for="info in indexes"
class=":loop.info.odd and 'odd' or 'even'">
<td>:info[0]</td><td>:info[1]</td>
<td><img src=":url('reindex')" class="clickable"
title="Reindex this index only"
onclick=":'reindexObject(%s)' % q(info[0])"/>
<x>:ztool.getCatalogValue(zobj, info[0])</x></td>
</tr>
</table>
</form>''')
pxView = Px('''
<x var="x=zobj.mayView(raiseError=True);
errors=req.get('errors', {});
@ -573,6 +591,7 @@ class AbstractWrapper(object):
x=zobj.removeMyLock(user, page);
groupedFields=zobj.getGroupedFields(layoutType, page,cssJs=cssJs)">
<x>:tool.pxPagePrologue</x>
<x if="'indexed' in req">:obj.pxIndexedContent</x>
<x var="tagId='pageLayout'; tagName=''; tagCss='';
layoutTarget=obj">:tool.pxLayoutedObject</x>
<x>:tool.pxPageBottom</x>
@ -679,10 +698,11 @@ class AbstractWrapper(object):
return res
@classmethod
def getIndexes(klass, includeDefaults=True):
def getIndexes(klass, includeDefaults=True, asList=False):
'''Returns a dict whose keys are the names of the indexes that are
applicable to instances of this class, and whose values are the
(Zope) types of those indexes.'''
(Zope) types of those indexes. If p_asList is True, it returns a
list of tuples insteadof a dict.'''
# Start with the standard indexes applicable for any Appy class.
if includeDefaults:
res = defaultIndexes.copy()
@ -696,6 +716,9 @@ class AbstractWrapper(object):
res[indexName] = field.getIndexType()
# Add the secondary index if present
if field.hasSortIndex(): res['%s_sort' % indexName] = 'FieldIndex'
if asList:
res = res.items()
res.sort(key=lambda e: e[0])
return res
# --------------------------------------------------------------------------