cleanup rendered html with new field names
authorThierry Parmentelat <thierry.parmentelat@inria.fr>
Thu, 20 Dec 2012 15:07:58 +0000 (16:07 +0100)
committerThierry Parmentelat <thierry.parmentelat@inria.fr>
Thu, 20 Dec 2012 15:07:58 +0000 (16:07 +0100)
engine/composite.py
engine/plugin.py
engine/templates/plugin.html
engine/views.py
plugins/simplelist.py
plugins/templates/verticallayout.html

index fd72db5..69953f7 100644 (file)
@@ -12,11 +12,14 @@ class Composite (Plugin):
     def template_env (self, request):
         # this is designed so as to support a template like
         # {% for son in sons %} {{ son.rendered }} ...
+        ranks=range(len(self.sons))
         return { 'sons': 
                  [ { 'rendered': son.render(request),
                      'title': son.title,
-                     'uuid': son.uuid}
-                   for son in self.sons ]}
+                     'uuid': son.uuid,
+                     'classname': son.classname,
+                     'rank': rank}
+                   for (son,rank) in zip(self.sons,ranks) ]}
 
     # xxx need a way to select an active son, like e.g.
     # Composite (active='some string')
index 2645810..b69d098 100644 (file)
@@ -8,10 +8,11 @@ from django.template.loader import render_to_string
 
 from engine.prelude import Prelude
 
-# set to
+#################### 
+# set DEBUG to
 # . False : silent
-# [ 'SliceList', 'TabbedView' ] : to debug these classes
-# True : to debug all slices
+# [ 'SliceList', 'TabbedView' ] : to debug these classes
+# . True : to debug all plugin
 
 DEBUG= [ 'SliceList' ]
 
@@ -53,10 +54,12 @@ class Plugin:
         self.name=name
         self.add_to_settings ( ['title','name'] )
         self.uuid=Plugin.newuuid()
+        self.classname=self._classname()
+        self.add_to_settings ( [ 'uuid', 'classname' ] )
         self.visible=visible
         self.hidable=hidable
         self.hidden_by_default=hidden_by_default
-        self.add_to_settings( ['uuid','visible','hidable','hidden_by_default'] )
+        self.add_to_settings( ['visible','hidable','hidden_by_default'] )
         # we store as a dictionary the arguments passed to constructor
         # e.g. SimpleList (list=[1,2,3]) => _settings = { 'list':[1,2,3] }
         # our own settings are not made part of _settings but could be..
@@ -73,7 +76,7 @@ class Plugin:
             for setting_name in setting_name_s:
                 self._settings[setting_name]=getattr(self,setting_name)
 
-    def classname (self): 
+    def _classname (self): 
         try:    return self.__class__.__name__
         except: return 'Plugin'
 
@@ -90,13 +93,12 @@ class Plugin:
     def need_debug (self):
         if not DEBUG:           return False
         if DEBUG is True:       return True
-        else:                   return self.classname() in DEBUG
+        else:                   return self.classname in DEBUG
 
     # returns the html code for that plugin
     # in essence, wraps the results of self.render_content ()
     def render (self, request):
         uuid = self.uuid
-        classname = self.classname()
         # initialize prelude placeholder 
         self._init_request (request)
         
@@ -105,15 +107,11 @@ class Plugin:
         # expose _settings in json format to js
         settings_json = json.dumps (self._settings, separators=(',',':'))
 
-        result = render_to_string ('plugin.html',
-                                   {'uuid':uuid, 
-                                    'classname':classname,
-                                    'visible':self.is_visible(),
-                                    'hidable':self.is_hidable(),
-                                    'hidden':self.is_hidden_by_default(),
-                                    'plugin_content' : plugin_content,
-                                    'settings_json' : settings_json,
-                                    })
+        env= {'plugin_content' : plugin_content,
+              'settings_json' : settings_json,
+              }
+        env.update(self._settings)
+        result = render_to_string ('plugin.html',env)
 
         # handle requirements() if defined on this class
         try: 
@@ -136,17 +134,18 @@ class Plugin:
         template = self.template_file()
         env=self.template_env(request)
         if not isinstance (env,dict):
-            raise Exception, "%s.template_env returns wrong type"%self.classname()
+            raise Exception, "%s.template_env returns wrong type"%self.classname
         # expose this class's settings to the template
         # xxx we might need to check that this does not overwrite env..
         env.update(self._settings)
         result=render_to_string (template, env)
         if self.need_debug():
-            print "%s.render_content: BEG --------------------"%self.classname()
+            print "%s.render_content: BEG --------------------"%self.classname
             print "template=%s"%template
-            print "env=%s"%env.keys()
-            # print result
-            print "%s.render_content: END --------------------"%self.classname()
+            print "env.keys=%s"%env.keys()
+            print "env=%s"%env
+            print result
+            print "%s.render_content: END --------------------"%self.classname
         return result
 
     #################### requirements/prelude management
@@ -178,7 +177,7 @@ class Plugin:
     def handle_requirements (self, request, d):
         for (k,v) in d.iteritems():
             if self.need_debug():
-                print "%s: handling requirement %s"%(self.classname(),v)
+                print "%s: handling requirement %s"%(self.classname,v)
             method_name='add_'+k
             method=Plugin.__dict__[method_name]
             method(self,request,v)
@@ -187,15 +186,15 @@ class Plugin:
     # your plugin is expected to implement either 
     # (*) def render_content(self, request) -> html fragment
     # -- or --
-    # (*) def template_file(self) -> filename
+    # (*) def template_file (self) -> filename
     #   relative to STATIC 
     # (*) def template_env (self, request) -> dict
     #   this is the variable->value association used to render the template
     # in which case the html template will be used
 
     # if you see this string somewhere your template_file() code is not kicking in
-    def template_file (self):                return "undefined_template"
-    def template_env (self, request):     return {}
+    def template_file (self):           return "undefined_template"
+    def template_env (self, request):   return {}
 
 #    # tell the framework about requirements (for the document <header>)
 #    # the notion of 'Media' in django provides for medium-dependant
index 2039801..650551a 100644 (file)
@@ -1,12 +1,13 @@
+{#<!--begin {{ classname }}{{ name }}{{ uuid }}-->#}
 {% if visible %}
-<div class='plugin-manage'>
+<div class='plugin-manage' id='{{ name }}'>
 {% if hidable %}
   {% if hidden %}
-<p id='show-{{ uuid }}' class='plugin-show'><a href='#'><span>&raquo; Show {{ classname }}</span></a></p>
-<p id='hide-{{ uuid }}' class='plugin-hide' style='display:none;'><a href='#'><span>&laquo; Hide {{ classname }}</span></a></p>
+<p id='show-{{ uuid }}' class='plugin-show'><a href='#'><span>&raquo; Show {{ classname }} "{{ title }}"</span></a></p>
+<p id='hide-{{ uuid }}' class='plugin-hide' style='display:none;'><a href='#'><span>&laquo; Hide {{ classname }} "{{ title }}"</span></a></p>
   {% else %}
-<p id='show-{{ uuid }}' class='plugin-show' style='display:none;'><a href='#'><span>&raquo; Show {{ classname }}</span></a></p>
-<p id='hide-{{ uuid }}' class='plugin-hide'><a href='#'><span>&laquo; Hide {{ classname }}</span></a></p>
+<p id='show-{{ uuid }}' class='plugin-show' style='display:none;'><a href='#'><span>&raquo; Show {{ classname }} "{{ title }}"</span></a></p>
+<p id='hide-{{ uuid }}' class='plugin-hide'><a href='#'><span>&laquo; Hide {{ classname }} "{{ title }}"</span></a></p>
   {% endif %}
 {% endif %}
 {% endif %}
 {% if visible %}
 </div>
 {% endif %}
+{#<!--end {{ classname }}{{ name }}{{ uuid }}-->#}
 
 {# Plugin initialization (if the plugin has the right structure) - from plugin.php #}
 {# note: that's the spirit but using {% insert %}  is not reliable enough #}
 {# so for now this doesn't make it back to the browser #}
 {% insert prelude_js %}
 if (typeof jQuery('#{{ uuid }}').$title != 'undefined') {
-  jQuery('#{{ uuid }}').$title({ {{ settings_json }} plugin_uuid: '{{ uuid }}' });
-  //jQuery('#{{ uuid }}').$title('show');
+  jQuery('#plugin-{{ uuid }}').$title({ {{ settings_json }} plugin_uuid: '{{ uuid }}' });
+  //jQuery('#plugin-{{ uuid }}').$title('show');
   }; 
 {% endinsert %}
index f2c157f..59a2e46 100644 (file)
@@ -57,7 +57,6 @@ def test_plugin_view (request):
     # request.plugin_prelude holds a summary of the requirements() for all plugins
     # define {js,css}_{files,chunks}
     prelude_env = request.plugin_prelude.template_env()
-    print 'prelude_env',prelude_env
     template_env.update(prelude_env)
 
     return render_to_response ('view-plugin.html',template_env,
index f119d76..d49118d 100644 (file)
@@ -22,7 +22,6 @@ class SimpleList (Plugin) :
         if self.with_datatables:
             reqs['js_files'].append ("datatables/js/dataTables.js")
             reqs['js_files'].append ("js/with-datatables.js")
-        print self.classname(),reqs
         return reqs
 # for tests
 #                 'js_chunks' : "/* a javascript chunk */",       
index 4b77c94..052dc49 100644 (file)
@@ -1,7 +1,7 @@
 <div id='{{ uuid }}'>
 {% for son in sons %}
 <h3>{{ son.title }}</h3>
-<div id='{{ uuid }}-{{ son.id }}'>
+<div id='{{ name }}-{{ son.rank }}'>
 {{ son.rendered }}
 </div>
 {% endfor %}