Changeset 2775 in products


Ignore:
Timestamp:
Sep 3, 2010 8:46:43 AM (14 years ago)
Author:
chervol
Message:

added caching and fallbacks

File:
1 edited

Legend:

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

    r2774 r2775  
    2020from quintagroup.dropdownmenu.util import getDropDownMenuSettings 
    2121 
     22from time import time 
     23from plone.memoize import ram 
     24import copy 
     25 
     26def cache_key(a,b,c):    
     27    return c + str(time() // (60 * 60)) 
    2228 
    2329class GlobalSectionsViewlet(common.GlobalSectionsViewlet): 
     
    6167    def _actions_tabs(self): 
    6268        """Return tree of tabs based on portal_actions tool configuration""" 
     69        t1 = time() 
    6370        conf = self.conf 
    6471        tool = self.tool 
     
    6875        if conf.actions_category not in tool.objectIds(): 
    6976            return [] 
    70         self.tabs =[] 
    71         self.normalize_actions(tool._getOb(conf.actions_category), context, 0) 
     77        listtabs = [] 
     78        res, listtabs = self.prepare_tabs(self.site_url) 
     79        res = copy.deepcopy(res) 
     80        self.tabs = listtabs 
     81         
     82        # if there is no custom menu in portal tabs return 
     83        if not listtabs: 
     84            return [] 
     85             
    7286        current_item = -1 
    7387        delta = 1000 
    74         if not self.tabs: 
    75             return [] 
    76         for info in self.tabs: 
     88        for info in listtabs: 
    7789            if  self.context_url.startswith(info['url']) and \ 
    7890               len(self.context_url) - len(info['url']) < delta: 
    7991               delta = len(self.context_url) - len(info['url']) 
    80                current_item = self.tabs.index(info) 
     92               current_item = listtabs.index(info) 
    8193        self.id_chain = []  
    82  
    83         if current_item > -1 and current_item < len(self.tabs): 
    84             self.mark_active(self.tabs[current_item]['id'],self.tabs[current_item]['title']) 
    85         return  self._subactions(tool._getOb(conf.actions_category), context, 0) 
    86          
    87     def mark_active(self, current_id, title): 
     94         
     95        if current_item > -1 and current_item < len(listtabs): 
     96            self.mark_active(listtabs[current_item]['id'], listtabs[current_item]['url']) 
     97 
     98        self._activate(res) 
     99        dt = time() - t1 
     100        res[0]['Title'] = str(dt) 
     101        return res 
     102         
     103    @ram.cache(cache_key) 
     104    def prepare_tabs(self, site_url): 
     105        def normalize_actions(category, object, level, parent_url = None): 
     106            """walk through the tabs dictionary and build list of all tabs""" 
     107            tabs = [] 
     108            for info in self._actionInfos(category, object): 
     109                icon = info['icon'] and '<img src="%s" />' % info['icon'] or '' 
     110                children = [] 
     111                bottomLevel = self.conf.actions_tabs_level 
     112                if bottomLevel < 1 or level < bottomLevel: 
     113                    # try to find out appropriate subcategory 
     114                    subcat_id = self.cat_prefix + info['id'] + self.cat_sufix 
     115                    if subcat_id != info['id'] and \ 
     116                       subcat_id in category.objectIds(): 
     117                        subcat = category._getOb(subcat_id) 
     118                        if IActionCategory.providedBy(subcat): 
     119                            children = normalize_actions(subcat, object, level+1, info['url']) 
     120 
     121                parent_id = category['id'].replace(self.cat_prefix,'').replace(self.cat_sufix,'') 
     122                tab = {'id' : info['id'], 
     123                   'title': info['title'], 
     124                   'url': info['url'], 
     125                   'parent': (parent_id, parent_url)} 
     126                tabslist.append(tab) 
     127                 
     128                tab = {'id' : info['id'], 
     129                       'Title': info['title'], 
     130                       'Description': info['description'], 
     131                       'getURL': info['url'], 
     132                       'show_children': len(children) > 0, 
     133                       'children': children, 
     134                       'currentItem': False,  
     135                       'currentParent': False, 
     136                       'item_icon': {'html_tag': icon}, 
     137                       'normalized_review_state': 'visible'} 
     138                tabs.append(tab) 
     139            return tabs     
     140        tabslist = [] 
     141        tabs = normalize_actions(self.tool._getOb(self.conf.actions_category), aq_inner(self.context), 0) 
     142        return tabs, tabslist 
     143 
     144    def _activate(self, res): 
     145        """Mark selected chain in the tabs dictionary""" 
     146        for info in res: 
     147            if info['getURL'] in self.id_chain: 
     148                info['currentItem'] = True 
     149                info['currentParent'] = True 
     150                if info['children']: 
     151                    self._activate(info['children']) 
     152 
     153         
     154    def mark_active(self, current_id, url): 
    88155        for info in self.tabs: 
    89             if info['id'] == current_id and info['title'] == title: 
     156            if info['id'] == current_id and info['url'] == url: 
    90157                self.mark_active(info['parent'][0], info['parent'][1]) 
    91                 self.id_chain.append((info['id'], info['title'])) 
     158                self.id_chain.append(info['url']) 
    92159                     
    93     def normalize_actions(self, category, object, level): 
    94         """walk through the tabs dictionary and build list of all tabs""" 
    95         for info in self._actionInfos(category, object): 
    96             children = [] 
    97             bottomLevel = self.conf.actions_tabs_level 
    98             if bottomLevel < 1 or level < bottomLevel: 
    99                 # try to find out appropriate subcategory 
    100                 subcat_id = self.cat_prefix + info['id'] + self.cat_sufix 
    101                 if subcat_id != info['id'] and \ 
    102                    subcat_id in category.objectIds(): 
    103                     subcat = category._getOb(subcat_id) 
    104                     if IActionCategory.providedBy(subcat): 
    105                         children = self.normalize_actions(subcat, object, level+1) 
    106              
    107             parent_id = category['id'].replace(self.cat_prefix,'').replace(self.cat_sufix,'') 
    108             tab = {'id' : info['id'], 
    109                'title': info['title'], 
    110                'url': info['url'], 
    111                'currentItem' : False, 
    112                'parent': (parent_id, category['title'])} 
    113             self.tabs.append(tab) 
    114  
    115     def _subactions(self, category, object, level=0): 
    116         """Build tabs dictionary out of portal actions""" 
    117         tabs = [] 
    118         for info in self._actionInfos(category, object): 
    119             icon = info['icon'] and '<img src="%s" />' % info['icon'] or '' 
    120             # look up children for a given action 
    121             children = [] 
    122             bottomLevel = self.conf.actions_tabs_level 
    123             if bottomLevel < 1 or level < bottomLevel: 
    124                 # try to find out appropriate subcategory 
    125                 subcat_id = info['id'] 
    126                 subcat_id = self.cat_prefix + info['id'] + self.cat_sufix 
    127                 if subcat_id != info['id'] and \ 
    128                    subcat_id in category.objectIds(): 
    129                     subcat = category._getOb(subcat_id) 
    130                     if IActionCategory.providedBy(subcat): 
    131                         children = self._subactions(subcat, object, level+1) 
    132  
    133             active = False 
    134             if (info['id'], info['title']) in self.id_chain: 
    135                 active = True 
    136             # make up final tab dictionary 
    137             tab = {'Title': info['title'], 
    138                    'Description': info['description'], 
    139                    'getURL': info['url'], 
    140                    'show_children': len(children) > 0, 
    141                    'children': children, 
    142                    'currentItem': active,  
    143                    'currentParent': active, 
    144                    'item_icon': {'html_tag': icon}, 
    145                    'normalized_review_state': 'visible'} 
    146             tabs.append(tab) 
    147         return tabs 
     160 
    148161 
    149162    def _actionInfos(self, category, object, check_visibility=1, 
Note: See TracChangeset for help on using the changeset viewer.