Ignore:
Timestamp:
Jul 30, 2009 3:06:20 PM (15 years ago)
Author:
piv
Message:

make viewlet work with portal_actions (though need some fixes yet), add manifest, add css w/o dtml injections, fix settings interface

File:
1 edited

Legend:

Unmodified
Added
Removed
  • quintagroup.dropdownmenu/trunk/quintagroup/dropdownmenu/browser/viewlets.py

    r1182 r1194  
    1 from zope.component import getMultiAdapter 
     1# -*- coding: utf-8 -*- 
     2from Acquisition import aq_inner 
     3 
     4from zope.component import getMultiAdapter, queryUtility 
    25 
    36from Products.Five.browser.pagetemplatefile import ViewPageTemplateFile 
     7from Products.CMFCore.utils import getToolByName 
     8from Products.CMFCore.interfaces import IAction, IActionCategory 
     9from Products.CMFCore.ActionInformation import ActionInfo 
    410 
     11from plone.memoize.instance import memoize 
    512from plone.app.layout.viewlets import common 
     13from plone.registry.interfaces import IRegistry 
     14 
     15from quintagroup.dropdownmenu.interfaces import IDropDownMenuSettings 
    616 
    717 
    818class GlobalSectionsViewlet(common.GlobalSectionsViewlet): 
    919    index = ViewPageTemplateFile('templates/sections.pt') 
     20    recurse = ViewPageTemplateFile('templates/sections_recurse.pt') 
    1021 
    1122    def update(self): 
    12         super(GlobalSectionsViewlet, self).update() 
     23        # we may need some previously defined variables 
     24        #super(GlobalSectionsViewlet, self).update() 
     25 
     26        # prepare to gather portal tabs 
     27        tabs = [] 
     28        context = aq_inner(self.context) 
     29        self.conf = conf = self._settings() 
     30        self.tool = getToolByName(context, 'portal_actions') 
     31 
     32        # fetch actions-based tabs? 
     33        if conf.show_actions_tabs: 
     34            tabs.extend(self._actions_tabs()) 
     35 
     36        # fetch content structure-based tabs? 
     37        if conf.show_content_tabs: 
     38            # put content-based actions before content structure-based ones? 
     39            if conf.content_before_actions_tabs: 
     40                tabs = self._content_tabs() + tabs 
     41            else: 
     42                tabs.extend(self._content_tabs()) 
     43 
     44        # assign collected tabs eventually 
     45        self.portal_tabs = tabs 
     46 
     47    def _actions_tabs(self): 
     48        """Return tree of tabs based on portal_actions tool configuration""" 
     49        conf = self.conf 
     50        tool = self.tool 
     51        context = aq_inner(self.context) 
     52 
     53        # check if we have required root actions category inside tool 
     54        if conf.actions_category not in tool.objectIds(): 
     55            return [] 
     56 
     57        #category_ids = category.objectIds() 
     58        #selectedTabs = self.context.restrictedTraverse('selectedTabs') 
     59        ## try to find out selected subtab 
     60        #if tab['id'] == self.selected_portal_tab: 
     61            #selection = selectedTabs(None, None, tab['subtabs']) 
     62            #self.selected_sub_tab = selection['portal'] 
     63        return self._subactions(tool._getOb(conf.actions_category), context) 
     64 
     65    def _subactions(self, category, object, level=0): 
     66        tabs = [] 
     67        for info in self._actionInfos(category, object): 
     68            # prepare data for action 
     69            # TODO: implement current element functionality, maybe should be 
     70            #       done on a template level because of separate content and  
     71            #       actions tabs are rendered separately 
     72            currentItem = False 
     73            currentParent = False 
     74            # TODO: adjust img tag 
     75            icon = info['icon'] and '<img src="%s" />' % info['icon'] or '' 
     76 
     77            # look up children for a given action 
     78            children = [] 
     79            if level <= self.conf.actions_tabs_level: 
     80                # try to find out appropriate subcategory 
     81                subcat_id = self.conf.nested_category_prefix + info['id'] + \ 
     82                         self.conf.nested_category_sufix 
     83                if subcat_id in category.objectIds(): 
     84                    subcat = category._getOb(subcat_id) 
     85                    if IActionCategory.providedBy(subcat): 
     86                        children = self._subactions(subcat, object, level+1) 
     87 
     88            # make up final tab dictionary 
     89            tab = {'Title': info['title'], 
     90                   'Description': info['description'], 
     91                   'getURL': info['url'], 
     92                   'show_children': len(children) > 0, 
     93                   'children': children, 
     94                   'currentItem': currentItem, 
     95                   'currentParent': currentParent, 
     96                   'item_icon': {'html_tag': icon}, 
     97                   'normalized_review_state': 'visible'} 
     98            tabs.append(tab) 
     99        return tabs 
     100 
     101    def _actionInfos(self, category, object, check_visibility=1, 
     102                     check_permissions=1, check_condition=1, max=-1): 
     103        """Return action infos for a given category""" 
     104        context = aq_inner(self.context) 
     105        ec = self.tool._getExprContext(object) 
     106        actions = [ActionInfo(action, ec) for action in category.objectValues() 
     107                    if IAction.providedBy(action)] 
     108 
     109        action_infos = [] 
     110        for ai in actions: 
     111            if check_visibility and not ai['visible']: 
     112                continue 
     113            if check_permissions and not ai['allowed']: 
     114                continue 
     115            if check_condition and not ai['available']: 
     116                continue 
     117            action_infos.append(ai) 
     118            if max + 1 and len(action_infos) >= max: 
     119                break 
     120        return action_infos 
     121 
     122    def _content_tabs(self): 
     123        """Return tree of tabs based on content structure""" 
     124        return [] 
     125 
     126    @memoize 
     127    def _settings(self): 
     128        """Fetch dropdown menu settings registry""" 
     129        registry = queryUtility(IRegistry) 
     130        return registry.forInterface(IDropDownMenuSettings) 
     131 
     132    def createMenu(self): 
     133        return self.recurse(children=self.portal_tabs, level=1) 
     134 
     135    def _old_update(self): 
    13136        context_state = getMultiAdapter((self.context, self.request), 
    14137                                        name=u'plone_context_state') 
Note: See TracChangeset for help on using the changeset viewer.