From 0bfd116596c1dc91f739b53cf3f469c94e8994a7 Mon Sep 17 00:00:00 2001 From: Thierry Parmentelat Date: Wed, 19 Dec 2012 18:15:07 +0100 Subject: [PATCH] yet another round, with 2 new Plugins Tabs and Raw (from raw html) --- engine/plugin.py | 2 +- engine/prelude.py | 10 +++--- engine/static/js/with-datatables.js | 2 +- engine/views.py | 54 ++++++++++------------------- myslice/viewutils.py | 20 +++++++++++ plugins/raw.py | 8 +++++ plugins/tabs.py | 25 +++++++++++++ plugins/templates/tabs.html | 13 +++++++ slice/views.py | 18 ++++++---- templates/layout-myslice.html | 2 -- templates/view-tab.html | 2 +- 11 files changed, 104 insertions(+), 52 deletions(-) create mode 100644 plugins/raw.py create mode 100644 plugins/tabs.py create mode 100644 plugins/templates/tabs.html diff --git a/engine/plugin.py b/engine/plugin.py index 3b52c804..1ce51028 100644 --- a/engine/plugin.py +++ b/engine/plugin.py @@ -44,7 +44,7 @@ class Plugin: except: return 'Plugin' # shorthands to inspect _settings - def get_setting (self, setting, default): + def get_setting (self, setting, default=None): if setting not in self._settings: return default else: return self._settings[setting] diff --git a/engine/prelude.py b/engine/prelude.py index bf5e0a91..b9835584 100644 --- a/engine/prelude.py +++ b/engine/prelude.py @@ -7,12 +7,12 @@ class Prelude: """A class for collecting dependencies on js/css files or fragments""" keys=[ 'js_files','css_files','js_chunks', 'css_chunks' ] - def __init__ (self): + def __init__ (self, js_files=[], css_files=[], js_chunks=[], css_chunks=[]): # it's tempting to use sets but sets are not ordered.. - self.js_files =[] - self.css_files =[] - self.js_chunks =[] - self.css_chunks=[] + self.js_files = Prelude._normalize(js_files) + self.css_files = Prelude._normalize(css_files) + self.js_chunks = Prelude._normalize(js_chunks) + self.css_chunks= Prelude._normalize(css_chunks) @staticmethod def _normalize (input): diff --git a/engine/static/js/with-datatables.js b/engine/static/js/with-datatables.js index 6acc7648..75e4cc2b 100644 --- a/engine/static/js/with-datatables.js +++ b/engine/static/js/with-datatables.js @@ -1,2 +1,2 @@ /* elements with class 'with-datatables' are set to use datatables */ -$(document).ready(function() {$('.with-datatables').each(function() { window.console.log('here'); $(this).dataTable() } ) } ) +$(document).ready(function() {$('.with-datatables').each(function() { $(this).dataTable() } ) } ) diff --git a/engine/views.py b/engine/views.py index b67197d0..7160944f 100644 --- a/engine/views.py +++ b/engine/views.py @@ -6,59 +6,41 @@ from django.template.loader import render_to_string from django.shortcuts import render_to_response from plugins.verticallayout import VerticalLayout +from plugins.tabs import Tabs from plugins.simplelist import SimpleList from plugins.slicelist import SliceList +from plugins.raw import Raw -from myslice.viewutils import topmenu_items, the_user, hard_wired_slice_names +from myslice.viewutils import topmenu_items, the_user +from myslice.viewutils import hard_wired_slice_names, hard_wired_list, lorem_p, lorem def test_plugin_view (request): # variables that will get passed to this template template_env = {} - # having html tags right here is not a real use case - hard_wired_list=[] - hard_wired_list.append("this hard-wired list") - hard_wired_list.append("is defined") - hard_wired_list.append("in plugins.simplelist.py") - hard_wired_list.append("which in turn relies on") - hard_wired_list.append("template widget-template.html") - hard_wired_list.append("while it should of course") - hard_wired_list.append("instead issue a query") - hard_wired_list.append("and fill the DOM in js from there") - hard_wired_list.append("it would however maybe make sense") - hard_wired_list.append("to offer the option to 'datatablify'") - hard_wired_list.append("the list from the python code") - hard_wired_list.append("just like a standard plugin can be set as visible or not") - hard_wired_list.append("") - hard_wired_list.append("OTOH and IMHO, there should be two separate and explicit subclasses of SimpleList for slices or testbeds") - - plugin_main1 = SimpleList (list=hard_wired_list, - header='Hard wired', - foo='the value for foo', - with_datatables=True) - plugin_main2 = SimpleList (hidable=True, - list=hard_wired_slice_names, - headers='Slices in main content') - layout = VerticalLayout (hidable=True, visible=True, - sons=[plugin_main1, plugin_main2] - ) -# layout.inspect_request (request,"before first render") - content_main = layout.render (request) -# layout.inspect_request (request,"after first render") - # this will be rendered as the main content - as per view-plugin.html and thus layout-myslice.html - template_env [ 'content_main' ] = content_main + main_plugin = \ + VerticalLayout ( sons = [ SimpleList (list=hard_wired_list, + header='Hard wired', + foo='the value for foo', + with_datatables=True), + Tabs (sons = [ Raw (html= 3*lorem_p), + SliceList(list=hard_wired_slice_names), + Raw (html=lorem) ]), + SimpleList (list=hard_wired_slice_names, + headers='Slices in main content') ] ) + # define 'content_main' to the template engine + template_env [ 'content_main' ] = main_plugin.render(request) ########## # lacks a/href to /slice/%s - plugin_related = SliceList (visible=True, + related_plugin = SliceList (visible=True, hidable=True, with_datatables='yes', list=hard_wired_slice_names, header='Slices' ) - content_related = plugin_related.render (request) # likewise but on the side view - template_env [ 'content_related' ] = content_related + template_env [ 'content_related' ] = related_plugin.render (request) # more general variables expected in the template template_env [ 'title' ] = 'Test Plugin View' diff --git a/myslice/viewutils.py b/myslice/viewutils.py index 212d3102..374417e1 100644 --- a/myslice/viewutils.py +++ b/myslice/viewutils.py @@ -33,7 +33,27 @@ lorem=""" Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat. Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. Nam liber tempor cum soluta nobis eleifend option congue nihil imperdiet doming id quod mazim placerat facer possim assum. Typi non habent claritatem insitam; est usus legentis in iis qui facit eorum claritatem. Investigationes demonstraverunt lectores legere me lius quod ii legunt saepius. Claritas est etiam processus dynamicus, qui sequitur mutationem consuetudium lectorum. Mirum est notare quam littera gothica, quam nunc putamus parum claram, anteposuerit litterarum formas humanitatis per seacula quarta decima et quinta decima. Eodem modo typi, qui nunc nobis videntur parum clari, fiant sollemnes in futurum. """ +lorem_p = "

"+lorem+"

" + hard_wired_slice_names = [] for site in [ 'inria', 'upmc' , 'ibbt' ]: for slice in [ 'foo', 'bar', 'tutu', 'test', 'omf', 'heartbeat' ]: hard_wired_slice_names.append ("ple.%s.%s"%(site,slice)) + +# having html tags right here is not a real use case +hard_wired_list=[] +hard_wired_list.append("this hard-wired list") +hard_wired_list.append("is defined") +hard_wired_list.append("in plugins.simplelist.py") +hard_wired_list.append("which in turn relies on") +hard_wired_list.append("template widget-template.html") +hard_wired_list.append("while it should of course") +hard_wired_list.append("instead issue a query") +hard_wired_list.append("and fill the DOM in js from there") +hard_wired_list.append("it would however maybe make sense") +hard_wired_list.append("to offer the option to 'datatablify'") +hard_wired_list.append("the list from the python code") +hard_wired_list.append("just like a standard plugin can be set as visible or not") +hard_wired_list.append("") +hard_wired_list.append("OTOH and IMHO, there should be two separate and explicit subclasses of SimpleList for slices or testbeds") + diff --git a/plugins/raw.py b/plugins/raw.py new file mode 100644 index 00000000..0a0be1d2 --- /dev/null +++ b/plugins/raw.py @@ -0,0 +1,8 @@ +from engine.plugin import Plugin + +# usage Raw (html="some html text") + +class Raw (Plugin): + + def render_content (self, request): + return self.get_setting('html') diff --git a/plugins/tabs.py b/plugins/tabs.py new file mode 100644 index 00000000..46e404bc --- /dev/null +++ b/plugins/tabs.py @@ -0,0 +1,25 @@ +from engine.composite import Composite + +class Tabs (Composite): + + def title (self): + return "Some tabs" + + def requirements (self): + return { 'js_files' : 'bootstrap/js/bootstrap.js', + 'css_files' : 'bootstrap/css/bootstrap.css' } + + def template (self): + return "tabs.html" + + def render_env (self, request): + env = {} + sons_rendered = [ son.render(request) for son in self.sons ] + sons_titles = [ son.title() for son in self.sons ] + ids = range (len(self.sons)) + # for now we don't have a title to pass + sons = [ { 'id':id, 'rendered':rendered, 'title':title } + for id,rendered,title in zip (ids, sons_rendered, sons_titles) ] + env['sons']=sons + return env + diff --git a/plugins/templates/tabs.html b/plugins/templates/tabs.html new file mode 100644 index 00000000..e8de9eb6 --- /dev/null +++ b/plugins/templates/tabs.html @@ -0,0 +1,13 @@ +{# could try to set class='active' on the one we want to highlight #} + +
+{% for son in sons %} +
+{{ son.rendered }} +
+{% endfor %} +
diff --git a/slice/views.py b/slice/views.py index 407e0df0..857499a0 100644 --- a/slice/views.py +++ b/slice/views.py @@ -4,6 +4,8 @@ from django.template import RequestContext from django.shortcuts import render_to_response from django.contrib.auth.decorators import login_required +from engine.prelude import Prelude + from myslice.viewutils import topmenu_items, the_user # tmp from myslice.viewutils import lorem, hard_wired_slice_names @@ -26,12 +28,16 @@ def fake_slice_view (request, name=None): @login_required def tab_view (request): - return render_to_response ('view-tab.html', - {'title':'Page for playing with Tabs', - 'topmenu_items': topmenu_items('tab',request), - 'username':the_user (request), - 'lorem': lorem, - }, + prelude=Prelude( js_files='bootstrap/js/bootstrap.js', css_files='bootstrap/css/bootstrap.css') + prelude_env = prelude.render_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)) @login_required diff --git a/templates/layout-myslice.html b/templates/layout-myslice.html index f8cdc10b..679e7465 100644 --- a/templates/layout-myslice.html +++ b/templates/layout-myslice.html @@ -19,8 +19,6 @@ {# let's add these ones no matter what #} {% insert_str prelude "jquery/js/jquery.js" %} -{% insert_str prelude "bootstrap/js/bootstrap.js" %} -{% insert_str prelude "bootstrap/css/bootstrap.css" %} {% insert_str prelude "js/plugin.js" %} {% insert_str prelude "css/myslice.css" %} diff --git a/templates/view-tab.html b/templates/view-tab.html index be358f3f..87a94e0c 100644 --- a/templates/view-tab.html +++ b/templates/view-tab.html @@ -8,7 +8,7 @@ +

{{ lorem|safe }}

-- 2.43.0