[views] do no depend on jqplot (closes #2266840)
authorDavid Douard <david.douard@logilab.fr>
Fri, 23 Mar 2012 10:52:03 +0100
changeset 823 dd72c0ee8388
parent 822 cb0e15cb988e
child 824 562c476dddbb
[views] do no depend on jqplot (closes #2266840)
views/plots.py
views/tracker.py
--- a/views/plots.py	Fri Mar 09 10:16:28 2012 +0100
+++ b/views/plots.py	Fri Mar 23 10:52:03 2012 +0100
@@ -8,8 +8,6 @@
 __docformat__ = "restructuredtext en"
 _ = unicode
 
-from cubes.jqplot.views import JQPlotSimpleView
-from cubicweb.selectors import multi_columns_rset
 
 ERROR_CODES = {
     'error': "#FF5555",
@@ -19,46 +17,59 @@
     'other': "#AAAAAA",
     }
 
-class JQPlotTestExecutionView(JQPlotSimpleView):
-    __regid__ = 'jqplot.testexecution'
-    __select__ = multi_columns_rset(3)
-    default_renderer = 'bar'
+from cubicweb.selectors import multi_columns_rset
 
-    onload = '''%(id)s = $.jqplot("%(id)s", %(data)s, %(options)s);
+try:
+    from cubes.jqplot.views import JQPlotSimpleView
+except ImportError:
+    pass
+else:
+    class JQPlotTestExecutionView(JQPlotSimpleView):
+        __regid__ = 'jqplot.testexecution'
+        __select__ = multi_columns_rset(3)
+        default_renderer = 'bar'
+
+        onload = '''%(id)s = $.jqplot("%(id)s", %(data)s, %(options)s);
 
-    $(document).ready(function(){
-        $("#%(id)s").bind('jqplotDataClick',
-            function (ev, seriesIndex, pointIndex, data) {
-            	/* To open in a NEW window use: */
-                if ( ev.which == 2 ) {
-            	   window.open(data[2]);
-                } else  {
-                   /* To open in the same window use: */
-              	   window.location.href=data[2];
-                };
+        $(document).ready(function(){
+            $("#%(id)s").bind('jqplotDataClick',
+                function (ev, seriesIndex, pointIndex, data) {
+                    /* To open in a NEW window use: */
+                    if ( ev.which == 2 ) {
+                       window.open(data[2]);
+                    } else  {
+                       /* To open in the same window use: */
+                       window.location.href=data[2];
+                    };
+                }
+            );
+        });
+        '''
+        default_options = {
+            'varyBarColor': True,
+            'barMargin':0,
             }
-        );
-    });
-    '''
-    default_options = {
-        'varyBarColor': True,
-        'barMargin':0,
-        }
-    default_legend = {
-        'show': False,
-        }
+        default_legend = {
+            'show': False,
+            }
+
+        def get_data(self):
+            return [[i+1,x[1],self._cw.build_url(x[0])] for i,x in enumerate(self.cw_rset.rows)]
 
-    def get_data(self):
-        return [[i+1,x[1],self._cw.build_url(x[0])] for i,x in enumerate(self.cw_rset.rows)]
+        def set_custom_options(self, options):
+            other = ERROR_CODES.get('other')
+            options['seriesColors'] = [ERROR_CODES.get(x[2], other) for x in self.cw_rset.rows]
+            #FIXME hack to approximate width since it doesn't work in js
+            options['series'][0]['rendererOptions']['barWidth'] = 350/len(self.cw_rset.rows)
 
-    def set_custom_options(self, options):
-        other = ERROR_CODES.get('other')
-        options['seriesColors'] = [ERROR_CODES.get(x[2], other) for x in self.cw_rset.rows]
-        #FIXME hack to approximate width since it doesn't work in js
-        options['series'][0]['rendererOptions']['barWidth'] = 350/len(self.cw_rset.rows)
+        def div_holder(self, divid, width, height):
+            super(JQPlotTestExecutionView, self).div_holder(divid, width, height)
+            self.w(u'''<table class="plotlegend"><tr>%s</tr></table>
+            ''' % ''.join(['<td>%s</td><td style="background:%s">&nbsp;&nbsp;&nbsp;</td>' % (x,y) for x,y in  ERROR_CODES.items()]))
+
 
-    def div_holder(self, divid, width, height):
-        super(JQPlotTestExecutionView, self).div_holder(divid, width, height)
-        self.w(u'''<table class="plotlegend"><tr>%s</tr></table>
-        ''' % ''.join(['<td>%s</td><td style="background:%s">&nbsp;&nbsp;&nbsp;</td>' % (x,y) for x,y in  ERROR_CODES.items()]))
+def registration_callback(vreg):
+    if not 'jqplot' in vreg.config.cubes():
+        return # don't register anything from this module
+    vreg.register_all(globals().values(), __name__)
 
--- a/views/tracker.py	Fri Mar 09 10:16:28 2012 +0100
+++ b/views/tracker.py	Fri Mar 23 10:52:03 2012 +0100
@@ -57,7 +57,6 @@
                   'X value V, X for_check CR, CR during_execution TE, TE using_environment TENV, '\
                   'P has_apycot_environment TENV, P eid %(p)s, TE starttime D, TE branch "%(branch)s" '
             var_dict['cri_label'] = cri_label
-            print rql % var_dict
             rset = form._cw.execute(rql % var_dict)
             if rset and len(rset) > MIN_NB_RUNS_IN_GRAPH:
                 js_call = form._cw.ajax_replace_url('graph-container',
@@ -135,6 +134,9 @@
     except ImportError:
         pass
     else:
-        vreg.register_all(globals().values(), __name__)
+        has_jqplot = 'jqplot' in vreg.config.cubes()
+        no_jqplot = [GraphRefreshForm, ProjectGraphTestResults]
+        to_register = [value for value in globals().values() if has_jqplot or value not in no_jqplot]
+        vreg.register_all(to_register, __name__)
         if 'apycottestresults_tab' not in ProjectPrimaryView.tabs:
             ProjectPrimaryView.tabs.append('apycottestresults_tab')