1 | ## Script (Python) "getSitemap" |
---|
2 | ##bind container=container |
---|
3 | ##bind context=context |
---|
4 | ##bind namespace= |
---|
5 | ##bind script=script |
---|
6 | ##bind subpath=traverse_subpath |
---|
7 | ##parameters= |
---|
8 | ##title= |
---|
9 | |
---|
10 | from Products.CMFCore.utils import getToolByName |
---|
11 | from Products.CMFPlone.utils import safe_callable |
---|
12 | from Products.CMFCore.WorkflowCore import WorkflowException |
---|
13 | |
---|
14 | def addToNavTreeResult(result, data): |
---|
15 | """Adds a piece of content to the result tree.""" |
---|
16 | path = data['path'] |
---|
17 | parentpath = '/'.join(path.split('/')[:-1]) |
---|
18 | # Tell parent about self |
---|
19 | if result.has_key(parentpath): |
---|
20 | result[parentpath]['children'].append(data) |
---|
21 | else: |
---|
22 | result[parentpath] = {'children':[data]} |
---|
23 | # If we have processed a child already, make sure we register it |
---|
24 | # as a child |
---|
25 | if result.has_key(path): |
---|
26 | data['children'] = result[path]['children'] |
---|
27 | result[path] = data |
---|
28 | |
---|
29 | ct = getToolByName(context, 'portal_catalog') |
---|
30 | ntp = getToolByName(context, 'portal_properties').navtree_properties |
---|
31 | stp = getToolByName(context, 'portal_properties').site_properties |
---|
32 | plone_utils = getToolByName(context, 'plone_utils') |
---|
33 | view_action_types = stp.getProperty('typesUseViewActionInListings', ()) |
---|
34 | currentPath = None |
---|
35 | portalpath = getToolByName(context, 'portal_url').getPortalPath() |
---|
36 | |
---|
37 | custom_query = getattr(context, 'getCustomNavQuery', None) |
---|
38 | if custom_query is not None and safe_callable(custom_query): |
---|
39 | query = custom_query() |
---|
40 | else: |
---|
41 | query = {} |
---|
42 | |
---|
43 | currentPath = portalpath |
---|
44 | query['path'] = {'query':currentPath, |
---|
45 | 'depth':ntp.getProperty('sitemapDepth', 2)} |
---|
46 | |
---|
47 | query['portal_type'] = plone_utils.typesToList() |
---|
48 | |
---|
49 | if ntp.getProperty('sortAttribute', False): |
---|
50 | query['sort_on'] = ntp.sortAttribute |
---|
51 | |
---|
52 | if (ntp.getProperty('sortAttribute', False) and ntp.getProperty('sortOrder', False)): |
---|
53 | query['sort_order'] = ntp.sortOrder |
---|
54 | |
---|
55 | if ntp.getProperty('enable_wf_state_filtering', False): |
---|
56 | query['review_state'] = ntp.wf_states_to_show |
---|
57 | |
---|
58 | query['is_default_page'] = False |
---|
59 | |
---|
60 | parentTypesNQ = ntp.getProperty('parentMetaTypesNotToQuery', ()) |
---|
61 | |
---|
62 | # Get ids not to list and make a dict to make the search fast |
---|
63 | ids_not_to_list = ntp.getProperty('idsNotToList', ()) |
---|
64 | excluded_ids = {} |
---|
65 | for exc_id in ids_not_to_list: |
---|
66 | excluded_ids[exc_id] = 1 |
---|
67 | |
---|
68 | rawresult = ct(**query) |
---|
69 | |
---|
70 | # Build result dict |
---|
71 | result = {} |
---|
72 | foundcurrent = False |
---|
73 | for item in rawresult: |
---|
74 | path = item.getPath() |
---|
75 | |
---|
76 | if item.is_folderish: |
---|
77 | default_page = item.getObject().getProperty('default_page', '') |
---|
78 | if default_page: |
---|
79 | item_url = '%s/%s' % (item.getURL(), default_page) |
---|
80 | else: |
---|
81 | item_url = (item.portal_type in view_action_types and item.getURL() + '/view') or item.getURL() |
---|
82 | else: |
---|
83 | item_url = (item.portal_type in view_action_types and item.getURL() + '/view') or item.getURL() |
---|
84 | |
---|
85 | currentItem = path == currentPath |
---|
86 | if currentItem: |
---|
87 | foundcurrent = path |
---|
88 | no_display = ( |
---|
89 | excluded_ids.has_key(item.getId) or |
---|
90 | not not getattr(item, 'exclude_from_nav', True)) |
---|
91 | data = {'Title':plone_utils.pretty_title_or_id(item), |
---|
92 | 'currentItem':currentItem, |
---|
93 | 'absolute_url': item_url, |
---|
94 | 'getURL':item_url, |
---|
95 | 'path': path, |
---|
96 | 'icon':item.getIcon, |
---|
97 | 'creation_date': item.CreationDate, |
---|
98 | 'portal_type': item.portal_type, |
---|
99 | 'review_state': item.review_state, |
---|
100 | 'Description':item.Description, |
---|
101 | 'show_children':item.is_folderish and item.portal_type not in parentTypesNQ, |
---|
102 | 'children':[], |
---|
103 | 'no_display': no_display} |
---|
104 | addToNavTreeResult(result, data) |
---|
105 | |
---|
106 | if ntp.getProperty('showAllParents', False): |
---|
107 | portal = getToolByName(context, 'portal_url').getPortalObject() |
---|
108 | parent = context |
---|
109 | parents = [parent] |
---|
110 | while not parent is portal: |
---|
111 | parent = parent.aq_parent |
---|
112 | parents.append(parent) |
---|
113 | |
---|
114 | wf_tool = getToolByName(context, 'portal_workflow') |
---|
115 | for item in parents: |
---|
116 | if getattr(item, 'getPhysicalPath', None) is None: |
---|
117 | # when Z3-style views are used, the view class will be in |
---|
118 | # the 'parents' list, but will not support 'getPhysicalPath' |
---|
119 | # we can just skip it b/c it's not an object in the content |
---|
120 | # tree that should be showing up in the nav tree (ra) |
---|
121 | continue |
---|
122 | path = '/'.join(item.getPhysicalPath()) |
---|
123 | if not result.has_key(path) or not result[path].has_key('path'): |
---|
124 | # item was not returned in catalog search |
---|
125 | if foundcurrent: |
---|
126 | currentItem = False |
---|
127 | else: |
---|
128 | currentItem = path == currentPath |
---|
129 | if currentItem: |
---|
130 | if plone_utils.isDefaultPage(item): |
---|
131 | # don't list folder default page |
---|
132 | continue |
---|
133 | else: |
---|
134 | foundcurrent = path |
---|
135 | try: |
---|
136 | review_state = wf_tool.getInfoFor(item, 'review_state') |
---|
137 | except WorkflowException: |
---|
138 | review_state = '' |
---|
139 | |
---|
140 | if item.is_folderish(): |
---|
141 | default_page = item.getProperty('default_page', '') |
---|
142 | if default_page: |
---|
143 | item_url = '%s/%s' % (item.absolute_url(), default_page) |
---|
144 | else: |
---|
145 | item_url = (item.portal_type in view_action_types and item.absolute_url() + '/view') or item.absolute_url() |
---|
146 | else: |
---|
147 | item_url = (item.portal_type in view_action_types and item.absolute_url() + '/view') or item.absolute_url() |
---|
148 | |
---|
149 | data = {'Title': plone_utils.pretty_title_or_id(item), |
---|
150 | 'currentItem': currentItem, |
---|
151 | 'absolute_url': item_url, |
---|
152 | 'getURL': item_url, |
---|
153 | 'path': path, |
---|
154 | 'icon': item.getIcon(), |
---|
155 | 'creation_date': item.CreationDate(), |
---|
156 | 'review_state': review_state, |
---|
157 | 'Description':item.Description(), |
---|
158 | 'children':[], |
---|
159 | 'portal_type':item.portal_type, |
---|
160 | 'no_display': 0} |
---|
161 | addToNavTreeResult(result, data) |
---|
162 | |
---|
163 | if not foundcurrent: |
---|
164 | for i in range(1, len(currentPath.split('/')) - len(portalpath.split('/')) + 1): |
---|
165 | p = '/'.join(currentPath.split('/')[:-i]) |
---|
166 | if result.has_key(p): |
---|
167 | foundcurrent = p |
---|
168 | result[p]['currentItem'] = True |
---|
169 | break |
---|
170 | |
---|
171 | if result.has_key(portalpath): |
---|
172 | return result[portalpath] |
---|
173 | else: |
---|
174 | return {} |
---|