source: products/qPloneTabs/tags/0.2.2/utils.py @ 1

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

Building directory structure

  • Property svn:eol-style set to native
File size: 3.9 KB
Line 
1"""
2   Utility functions for portal_tab actions modifications.
3"""
4from Products.CMFCore.Expression import Expression
5from Products.CMFCore.utils import getToolByName
6
7def getPortalTabs(self):
8    """ Return all portal actions with 'portal_tabs' category """
9    return filter(lambda a: a.category == 'portal_tabs', getToolByName(self, 'portal_actions')._cloneActions())
10
11def editAction(self, num, name, id, action='', condition='', visibility=[]):
12    """ Function for editing given action """
13    actions = getToolByName(self, 'portal_actions')._actions
14    tabs = filter(lambda a: a.category == 'portal_tabs', actions)
15    tab = tabs[int(num)]
16    if visibility != []:
17        if visibility == 'true': visibility = True
18        else: visibility = False
19        tab.visible = visibility
20        return 'Changed visibility'
21    if id: tab.id = id
22    if name: tab.title = name
23    if isinstance(condition, basestring): tab.condition = Expression(condition)
24    if isinstance(action, basestring): tab.setActionExpression(Expression(action))
25    return True
26
27def reorderActions(self, idxs):
28    """ Reorder portal_tabs actions in given order """
29    idxs = list(map(int,idxs))
30    portal_actions = getToolByName(self, 'portal_actions')
31    actions = portal_actions._cloneActions()
32    tabs = [[action, actions.index(action)] for action in actions if action.category == 'portal_tabs']
33    for idx in range(len(idxs)):
34        actions[tabs[idx][1]] = tabs[idxs[idx]][0]
35    portal_actions._actions = tuple(actions)
36    return idxs
37
38def deleteAction(self, idx, id):
39    """ Delete portal_tabs action with given index """
40    portal_actions = getToolByName(self, 'portal_actions')
41    actions = portal_actions._cloneActions()
42    tabs = filter(lambda a: a.category == 'portal_tabs', actions)
43    if tabs[int(idx)].id == id:
44        portal_actions.deleteActions([actions.index(tabs[int(idx)]),])
45    return id
46
47def processUrl(self, url):
48    """ Return url in a right format """
49    import re
50    if url.find('/') == 0: return 'string:${portal_url}' + url
51    elif re.compile('^(ht|f)tps?\:', re.I).search(url): return 'string:' + url
52    elif re.compile('^(python:|string:|not:|exists:|nocall:|path:)', re.I).search(url): return url
53    else: return 'string:${object_url}/' + url
54
55def getRootTabs(self):
56    """ Return all portal root elements which are displayed in poral globalnav """
57    stp = getToolByName(self, 'portal_properties').site_properties
58    if stp.getProperty('disable_folder_sections', True):
59        return []
60    result = []
61    query = {}
62    portal_path = getToolByName(self, 'portal_url').getPortalPath()
63    query['path'] = {'query':portal_path, 'navtree':1}
64    utils = getToolByName(self, 'plone_utils')
65    ct = getToolByName(self, 'portal_catalog')
66    ntp = getToolByName(self, 'portal_properties').navtree_properties
67    views = stp.getProperty('typesUseViewActionInListings', ())
68    query['portal_type'] = utils.typesToList()
69    if ntp.getProperty('sortAttribute', False):
70        query['sort_on'] = ntp.sortAttribute
71    if (ntp.getProperty('sortAttribute', False) and ntp.getProperty('sortOrder', False)):
72        query['sort_order'] = ntp.sortOrder
73    if ntp.getProperty('enable_wf_state_filtering', False):
74        query['review_state'] = ntp.wf_states_to_show
75    query['is_default_page'] = False
76    query['is_folderish'] = True
77    excluded_ids = {}
78    for exc_id in ntp.getProperty('idsNotToList', ()):
79        excluded_ids[exc_id] = 1
80    rawresult = ct(**query)
81    for item in rawresult:
82        if not excluded_ids.has_key(item.getId):
83            item_url = (item.portal_type in views and item.getURL() + '/view') or item.getURL()
84            data = {'name' : utils.pretty_title_or_id(item),
85                    'id' : item.getId,
86                    'url' : item_url,
87                    'description' : item.Description,
88                    'exclude_from_nav' : item.exclude_from_nav}
89            result.append(data)
90    return result
Note: See TracBrowser for help on using the repository browser.