| 1 |
""" |
|---|
| 2 |
Utility functions for portal_tab actions modifications. |
|---|
| 3 |
""" |
|---|
| 4 |
from Products.CMFCore.Expression import Expression |
|---|
| 5 |
from Products.CMFCore.utils import getToolByName |
|---|
| 6 |
|
|---|
| 7 |
def getPortalActions(self, category='portal_tabs'): |
|---|
| 8 |
""" Return all portal actions with 'portal_tabs' category """ |
|---|
| 9 |
return filter(lambda a: a.category == category, getToolByName(self, 'portal_actions')._cloneActions()) |
|---|
| 10 |
|
|---|
| 11 |
def editAction(self, num, name, id, action='', condition='', visibility=[], category='portal_tabs'): |
|---|
| 12 |
""" Function for editing given action """ |
|---|
| 13 |
actions = getToolByName(self, 'portal_actions')._actions |
|---|
| 14 |
tabs = filter(lambda a: a.category == category, 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 |
|
|---|
| 27 |
def reorderActions(self, idxs, category='portal_tabs'): |
|---|
| 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 == category] |
|---|
| 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 |
|
|---|
| 38 |
def deleteAction(self, idx, id, category='portal_tabs'): |
|---|
| 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 == category, actions) |
|---|
| 43 |
if tabs[int(idx)].id == id: |
|---|
| 44 |
portal_actions.deleteActions([actions.index(tabs[int(idx)]),]) |
|---|
| 45 |
return id |
|---|
| 46 |
|
|---|
| 47 |
def 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 |
|
|---|
| 55 |
def 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 |
|---|