Plugin now has __repr__
authorThierry Parmentelat <thierry.parmentelat@inria.fr>
Fri, 22 Mar 2013 09:44:45 +0000 (10:44 +0100)
committerThierry Parmentelat <thierry.parmentelat@inria.fr>
Fri, 22 Mar 2013 09:44:45 +0000 (10:44 +0100)
tweak Composite to ensure it has exactly one active son
add checkboxes=True to Hazelnut, although not tested..
slice view now has 2 hazelnuts in a tabs, with and without checkboxes

12 files changed:
myslice/viewutils.py
plugins/hazelnut/hazelnut.html
plugins/hazelnut/hazelnut.py
plugins/tabs/tabs.html
plugins/tabs/tabs.py
trash/pluginview.py
trash/sampleviews.py
trash/sliceview.py
trash/templates/view-tab.html
unfold/composite.py
unfold/plugin.py
views/templates/widget-topmenu.html

index 669c4ca..29ee071 100644 (file)
@@ -16,7 +16,7 @@ standard_topmenu_items = [
 def topmenu_items (current,request=None):
     result=deepcopy(standard_topmenu_items)
     for d in result:
-        if d['label'].lower().find(current)>=0: d['active']=True
+        if d['label'].lower().find(current)>=0: d['is_active']=True
     if not request: return result
     has_user=request.user.is_authenticated()
 #    result.append (login_out_items [ has_user] )
index cbaac59..f3b3573 100644 (file)
@@ -1,6 +1,6 @@
 <table class='display' id='hazelnut-{{ domid }}'>
 <thead><tr> {% for subject_field in subject_fields %}
-<th>{{ subject_field }}</th>{% endfor %}
+<th>{{ subject_field }}</th>{% endfor %} {% if checkboxes %}<th>+/-</th>{% endif %}
 </tr></thead>
 <tbody>
 </tbody>
index 60c9ec9..3d748e8 100644 (file)
@@ -2,9 +2,10 @@ from unfold.plugin import Plugin
 
 class Hazelnut (Plugin):
 
-    def __init__ (self, query, **settings):
+    def __init__ (self, query, checkboxes=False, **settings):
         Plugin.__init__ (self, **settings)
         self.query=query
+        self.checkboxes=checkboxes
 
     def template_file (self):
         return "hazelnut.html"
index 7c387d8..cdf73ac 100644 (file)
@@ -1,12 +1,11 @@
-{# could try to set class='active' on the one we want to highlight #}
 <ul class="nav nav-tabs" id='tab-{{ domid }}'>
 {% for son in sons %}
-<li{% if son.active %} class='active'{% endif %}> <a href="#{{ son.domid }}" data-toggle="tab">{{ son.title }}</a> </li>
+<li{% if son.is_active %} class='active'{% endif %}> <a href="#{{ son.domid }}" data-toggle="tab">{{ son.title }}</a> </li>
 {% endfor %}
 </ul><!--nav-tabs-->
 <div class="tab-content">
 {% for son in sons %}
-<div class="tab-pane fade in{% if son.active %} active{% endif %}" id="{{ son.domid }}">
+<div class="tab-pane fade in{% if son.is_active %} active{% endif %}" id="{{ son.domid }}">
 {{ son.rendered }}
 </div><!--tab-pane-->
 {% endfor %}
index 211af42..0350c26 100644 (file)
@@ -10,3 +10,5 @@ class Tabs (Composite):
     def template_file (self):
         return "tabs.html"
 
+    # see Composite.py for the details of template_env, that exposes global
+    # 'sons' as a list of sons with each a set of a few attributes
index a7269b9..c836c76 100644 (file)
@@ -38,20 +38,27 @@ def test_plugin_view (request):
                                      toggled=False),
                          Tabs (page=page,
                                title='Sample Tabs',
+                               domid='test-tabs',
                                # *** we select this one to be the active tab ***
-                               active='raw2',
+                               active_domid='son2',
                                sons = [ Raw (page=page,
-                                             title='a raw plugin',domid='raw1',
+                                             title='a raw plugin',
+                                             domid='son0',
                                              togglable=False,
-                                             html= 3*lorem_p),
+                                             html= 3*lorem_p,
+                                             ),
                                         StaticList(page=page,
                                                    title='a slice list',
+                                                   domid='son1',
                                                    togglable=False,
                                                    header="static list but not togglable",
-                                                   list=hard_wired_slice_names),
+                                                   list=hard_wired_slice_names,
+                                                   ),
                                         Raw (page=page,
-                                             title='raw title',domid='raw2',
-                                             togglable=False,html=lorem) ]),
+                                             title='raw title',
+                                             domid='son2',
+                                             togglable=False,
+                                             html=lorem) ]),
                          StaticList (page=page,
                                      title='SimpleList with slice names', 
                                      list=hard_wired_slice_names,
index 5e3c942..e268778 100644 (file)
@@ -14,13 +14,13 @@ from myslice.viewutils import lorem, hard_wired_slice_names
 def tab_view (request):
     prelude=Prelude( js_files='js/bootstrap.js', css_files='css/bootstrap.css')
     prelude_env = prelude.prelude_env()
+
     tab_env = {'title':'Page for playing with Tabs',
                'topmenu_items': topmenu_items('tab',request),
                'username':the_user (request),
                'lorem': lorem,                                
                }
     tab_env.update (prelude_env)
-
     return render_to_response ('view-tab.html', tab_env,
                                context_instance=RequestContext(request))
 
index a410a71..4f4202d 100644 (file)
@@ -9,6 +9,7 @@ from unfold.page import Page
 from manifold.manifoldquery import ManifoldQuery
 
 from plugins.stack.stack import Stack
+from plugins.tabs.tabs import Tabs
 from plugins.hazelnut.hazelnut import Hazelnut 
 from plugins.lists.slicelist import SliceList
 from plugins.querycode.querycode import QueryCode
@@ -41,20 +42,36 @@ def slice_view (request, slicename=tmp_default_slice):
     main_plugin = Stack (
         page=page,
         title="global container",
-        sons=[ 
-            Hazelnut ( # setting visible attributes first
+        togglable=False,
+        sons=[Tabs (
                 page=page,
-                title='a sample and simple hazelnut',
-                # this is the query at the core of the slice list
-                query=main_query,
-                ),
-            QueryCode (
+                title="different angles",
+                active_domid='with-checkboxes',
+                sons=[
+                    Hazelnut ( 
+                        page=page,
+                        title='a sample and simple hazelnut',
+                        togglable=False,
+                        # this is the query at the core of the slice list
+                        query=main_query,
+                        ),
+                    Hazelnut ( 
+                        page=page,
+                        title='with checkboxes',
+                        domid='with-checkboxes',
+                        togglable=False,
+                        checkboxes=True,
+                        # this is the query at the core of the slice list
+                        query=main_query,
+                        ),
+                    ]),
+              QueryCode (
                 page=page,
                 title='xmlrpc code',
                 query=main_query,
                 toggled=False,
                 ),
-            ])
+              ])
 
     # variables that will get passed to the view-plugin.html template
     template_env = {}
@@ -65,7 +82,7 @@ def slice_view (request, slicename=tmp_default_slice):
     # more general variables expected in the template
     template_env [ 'title' ] = 'Test view for hazelnut'
     # the menu items on the top
-    template_env [ 'topmenu_items' ] = topmenu_items('hazelnut', request) 
+    template_env [ 'topmenu_items' ] = topmenu_items('slice', request) 
     # so we can sho who is logged
     template_env [ 'username' ] = the_user (request) 
 
index 1e48e5c..2cc6817 100644 (file)
@@ -4,7 +4,7 @@
 
 {% insert_str prelude "css/sample.css" %}
 {% insert prelude_js %}
-$(function(){console.log('hey!');$('#tooltipid').tooltip({'selector':'','placement':'bottom'});});
+$(function(){$('#tooltipid').tooltip({'selector':'','placement':'bottom'});});
 {% endinsert %}
 
 <div id=sample-tabs>
@@ -32,7 +32,7 @@ a first <a id='tooltipid' href="#" data-toggle="tooltip" data-original-title="wh
 
 Let us now try to attach <button id='mylougout' class='btn btn-danger' data='mydata'>a click function to a button</button>
 <script>
-function foo() {alert ('foo' + $(this).attr('data'));}
+function foo() {alert ('foo ' + $(this).attr('data'));}
 $(function() {$('#mylougout').click(foo);})
 </script>
 
index 409b1bc..ffa6de9 100644 (file)
@@ -1,27 +1,42 @@
 from unfold.plugin import Plugin
 
+# this is a simple base class for plugins that contain/arrange a set of other plugins
+# sons is expected to be a list of the contained plugins, and 
+# active_domid is the domid for the one son that should be displayed as active
+# some subclasses of Composite, like e.g. Tabs, will not behave as expected 
+# if a valid active_domid is not be provided
+
 class Composite (Plugin):
 
-    def __init__ (self, sons=[], active=None, *args, **kwds):
+    def __init__ (self, sons=[], active_domid=None, *args, **kwds):
         Plugin.__init__ (self, *args, **kwds)
         self.sons=sons
-        self.active=active
+        self.active_domid=active_domid
+        # make sure this is valid, unset otherwise, so we always have exactly one active
+        self.check_active_domid()
+        
+    def check_active_domid(self):
+        matches= [ son for son in self.sons if son.domid==self.active_domid ]
+        if len(matches)!=1: 
+            print "WARNING: %s has %d valid son(s) for being active - expecting 1, resetting"%\
+                (self,len(matches))
+            self.active_domid=None
         
     def insert (self, plugin):
         self.sons.append(plugin)
 
-    # xxx currently there is no guarantee that exactly one son will be active
     def template_env (self, request):
         # this is designed so as to support a template like
         # {% for son in sons %} {{ son.rendered }} ...
-        def is_active (son):
-#            print 'comparing >%s< and >%s<'%(son.domid,self.active)
-            return son.domid==self.active
+        def is_active (son,rank):
+            # if active_domid is not specified, make the first one active
+            if not self.active_domid: return rank==0
+            return son.domid==self.active_domid
         ranks=range(len(self.sons))
         env = { 'sons':
                  [ { 'rendered': son.render(request),
                      'rank': rank,
-                     'active': is_active(son),
+                     'is_active': is_active(son,rank),
                      'title': son.title,
                      'domid': son.domid,
                      'classname': son.classname,
index e56690b..178cbc6 100644 (file)
@@ -80,6 +80,9 @@ class Plugin:
         # do this only once the structure is fine
         self.page.record_plugin(self)
 
+    def __repr__ (self):
+        return "[%s]:%s"%(self.classname,self.domid)
+
     def _py_classname (self): 
         try:    return self.__class__.__name__
         except: return 'Plugin'
index ceb56f2..75a57d6 100644 (file)
@@ -11,7 +11,7 @@
       <div class="nav-collapse topmenu">
        <ul class="nav nav-pills" id='menu_ul'>
          {% for d in topmenu_items %}
-         {% if d.active %}
+         {% if d.is_active %}
          <li class='active'> <a href="{{ d.href }}"> {{ d.label }} </a> </li>
          {% else %}
          <li class='other'> <a href="{{ d.href }}"> {{ d.label }} </a> </li>