Changeset 2485


Ignore:
Timestamp:
11/16/07 17:12:35 (8 years ago)
Author:
ewr119@…
Message:
  • Now you can say currentItem=None to forgo item highlighting altogether.
  • Added instructions for Plone 3. This seems to run fine under Plone 3, but none of the tests pass.
  • Refactored tests a little and added some for various currentItem cases.
Location:
weblion/CustomNav/trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • weblion/CustomNav/trunk/Extensions/nav.py

    r1939 r2485  
    11from Products.CMFPlone.browser.navtree import buildFolderTree, DefaultNavtreeStrategy, NavtreeQueryBuilder 
    22from Products.CMFCore.utils import getToolByName 
     3 
     4 
     5_marker = object() 
    36 
    47 
     
    1215 
    1316 
    14 def customNavtree(self, basePath='.', types=None, currentItem=None, filter=None): 
     17def customNavtree(self, basePath='.', types=None, currentItem=_marker, filter=None): 
    1518    """Return a navtree suitable for passing to portlet_navtree_macro(). 
    1619     
     
    4043            * To default to `context` (that is, whatever the user is presently 
    4144              looking at), omit altogether. 
     45            * To highlight no "current item" at all, pass currentItem=None. 
    4246        filter -- 
    4347            A catalog query to further refine the results 
     
    7377     
    7478    # Normalize args: 
    75     if not currentItem:  # If no explicit currentItem is passed in, default to whatever the user is looking at. 
     79    if currentItem is _marker:  # If no explicit currentItem is passed in, default to whatever the user is looking at. 
    7680        currentItem = context 
    7781    if basePath == '.': 
     
    8791    # And then pave over the parts we have differing opinions on: 
    8892    query.update(filter) 
     93     
    8994    # We normally put the path of currentItem in the query (or the nearest folder above it, if it's not folderish), so we get its children, which we want to show. If currentItem isn't within absoluteBasePath, however, query for absoluteBasePath instead; otherwise, by querying for currentItem, we won't find the contents of absoluteBasePath at all, and the rootPath restriction applied later by our _CustomStrategy will filter everything out, leaving the tree empty. 
    90     pathToQuery = isBeneath(absoluteBasePath.split('/'), currentItem.getPhysicalPath()) and '/'.join(nearestFolderAbove(currentItem).getPhysicalPath()) or absoluteBasePath 
     95    if not currentItem or not isBeneath(absoluteBasePath.split('/'), currentItem.getPhysicalPath()): 
     96        pathToQuery = absoluteBasePath 
     97    else: 
     98        pathToQuery = '/'.join(nearestFolderAbove(currentItem).getPhysicalPath()) 
     99     
    91100    depthOfBasePathBelowPortal = len(absoluteBasePath.split('/')) - len(portalPath.split('/'))  # 1 if portal root is /portal and absoluteBasePath is /portal/someFolder 
    92101    query['path'].update({'query': pathToQuery, 'navtree_start': depthOfBasePathBelowPortal + 1}) 
    93102    if types: 
    94103        query['portal_type'] = types 
    95  
     104     
    96105    return buildFolderTree(context, obj=currentItem, query=query, strategy=_CustomStrategy(context, absoluteBasePath)) 
  • weblion/CustomNav/trunk/README.txt

    r2148 r2485  
    4040        2. Click the Properties tab. 
    4141 
    42         3. Add 'here/portlet_custom_navigation_example/macros/portlet' to 
    43            **left_slots**, and click Save Changes. 
     42        3. If you are using Plone 2.5... 
     43             
     44               1. Add 'here/portlet_custom_navigation_example/macros/portlet' to 
     45                  **left_slots**, and click Save Changes. 
     46                
     47        4. If you are using Plone 3.0... 
     48                
     49               1. On the page in your site where you want the portlet to appear, click Manage Portlets. 
     50                
     51               2. From the *Add portlet...* menu, choose *Classic portlet*. 
     52                
     53               3. For *Template*, enter "portlet_custom_navigation_example"; for *Macro*, enter "portlet". 
     54                
     55               4. Click *Save*. 
    4456 
    4557        4. Click the View tab. On the left side of the page, there should be a 
    4658           portlet called "customnav example: member folders" showing the 
    4759           contents of the Members folder. 
     60  
    4861     
    4962    Customize the Portlet 
     
    7386Version History 
    7487 
     88    ' ' 1.1 -- ' ' 
     89                
     90               * Now you can forgo highlighting a currentItem altogether. Just 
     91                 pass 'currentItem=None' to 'customNavtree'. 
     92                
     93               * Spews fewer warnings on startup. 
     94     
    7595    ' ' 1.0 -- Initial release 
    7696 
  • weblion/CustomNav/trunk/tests/test_nav.py

    r1833 r2485  
    2424        manage_addExternalMethod(self.portal, 'customNavtree', 'customNavtree', 'CustomNav.nav', 'customNavtree') 
    2525        self.logout() 
     26        self.customNavTree = self.portal.customNavtree 
     27        self.misterFolder = self.portal['misterfolder'] 
    2628     
    2729    def _failUnlessShowingStuffInMisterFolderOnly(self, tree): 
     
    3335     
    3436    def testFromAboveBasePath(self): 
    35         tree = self.portal.customNavtree(self.portal, basePath='/misterfolder') 
     37        tree = self.customNavTree(self.portal, basePath='/misterfolder') 
    3638        self._failUnlessShowingStuffInMisterFolderOnly(tree) 
    3739 
    3840    def testFromAtBasePath(self): 
    39         tree = self.portal.customNavtree(self.portal.misterfolder, basePath='/misterfolder') 
     41        tree = self.customNavTree(self.misterFolder, basePath='/misterfolder') 
    4042        self._failUnlessShowingStuffInMisterFolderOnly(tree) 
    4143     
     
    4951    def testFrom1BelowBasePath(self): 
    5052        """When you're in missfolder, missfolder's children should be disclosed, and missfolder should be highlighted.""" 
    51         tree = self.portal.customNavtree(self.portal.misterfolder.missfolder, basePath='/misterfolder') 
     53        tree = self.customNavTree(self.misterFolder['missfolder'], basePath='/misterfolder') 
    5254        firstLevelChildren = tree['children'] 
    5355        missFolder = firstLevelChildren[0] 
     
    5860    def testFrom3BelowBasePath(self): 
    5961        """When you're in kidderfolder, its and kidfolder's children should be disclosed, and kidderfolder should be highlighted.""" 
    60         tree = self.portal.customNavtree(self.portal.misterfolder.missfolder.kidfolder.kidderfolder, basePath='/misterfolder') 
     62        tree = self.customNavTree(self.misterFolder['missfolder']['kidfolder']['kidderfolder'], basePath='/misterfolder') 
    6163        firstLevelChildren = tree['children'] 
    6264        missFolder = firstLevelChildren[0] 
     
    8183        navtreeProperties.metaTypesNotToList = navtreeProperties.metaTypesNotToList + ('Document',)  # Quit showing Documents. 
    8284        try: 
    83             tree = self.portal.customNavtree(self.portal.misterfolder, basePath='/misterfolder') 
     85            tree = self.customNavTree(self.misterFolder, basePath='/misterfolder') 
    8486            self.failUnless(len(tree['children']) == 1)  # Should be showing Miss Folder but not Mister's Page 
    8587        finally: 
     
    8890    def testCustomTypeHiding(self): 
    8991        """See if the `types` arg works.""" 
    90         tree = self.portal.customNavtree(self.portal.misterfolder, basePath='/misterfolder', types=['Document']) 
     92        tree = self.customNavTree(self.misterFolder, basePath='/misterfolder', types=['Document']) 
    9193        firstLevelChildren = tree['children'] 
    9294        self.failUnless(len(firstLevelChildren) == 1) 
     
    9597    def testDefaultBasePath(self): 
    9698        """Test the default case: basePath='.'""" 
    97         tree = self.portal.customNavtree(self.portal.misterfolder) 
     99        tree = self.customNavTree(self.misterFolder) 
    98100        firstLevelChildren = tree['children'] 
    99101        self._assertContract(firstLevelChildren[0], 'missfolder', False, False, 1) 
     102        self._assertContract(firstLevelChildren[1], 'misterspage', False, False, 1) 
     103     
     104    def testCurrentItemBeneathContext(self): 
     105        """Test passing as `currentItem` something within the subtree rooted at the current browser location.""" 
     106        tree = self.customNavTree(self.misterFolder, currentItem=self.misterFolder['missfolder']) 
     107        firstLevelChildren = tree['children'] 
     108        self.failUnless(len(firstLevelChildren) == 2) 
     109        self._assertContract(firstLevelChildren[0], 'missfolder', True, False, 1) 
     110        self._assertContract(firstLevelChildren[1], 'misterspage', False, False, 1) 
     111     
     112    def testCurrentItemOutsideContext(self): 
     113        """Test passing as `currentItem` something above the subtree rooted at the current browser location.""" 
     114        tree = self.customNavTree(self.misterFolder, currentItem=self.portal) 
     115        firstLevelChildren = tree['children'] 
     116        self.failUnless(len(firstLevelChildren) == 2) 
     117        self._assertContract(firstLevelChildren[0], 'missfolder', False, False, 1) 
     118        self._assertContract(firstLevelChildren[1], 'misterspage', False, False, 1) 
     119     
     120    def testNoCurrentItem(self): 
     121        """Test passing currentItem=None. 
     122         
     123        This is a lot like testFrom1BelowBasePath().""" 
     124        tree = self.customNavTree(self.misterFolder['missfolder'], basePath='/misterfolder', currentItem=None) 
     125        firstLevelChildren = tree['children'] 
     126        self.failUnless(len(firstLevelChildren) == 2) 
     127        self._assertContract(firstLevelChildren[0], 'missfolder', False, False, 1)  # See? It's not the current item, even though we're at it. 
    100128        self._assertContract(firstLevelChildren[1], 'misterspage', False, False, 1) 
    101129 
Note: See TracChangeset for help on using the changeset viewer.