-
Notifications
You must be signed in to change notification settings - Fork 2
/
contentlisting.py
38 lines (31 loc) · 1.37 KB
/
contentlisting.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
from plone.app.layout.viewlets import ViewletBase
from Products.Five.browser.pagetemplatefile import ViewPageTemplateFile
from plone.memoize import instance
class ContentListingViewlet(ViewletBase):
"""Lists content by categories"""
render = ViewPageTemplateFile('contentlisting.pt')
def available(self):
return bool(self.get_content())
@instance.memoize
def get_content(self):
query = {
'object_provides': 'ftw.contentpage.interfaces.ICategorizable'}
contents = self.context.getFolderContents(contentFilter=query,
full_objects=True)
return self._create_resultmap(contents)
def _create_resultmap(self, contents=None):
resultmap = {}
if not contents:
return []
for obj in contents:
categories = obj.Schema()['categories'].get(obj)
for cat in categories:
if cat not in resultmap:
resultmap[cat] = []
resultmap[cat].append((obj.title_or_id(),
obj.absolute_url(),
obj.Description() or obj.title_or_id()))
for value in resultmap.values():
value.sort()
# Return result sorted by title_or_id
return sorted(resultmap.items(), key=lambda item: item[1][0])