Skip to content
This repository
Browse code

display menu now correct. just need to unregister views when they change

  • Loading branch information...
commit 98540ccf6a01e04aa1d0f8eb4158feb12eed70e7 1 parent 0b8d6b8
authored November 13, 2012
9  README.rst
Source Rendered
@@ -239,3 +239,12 @@ possible future directions
239 239
 - support hierarchical listing
240 240
 - support ajax batching
241 241
 - support infinite lists (auto load next when scrolled down)
  242
+
  243
+Dynamic adding to displaymenu
  244
+=============================
  245
+
  246
+https://github.com/plone/Products.CMFDynamicViewFTI/blob/master/Products/CMFDynamicViewFTI/browserdefault.py
  247
+https://github.com/plone/plone.app.customerize/blob/master/plone/app/customerize/browser.py
  248
+        sm = getSiteManager(self.context)
  249
+        sm.registerAdapter(viewzpt, required = reg.required,
  250
+                           provided = reg.provided, name = reg.name)
5  buildout.cfg
@@ -7,6 +7,8 @@ extends =
7 7
 #    conf/test-3.3.x.cfg
8 8
     conf/test-4.2.x.cfg
9 9
 
  10
+versions=versions
  11
+
10 12
 [plone]
11 13
 eggs +=
12 14
     collective.listingviews
@@ -18,3 +20,6 @@ recipe = zc.recipe.testrunner
18 20
 eggs = 
19 21
   ${plone:eggs}
20 22
   plone.app.collection
  23
+
  24
+[versions]
  25
+zc.buildout = 1.6.3
2  conf/test-4.2.x.cfg
@@ -7,7 +7,7 @@ extensions =
7 7
     buildout.bootstrap
8 8
     buildout.dumppickedversions
9 9
     mr.developer
10  
-extends = http://dist.plone.org/release/4.2rc2/versions.cfg
  10
+extends = http://dist.plone.org/release/4.2-latest/versions.cfg
11 11
 find-links = 
12 12
     http://dist.plone.org/thirdparty/elementtree-1.2.7-20070827-preview.zip
13 13
     http://downloads.sourceforge.net/project/docutils/docutils/0.9/docutils-0.9.tar.gz
5  src/collective/listingviews/browser/views/configure.zcml
@@ -50,4 +50,9 @@
50 50
 	  action="listing_views"
51 51
       description="A custom listing view for folder and collection." />
52 52
 
  53
+    <subscriber
  54
+        for="Products.CMFPlone.interfaces.IPloneSiteRoot
  55
+             zope.app.publication.interfaces.IBeforeTraverseEvent"
  56
+        handler=".controlpanel.registerMenuItems"
  57
+        />
53 58
 </configure>
96  src/collective/listingviews/browser/views/controlpanel.py
@@ -5,13 +5,30 @@
5 5
 from zope.interface import implements
6 6
 from plone.registry.interfaces import IRegistry
7 7
 from zope.component import queryUtility
8  
-from zope.component import adapts, getUtility
  8
+from zope.component import adapts, getUtility, getAdapters
  9
+from zope.component import getSiteManager
  10
+from zope.component import getGlobalSiteManager
  11
+from zope.browser.interfaces import IBrowserView
  12
+from zope.publisher.interfaces.browser import IBrowserRequest
  13
+from zope.publisher.interfaces.browser import IDefaultBrowserLayer
  14
+from Products.CMFCore.interfaces import IFolderish, IContentish
9 15
 from collective.listingviews.utils import ComplexRecordsProxy
  16
+from five.customerize.zpt import TTWViewTemplate
  17
+from collective.listingviews.browser.views.listing_view import ListingView
  18
+from Products.CMFCore.utils import getToolByName
  19
+from zope.browsermenu.interfaces import IBrowserMenu
  20
+from zope.browsermenu.metaconfigure import addMenuItem, MenuItemFactory, menuItemDirective
  21
+from zope.browsermenu.menu import BrowserMenu, BrowserMenuItem, BrowserSubMenuItem
  22
+from zope.browsermenu.interfaces import IBrowserMenuItem
  23
+from Products.ATContentTypes.permission import ModifyViewTemplate
10 24
 
11 25
 class ListingControlPanel(object):
12 26
     implements(IListingControlPanel)
13 27
 
14 28
 
  29
+
  30
+
  31
+
15 32
 class ListingControlPanelForm(controlpanel.RegistryEditForm):
16 33
 
17 34
     schema = IListingControlPanel
@@ -24,6 +41,83 @@ def getContent(self):
24 41
                                     key_names={'views':'id'})
25 42
         return proxy
26 43
 
  44
+    def applyChanges(self, data):
  45
+        # for each view we will create a new view in customerize and add that as a menu
  46
+        # item in the display menu
  47
+        sm = getSiteManager(self.context)
  48
+
  49
+        portal_types = getToolByName(self.context, "portal_types")
  50
+
  51
+        for view in data['views']:
  52
+            view_name = 'collective.listingviews.%s'%view.id
  53
+            sm.registerAdapter(ListingView,
  54
+                               required = (IFolderish, IBrowserRequest),
  55
+                               provided = IBrowserView,
  56
+                               name = view_name)
  57
+
  58
+            # add view to the relevent types
  59
+            for type_ in ['Folder','Topic']:
  60
+                fti = portal_types.getTypeInfo(type_)
  61
+                if view_name not in fti.view_methods:
  62
+                    fti.manage_changeProperties(view_methods=fti.view_methods+(view_name,))
  63
+
  64
+            # registering a menu item will be done in beforeSiteTraverse event
  65
+
  66
+
  67
+        #TODO unregister any old views
  68
+
  69
+        super(ListingControlPanelForm, self).applyChanges(data)
  70
+
  71
+
  72
+        # register all the menu names again from registery
  73
+        _registerMenuItems()
  74
+
  75
+
  76
+# We need to register our menuitems the first time it's accessed
  77
+def registerMenuItems(site, event, _handled=set()):
  78
+    if site.getPhysicalPath() not in _handled:
  79
+        _registerMenuItems()
  80
+        _handled.add(site.getPhysicalPath())
  81
+
  82
+        
  83
+def _registerMenuItems():
  84
+
  85
+    reg = getUtility(IRegistry)
  86
+    proxy = ComplexRecordsProxy(reg, IListingControlPanel, prefix='collective.listingviews',
  87
+                                key_names={'views':'id'})
  88
+    gsm = getGlobalSiteManager()
  89
+    menu = getUtility(IBrowserMenu, 'plone_displayviews')
  90
+    for view in proxy.views:
  91
+        # register a menu item
  92
+        view_name = 'collective.listingviews.%s'%view.id
  93
+        factory = MenuItemFactory(
  94
+            BrowserMenuItem,
  95
+            title=view.name,
  96
+            action=view_name,
  97
+            #description=description,
  98
+            # icon=icon,
  99
+            #filter=filter, permission=permission, extra=extra, order=order,
  100
+    #                    _for=(IContentish, IDefaultBrowserLayer)
  101
+            )
  102
+        # ensure we remove our old factory if already registered
  103
+        gsm.unregisterAdapter(
  104
+            required = (IFolderish, IDefaultBrowserLayer),
  105
+            provided = menu.getMenuItemType(),
  106
+            name = view.name,
  107
+        )
  108
+
  109
+        gsm.registerAdapter(
  110
+            factory,
  111
+            required = (IFolderish, IDefaultBrowserLayer),
  112
+            provided = menu.getMenuItemType(),
  113
+            name = view.name,
  114
+        )
  115
+
  116
+
  117
+        #assert menu.getMenuItemByAction(IFolderish, self.request, view_name)
  118
+        # pp [x for x in gsm.registeredAdapters() if x.provided == menu.getMenuItemType()]
  119
+
  120
+
27 121
 
28 122
 class ListingCustomFieldControlPanel(object):
29 123
     implements(IListingCustomFieldControlPanel)
20  src/collective/listingviews/browser/views/listing_view.py
@@ -6,15 +6,29 @@
6 6
 #from collective.listingviews.utils import getListingAdapter
7 7
 from collective.listingviews.adapters import BasicAdapter
8 8
 
  9
+class TempSettings(object):
  10
+    listing_choice = None
  11
+
  12
+
9 13
 
10 14
 class ListingView(BrowserView):
11 15
     """
12  
-    View for file will redirect to download if user can't edit it.
  16
+    
13 17
     """
14 18
     implements(IListingViews)
15  
-    select_listing_view = ViewPageTemplateFile("templates/layout.pt")
  19
+    index = ViewPageTemplateFile("templates/layout.pt")
16 20
 
17 21
     def __init__(self, context, request):
18 22
         super(ListingView, self).__init__(context, request)
  23
+        view_name = request.getURL().split('/')[-1]
  24
+        listing_view_name = view_name.rsplit('.',1)[-1]
  25
+        #self.settings = ListingSettings(self.context, interfaces=[self.adapter.schema])
  26
+        self.settings = TempSettings()
  27
+        self.settings.listing_choice = listing_view_name
19 28
         self.adapter = BasicAdapter(self.context, self.request)
20  
-        self.settings = ListingSettings(self.context, interfaces=[self.adapter.schema])
  29
+
  30
+    def render(self):
  31
+        return self.index()
  32
+
  33
+    def __call__(self):
  34
+        return self.render()

0 notes on commit 98540cc

Please sign in to comment.
Something went wrong with that request. Please try again.