Skip to content

Commit

Permalink
Merge 07ad57b into 2ee7444
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Havlik committed May 14, 2019
2 parents 2ee7444 + 07ad57b commit b66d41a
Show file tree
Hide file tree
Showing 6 changed files with 215 additions and 104 deletions.
7 changes: 6 additions & 1 deletion CHANGES.rst
Expand Up @@ -4,7 +4,12 @@ Changelog
5.1 (unreleased)
----------------

- Nothing changed yet.
Bug fixes
+++++++++

- Fix sorting of index overview table in ZMI. Migrated the template from
to zpt.
(`#62 https://github.com/zopefoundation/Products.ZCatalog/issues/62`)


5.0 (2019-05-10)
Expand Down
3 changes: 2 additions & 1 deletion src/Products/ZCatalog/ZCatalog.py
Expand Up @@ -38,6 +38,7 @@
from OFS.Folder import Folder
from OFS.ObjectManager import ObjectManager
from Persistence import Persistent
from Products.PageTemplates.PageTemplateFile import PageTemplateFile
from Products.PluginIndexes.interfaces import IPluggableIndex
import transaction
from zExceptions import BadRequest
Expand Down Expand Up @@ -121,7 +122,7 @@ class is that it is not Zope specific. You can use it in any
manage_catalogView = DTMLFile('dtml/catalogView', globals())

security.declareProtected(manage_zcatalog_entries, 'manage_catalogIndexes')
manage_catalogIndexes = DTMLFile('dtml/catalogIndexes', globals())
manage_catalogIndexes = PageTemplateFile('zpt/catalogIndexes', globals())

security.declareProtected(manage_zcatalog_entries, 'manage_catalogSchema')
manage_catalogSchema = DTMLFile('dtml/catalogSchema', globals())
Expand Down
13 changes: 12 additions & 1 deletion src/Products/ZCatalog/ZCatalogIndexes.py
Expand Up @@ -16,6 +16,7 @@
from AccessControl.class_init import InitializeClass
from AccessControl.SecurityInfo import ClassSecurityInfo
from AccessControl.Permissions import manage_zcatalog_indexes
from AccessControl.Permissions import view_management_screens
from Acquisition import aq_base
from Acquisition import aq_inner
from Acquisition import aq_parent
Expand Down Expand Up @@ -72,6 +73,17 @@ def _getOb(self, id, default=_marker):
return indexes.get(id)
return indexes.get(id, default)

@security.protected(view_management_screens)
def manage_get_sortedObjects(self, sortkey, revkey):
"""We need to wrap the index objects because some of them
can have security which does not work if they are unwrapped.
This happened to ZCTextIndex objects in Plone."""
items = super(ZCatalogIndexes, self).manage_get_sortedObjects(
sortkey, revkey)
for item in items:
item['obj'] = item['obj'].__of__(self)
return items

@security.protected(manage_zcatalog_indexes)
def objectIds(self, spec=None):
indexes = aq_parent(aq_inner(self))._catalog.indexes
Expand All @@ -98,7 +110,6 @@ def _setObject(self, id, object, roles=None, user=None, set_owner=1):

def __bobo_traverse__(self, REQUEST, name):
indexes = aq_parent(self)._catalog.indexes

o = indexes.get(name, None)
if o is not None:
if getattr(o, 'manage_workspace', None) is None:
Expand Down
101 changes: 0 additions & 101 deletions src/Products/ZCatalog/dtml/catalogIndexes.dtml

This file was deleted.

45 changes: 45 additions & 0 deletions src/Products/ZCatalog/tests/test_manage_indexes.py
@@ -0,0 +1,45 @@
# -*- coding: utf-8 -*-
import Testing.testbrowser
import Testing.ZopeTestCase
import Zope2.App


class IndexSortingTests(Testing.ZopeTestCase.FunctionalTestCase):
"""Browser testing /manage_catalogIndexes"""

def setUp(self):
super(IndexSortingTests, self).setUp()

Zope2.App.zcml.load_site(force=True)

uf = self.app.acl_users
uf.userFolderAddUser('manager', 'manager_pass', ['Manager'], [])
zcatalog = self.app.manage_addProduct['ZCatalog']
zcatalog.manage_addZCatalog('catalog', 'The Catalog')
pli = self.app.catalog.Indexes.manage_addProduct['PluginIndexes']
pli.manage_addFieldIndex('Index1')
pli.manage_addKeywordIndex('Index2')
self.browser = Testing.testbrowser.Browser()
self.browser.login('manager', 'manager_pass')

def check_order(self, expect_1_before_2):
index1_pos = self.browser.contents.find('Index1')
index2_pos = self.browser.contents.find('Index2')
found_1_before_2 = index2_pos > index1_pos
self.assertEqual(found_1_before_2, expect_1_before_2)

def test_sortby(self):
base_url = (
'http://localhost/catalog/manage_catalogIndexes?skey=%s&rkey=%s')

self.browser.open(base_url % ('id', 'asc'))
self.check_order(expect_1_before_2=True)

self.browser.open(base_url % ('id', 'desc'))
self.check_order(expect_1_before_2=False)

self.browser.open(base_url % ('meta_type', 'asc'))
self.check_order(expect_1_before_2=True)

self.browser.open(base_url % ('meta_type', 'desc'))
self.check_order(expect_1_before_2=False)
150 changes: 150 additions & 0 deletions src/Products/ZCatalog/zpt/catalogIndexes.zpt
@@ -0,0 +1,150 @@
<tal:header replace="structure here/manage_page_header" />

<tal:tabs replace="structure here/manage_tabs" />

<main class="container-fluid">

<p class="form-help">
This list defines what indexes the Catalog will contain. When objects
get cataloged, the values of any attributes which match
an index in this list will get indexed. If you add indexes to a Catalog
which contains indexed objects, you MUST at the least re-index your newly
added index. You may want to update the whole Catalog.
</p>
<tal:indexes define="indexes context/Indexes">
<tal:add define="filtered_meta_types python:sorted(indexes.filtered_meta_types(), key=lambda mt: mt['name'])">
<form tal:attributes="action context/absolute_url" method="get">
<div class="form-group mt-4 mb-4">
<div class="input-group">
<div class="input-group-prepend">
<span class="input-group-text">Add <span class="d-sm-block d-none">&nbsp;new&nbsp;</span> Index:</span>
</div>
<tal:indexlist condition="python:len(filtered_meta_types) > 1">
<select id="addindex" class="form-control" name=":action"
onChange=""
tal:attributes="onChange string:location.href='${request/URL1}/'+this.options[this.selectedIndex].value">
<option value="manage_workspace" disabled>Select type to add...</option>
<tal:types repeat="indextype filtered_meta_types">
<option tal:attributes="value indextype/action"
tal:content="indextype/name"></option>
</tal:types>
</select>
</tal:indexlist>
<div class="input-group-append">
<input class="btn btn-primary" type="submit" name="submit" value="Add" />
</div>
</div>
</div>

</form>
</tal:add>
<form tal:attributes="action string:${request/URL1}/" name="objectItems" method="post">
<tal:items define="
skey python:request.get('skey','id');
rkey python:request.get('rkey','asc');
rkey_alt python:'desc' if rkey=='asc' else 'asc';
obs python: indexes.manage_get_sortedObjects(sortkey=skey, revkey=rkey);">
<tal:itemslist condition="obs">
<table class="table table-striped table-hover table-sm objectItems">
<thead class="thead-light" tal:attributes="class python:'thead-light sorted_%s'%(request.get('rkey',''))">
<tr>
<th scope="col" class="zmi-object-check text-right">
<input type="checkbox" id="checkAll" onclick="checkbox_all();" />
</th>

<th scope="col" class="zmi-object-id">
<a title="Sort Ascending by Name"
href="?skey=id&rkey=asc"
tal:attributes="title python:'Sort %s by Name'%( rkey_alt.upper() );
href python:'?skey=id&rkey=%s'%( rkey_alt );
class python:request.get('skey',None)=='id' and 'zmi-sort_key' or None;
">
Name
<i class="fa fa-sort"></i>
</a>
</th>

<th scope="col" class="zmi-object-indextype">
<a title="Sort Ascending by index type"
href="?skey=meta_type&rkey=asc"
tal:attributes="title python:'Sort %s by index type'%( rkey_alt.upper() );
href python:'?skey=meta_type&rkey=%s'%( rkey_alt );
class python:request.get('skey',None)=='meta_type' and 'zmi-sort_key' or None;
">
Index type
<i class="fa fa-sort"></i>
</a>
</th>

<th scope="col" class="zmi-object-size hidden-xs">
<a title="Sort Ascending by indexed values"
href="?skey=indexSize&rkey=asc"
tal:attributes="title python:'Sort %s by indexed values'%( rkey_alt.upper() );
href python:'?skey=indexSize&rkey=%s'%( rkey_alt );
class python:request.get('skey',None)=='indexSize' and 'zmi-sort_key' or None;
">
# distinct values
<i class="fa fa-sort"></i>
</a>
</th>

</tr>
</thead>
<tbody>
<tr tal:repeat="ob_dict obs">
<tal:obj define="ob nocall:ob_dict/obj">
<td class="zmi-object-check text-right" onclick="$(this).children('input').trigger('click');">
<input type="checkbox" class="checkbox-list-item" name="ids:list" tal:attributes="value ob_dict/id"
onclick="event.stopPropagation();select_objectitem($(this));" />
</td>
<td>
<a tal:attributes="href string:Indexes/${ob_dict/quoted_id}/manage_workspace" tal:content="ob_dict/id"></a>
<small tal:define="sourcenames ob/getIndexSourceNames"
tal:condition="python: len(sourcenames) != 1 or sourcenames[0] != ob_dict['id']"
tal:on-error="string:(${ob_dict/title|nothing})">
(indexed attributes: <span tal:replace="python: ', '.join(sourcenames)"/>)
</small>
</td>
<td class="text-left" tal:content="ob/meta_type"></td>
<td class="text-left zmi-object-size hidden-xs"
tal:content="ob/indexSize|string:n/a">
</td>
</tal:obj>
</tr>
</tbody>
</table>

<div class="zmi-controls">
<input class="btn btn-primary" type="submit" name="manage_delIndex:method" value="Remove index" />
<input class="btn btn-primary" type="submit" name="manage_reindexIndex:method" value="Reindex" />
<input class="btn btn-primary" type="submit" name="manage_clearIndex:method" value="Clear index" />
</div>

</tal:itemslist>
<tal:noitems condition="not: obs">
<div class="alert alert-info">There are currently no indexes</div>
</tal:noitems>
</tal:items>
</form>
</tal:indexes>
</main>
<script>
// +++++++++++++++++++++++++++
// Item Selection
// +++++++++++++++++++++++++++
function checkbox_all() {
var checkboxes = document.getElementsByClassName('checkbox-list-item');
// Toggle Highlighting CSS-Class
if (document.getElementById('checkAll').checked) {
$('table.objectItems tbody tr').addClass('checked');
} else {
$('table.objectItems tbody tr').removeClass('checked');
};
// Set Checkbox like checkAll-Box
for (i = 0; i < checkboxes.length; i++) {
checkboxes[i].checked = document.getElementById('checkAll').checked;
}
};
</script>
<tal:footer replace="structure here/manage_page_footer" />

0 comments on commit b66d41a

Please sign in to comment.