Skip to content

Commit

Permalink
Move sorting logic to ObjectManager, fix sorting by date and size
Browse files Browse the repository at this point in the history
  • Loading branch information
viktordick committed Oct 2, 2018
1 parent 4428baf commit 387bfed
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 15 deletions.
41 changes: 41 additions & 0 deletions src/OFS/ObjectManager.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
from zope.interface.interfaces import ComponentLookupError
from zope.lifecycleevent import ObjectAddedEvent
from zope.lifecycleevent import ObjectRemovedEvent
import zope.sequencesort

from App.Common import is_acquired
from App.config import getConfiguration
Expand Down Expand Up @@ -865,6 +866,46 @@ def last_modified(self, ob):
except (DateTimeError, AttributeError):
return ''

security.declareProtected(view_management_screens, 'manage_get_sortedObjects')
def manage_get_sortedObjects(self, sortkey, revkey):
'''
Return dictionaries used for the management page, sorted by sortkey
(which is 'id' or an attribute of the objects). The direction is
determined by rkey, which can be 'asc' for ascending or 'desc' for
descending.
It returns a list of dictionaries, with keys 'id' and 'obj', where 'id'
is the ID of the object as known by the parent and 'obj' is the child
object.
'''
if sortkey not in ['id', 'title', 'meta_type', 'get_size', '_p_mtime']:
sortkey = 'id'

items = []
for id, obj in self.objectItems():
item = {'id': id, 'obj': obj}
if sortkey != 'id' and hasattr(obj, sortkey):
# add the attribute by which we need to sort
item[sortkey] = getattr(obj, sortkey)
items.append(item)

if sortkey in ['id', 'title', 'meta_type']:
sort_func = 'strcoll'
else:
sort_func = 'cmp'

sorted_items = zope.sequencesort.sort(
items,
((sortkey, sort_func, revkey), ),
mapping = 1
)

# remove the additional attribute
return [
{'id': item['id'], 'obj': item['obj']}
for item in sorted_items
]



# Don't InitializeClass, there is a specific __class_init__ on ObjectManager
# InitializeClass(ObjectManager)
Expand Down
20 changes: 5 additions & 15 deletions src/OFS/zpt/main.zpt
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,9 @@
skey python:request.get('skey','id');
rkey python:request.get('rkey','asc');
rkey_alt python:request.get('rkey','asc')=='asc' and 'desc' or 'asc';
sort_func python:['strcoll','cmp'][skey not in ['meta_type','id','title']];
obs python: [
[('id', item[0]), ('obj', item[1]),]
+ [
(key, getattr(item[1],key))
for key in ['title', 'meta_type']
if hasattr(item[1],key)
]
for item in here.objectItems()
];
obs python: [dict(item) for item in obs];
obs python: sequence.sort(obs, ((skey,sort_func,rkey),), mapping=1 );
"
sort_func python:'strcoll' if skey in ['meta_type', 'id', 'title'] else 'cmp';
obs python: here.manage_get_sortedObjects(sortkey = skey, revkey = rkey);
"
tal:attributes="action string:${request/URL1}/">

<tal:not-empty condition="obs">
Expand Down Expand Up @@ -79,7 +69,7 @@
</thead>
<tbody>
<tr tal:repeat="ob_dict obs">
<tal:x define="ob nocall:ob_dict/obj">
<tal:obj define="ob nocall:ob_dict/obj">
<td class="zmi-object-check text-right"
onclick="$(this).children('input').trigger('click');">
<input type="checkbox"
Expand Down Expand Up @@ -118,7 +108,7 @@
<td class="text-right zmi-object-date hidden-xs pl-3"
tal:content="python:here.last_modified(ob)">
</td>
</tal:x>
</tal:obj>
</tr>
</tbody>
</table>
Expand Down

0 comments on commit 387bfed

Please sign in to comment.