source: products/qSiloGroup/branches/plone-2.1/skins/qSiloGroup/getSitemap.py @ 1

Last change on this file since 1 was 1, checked in by myroslav, 19 years ago

Building directory structure

  • Property svn:eol-style set to native
File size: 6.3 KB
Line 
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
10from Products.CMFCore.utils import getToolByName
11from Products.CMFPlone.utils import safe_callable
12from Products.CMFCore.WorkflowCore import WorkflowException
13
14def 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
29ct = getToolByName(context, 'portal_catalog')
30ntp = getToolByName(context, 'portal_properties').navtree_properties
31stp = getToolByName(context, 'portal_properties').site_properties
32plone_utils = getToolByName(context, 'plone_utils')
33view_action_types = stp.getProperty('typesUseViewActionInListings', ())
34currentPath = None
35portalpath = getToolByName(context, 'portal_url').getPortalPath()
36
37custom_query = getattr(context, 'getCustomNavQuery', None)
38if custom_query is not None and safe_callable(custom_query):
39    query = custom_query()
40else:
41    query = {}
42
43currentPath = portalpath
44query['path'] = {'query':currentPath,
45                 'depth':ntp.getProperty('sitemapDepth', 2)}
46
47query['portal_type'] = plone_utils.typesToList()
48
49if ntp.getProperty('sortAttribute', False):
50    query['sort_on'] = ntp.sortAttribute
51
52if (ntp.getProperty('sortAttribute', False) and ntp.getProperty('sortOrder', False)):
53    query['sort_order'] = ntp.sortOrder
54
55if ntp.getProperty('enable_wf_state_filtering', False):
56    query['review_state'] = ntp.wf_states_to_show
57
58query['is_default_page'] = False
59
60parentTypesNQ = ntp.getProperty('parentMetaTypesNotToQuery', ())
61
62# Get ids not to list and make a dict to make the search fast
63ids_not_to_list = ntp.getProperty('idsNotToList', ())
64excluded_ids = {}
65for exc_id in ids_not_to_list:
66    excluded_ids[exc_id] = 1
67
68rawresult = ct(**query)
69
70# Build result dict
71result = {}
72foundcurrent = False
73for 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
106if 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
163if 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
171if result.has_key(portalpath):
172    return result[portalpath]
173else:
174    return {}
Note: See TracBrowser for help on using the repository browser.