1 from unfold.plugin import Plugin
3 class Composite (Plugin):
5 """a simple base class for plugins that contain/arrange a set of other plugins
6 sons is expected to be a list of the contained plugins, and
7 active_domid is the domid for the one son that should be displayed as active
8 some subclasses of Composite, like e.g. Tabs, will not behave as expected
9 if a valid active_domid is not provided
12 def __init__ (self, sons=None, active_domid=None, *args, **kwds):
13 Plugin.__init__ (self, *args, **kwds)
14 self.sons= sons if sons else []
15 self.active_domid=active_domid
16 # make sure this is valid, unset otherwise, so we always have exactly one active
17 self.check_active_domid()
19 def check_active_domid(self):
20 matches= [ son for son in self.sons if son.domid==self.active_domid ]
22 print "WARNING: %s has %d valid son(s) for being active - expecting 1, resetting"%\
24 self.active_domid=None
26 def insert (self, plugin):
27 self.sons.append(plugin)
29 def template_env (self, request):
30 # this is designed so as to support a template like
31 # {% for son in sons %} {{ son.rendered }} ...
32 def is_active (son,rank):
33 # if active_domid is not specified, make the first one active
34 if not self.active_domid: return rank==0
35 return son.domid==self.active_domid
36 ranks=range(len(self.sons))
38 [ { 'rendered': son.render(request),
40 'is_active': is_active(son,rank),
43 'classname': son.classname,
45 for (son,rank) in zip(self.sons,ranks) ]}