[Zope3-Users] practical example of migrating from services to utilities

Andreas Reuleaux reuleaux at web.de
Mon Aug 8 16:52:40 EDT 2005


On Mon, Aug 08, 2005 at 01:57:21PM -0600, Duncan McGreggor wrote:
> Hey all,
> 
> I just wanted to check to make sure I was doing this right, and if so, 
> provide a resource that would turn up on subsequent googling. This 
> example involves the getting menus items with the getService/getUtility 
> functions and is taken from Philip's worldcookery example code.
> 
> Here's the pertinent zcml (worldcookery/browser/configure.zcml):
> 
>   <browser:menu
>       id="alternate_views"
>       title="Menu containing a list of alternative views for an object"
>       />
> 
>   <browser:addMenuItem
>       title="[label-recipe] Recipe"
>       class="worldcookery.recipe.Recipe"
>       permission="worldcookery.EditRecipes"
>       view="AddRecipe.html"
>       />
> 
> Here's the original code (worldcookery/browser/recipe.py):
> 
>   def alternateViews(self):
>     menu_service = zapi.getService(servicenames.BrowserMenu)
>     menu_id = 'alternate_views'
>     return menu_service.getMenu(menu_id, self.context, self.request)
> 
> Here's the modified example that I'd want to check if I am doing 
> correctly:
> 
>   def alternateViews(self):
>     browser_menu = zapi.getUtility(IBrowserMenu, 'alternate_views')
>     return browser_menu.getMenuItems(self.context, self.request)
> 
> Am I doing this properly?
> ...


I haven't tried  your solution

  zapi.getUtility(IBrowserMenu, 'alternate_views')

(I doubt it works) but I had the same problem some time ago, and
Philipp had pointed out to me that there are simpler solutions now -
both of them worked fine for me (his original e-mail to me in German
below, the English translation is mine):

--------------------

There is no global browser menu service available any more. And there is 
no corresponding utility, as for most of the other services. Menu items
are simply adapters in Zope 3.1. The helper function getMenu
from zope.app.publisher.browser.menu is handy for the lookup.
The method alternateViews() from example 12.3.3 can now be written as

  def alternateViews(self):
      menu_id = 'alternate_views'
      return getMenu(menu_id, self.context, self.request)

Besides that there is an even easier to use view
"view_get_menu" (that Philip regrets to not have mentioned in his book).
Example 12.3.4 could be written as

  <li tal:repeat="item context/@@view_get_menu/alternate_views">
    ...
  </li>

This worked in Zope 3.0 already (and has not changed in Zope 3.1)
It can also be recommended as there is no requirement for an additional
method in a view class.

Regards, Andreas

--------------------

(from Philips E-mail:)
Der Global Browser Menu Service ist komplett weggefallen. Eine
Entsprechung als Utility, wie das mit den meisten anderen Services der
Fall ist, gibt ebenfalls nicht. Menü-Einträge sind unter Zope 3.1
einfache Adapter. Die Hilfsfunktion getMenu aus dem
zope.app.publisher.browser.menu Modul erleichtert das Lookup. Die
Methode alternateViews() in Beispiel 12.3.3 könnte nun folgendermaßen
implementiert werden:

  def alternateViews(self):
      menu_id = 'alternate_views'
      return getMenu(menu_id, self.context, self.request)

Es gibt übrigens auch den etwas leichter zu bedienden View
"view_get_menu", den ich leider vergessen habe zu behandeln. So könnte
Beispiel 12.3.4 ab Zeile 20 auch heißen:

  <li tal:repeat="item context/@@view_get_menu/alternate_views">
    ...
  </li>

Diese Methode ist transparent gegenüber den Änderungen von X3.0 zu 3.1
und insofern auch empfehlenswert, allein schon weil eine zusätzliche
Methode in der View-Klasse entfällt.


More information about the Zope3-users mailing list