ManageUser: Admin edit's user details
[myslice.git] / unfold / composite.py
1 from unfold.plugin import Plugin
2
3 class Composite (Plugin):
4
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
10 """
11
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()
18         
19     def check_active_domid(self):
20         matches= [ son for son in self.sons if son.domid==self.active_domid ]
21         if len(matches)!=1: 
22             print "WARNING: %s has %d valid son(s) for being active - expecting 1, resetting"%\
23                 (self,len(matches))
24             self.active_domid=None
25         
26     def insert (self, plugin):
27         self.sons.append(plugin)
28
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))
37         env = { 'sons':
38                  [ { 'rendered': son.render(request),
39                      'rank': rank,
40                      'is_active': is_active(son,rank),
41                      'title': son.title,
42                      'domid': son.domid,
43                      'classname': son.classname,
44                      }
45                    for (son,rank) in zip(self.sons,ranks) ]}
46         return env
47