source: products/quintagroup.plonetabs/trunk/quintagroup/plonetabs/utils.py @ 3607

Last change on this file since 3607 was 3607, checked in by vmaksymiv, 11 years ago

replaced wrong link

File size: 2.1 KB
Line 
1""" This module dedicated to work with viewlets. """
2from Acquisition import aq_inner
3
4from plone.app.customerize import registration
5
6from zope.publisher.interfaces.browser import IBrowserRequest
7from zope.viewlet.interfaces import IViewlet
8
9
10# TODO: Methods 'getViewletByName' and 'setupViewletByName' were copied
11# from https://github.com/collective/collective.developermanual/blob/master/source/views/viewlets.rst#rendering-viewlet-by-name
12# Better solution would be to use collective.fastview
13# (http://svn.plone.org/svn/collective/collective.fastview/trunk/)
14# which has not yet included in plone.
15
16def getViewletByName(name):
17    """ Viewlets allow through-the-web customizations.
18
19    Through-the-web customization magic is managed by five.customerize.
20    We need to think of this when looking up viewlets.
21
22    @return: Viewlet registration object
23    """
24    views = registration.getViews(IBrowserRequest)
25
26    for v in views:
27
28        if v.provided == IViewlet:
29            # Note that we might have conflicting BrowserView with the same name,
30            # thus we need to check for provided
31            if v.name == name:
32                return v
33
34    return None
35
36
37def setupViewletByName(view, context, request, name):
38    """ Constructs a viewlet instance by its name.
39
40    Viewlet update() and render() method are not called.
41
42    @return: Viewlet instance of None if viewlet with name does not exist
43    """
44    context = aq_inner(context)
45
46    # Perform viewlet regisration look-up
47    # from adapters registry
48    reg = getViewletByName(name)
49    if reg is None:
50        return None
51
52    # factory method is responsible for creating the viewlet instance
53    factory = reg.factory
54
55    # Create viewlet and put it to the acquisition chain
56    # Viewlet need initialization parameters: context, request, view
57    try:
58        viewlet = factory(context, request, view, None).__of__(context)
59    except TypeError:
60        # Bad constructor call parameters
61        raise RuntimeError("Unable to initialize viewlet %s. Factory method %s call failed." % (name, str(factory)))
62
63    return viewlet
Note: See TracBrowser for help on using the repository browser.