new engine/ app, the location to where to implement plugin/query and
authorThierry Parmentelat <thierry.parmentelat@inria.fr>
Mon, 10 Dec 2012 14:09:32 +0000 (15:09 +0100)
committerThierry Parmentelat <thierry.parmentelat@inria.fr>
Mon, 10 Dec 2012 14:09:32 +0000 (15:09 +0100)
the like
first stab at a small plugin class that can render
toggable (hide&show) plugins

13 files changed:
engine/__init__.py [new file with mode: 0644]
engine/models.py [new file with mode: 0644]
engine/plugin.py [new file with mode: 0644]
engine/query.py [new file with mode: 0644]
engine/tests.py [new file with mode: 0644]
engine/views.py [new file with mode: 0644]
myslice/settings.py
myslice/urls.py
plugins/__init__.py [new file with mode: 0644]
plugins/simplelist.py [new file with mode: 0644]
static/js/plugin.js [new file with mode: 0644]
templates/myslice-layout.html
templates/render_plugin.html [new file with mode: 0644]

diff --git a/engine/__init__.py b/engine/__init__.py
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/engine/models.py b/engine/models.py
new file mode 100644 (file)
index 0000000..71a8362
--- /dev/null
@@ -0,0 +1,3 @@
+from django.db import models
+
+# Create your models here.
diff --git a/engine/plugin.py b/engine/plugin.py
new file mode 100644 (file)
index 0000000..f3c659f
--- /dev/null
@@ -0,0 +1,50 @@
+# this is the abstract interface for Plugin instances
+# so it should be specialized in real plugin classes
+# like e.g. plugins.simplelist.SimpleList
+
+from django.template.loader import render_to_string
+
+class Plugin:
+
+    uid=0
+
+    def __init__ (self, **settings):
+        self.uuid=Plugin.uid
+        Plugin.uid += 1
+        # we store as a dictionary the arguments passed to constructor
+        # e.g. SimpleList (visible=True) => _settings = {'visible':True}
+        self._settings=settings
+
+    def get_class (self): return type(self).__name__
+
+    # shorthands to inspect _settings
+    def get_setting (self, setting, default):
+        if setting not in self._settings: return default
+        else:                             return self._settings[setting]
+
+    def is_visible (self): return self.get_setting ('visible',True)
+    def is_hidable (self): return self.get_setting ('hidable',False)
+    def is_hidden_by_default (self): return self.get_setting ('hidden_by_default', False)
+
+    # returns the html code for that plugin
+    # in essence, wraps the results of self.render_content ()
+    def render (self):
+        uuid = self.uuid
+        title = self.get_class()
+        plugin_content = self.render_content ()
+
+        # xxx missing from the php version
+        # compute an 'optionstr' from the set of available settings/options as a json string
+        # that gets passed to jquery somehow
+        # see the bottom of 
+        result = render_to_string ('render_plugin.html',
+                                   {'uuid':uuid, 'title':title,
+                                    'visible':self.is_visible(),
+                                    'hidable':self.is_hidable(),
+                                    'hidden':self.is_hidden_by_default(),
+                                    'plugin_content' : plugin_content,
+                                    'optionstr' : 'xxx-todo',
+                                    })
+
+        return result
+        
diff --git a/engine/query.py b/engine/query.py
new file mode 100644 (file)
index 0000000..62b4783
--- /dev/null
@@ -0,0 +1,23 @@
+# needed imports
+# uniqid
+
+class Query:
+
+    def __init__ (self):
+        self.action=None
+        self.method=None
+        self.ts=None
+        self.filters=[]
+        self.params=[]
+        self.fields=[]
+        self.unique=False
+        self.uuid=uniquid()
+        self.sort=None
+        self.limit=None
+        self.offset=None
+        self.analyzed_query=None
+        self.subqueries = []
+
+    def to_json (self):
+        return "Query.json: todo"
+
diff --git a/engine/tests.py b/engine/tests.py
new file mode 100644 (file)
index 0000000..adc92ed
--- /dev/null
@@ -0,0 +1,19 @@
+"""
+This file demonstrates writing tests using the unittest module. These will pass
+when you run "manage.py test".
+
+Replace this with more appropriate tests for your application.
+"""
+
+from django.test import TestCase
+
+from plugins.simplelist import SimpleList
+
+class PluginTest(TestCase):
+    def test_basic(self):
+        """
+        Tests that 1 + 1 always equals 2.
+        """
+        sl = SimpleList (visible=True)
+        print 'rendering', sl.render()
+        self.assertEqual(1 + 1, 2)
diff --git a/engine/views.py b/engine/views.py
new file mode 100644 (file)
index 0000000..f7996a2
--- /dev/null
@@ -0,0 +1,22 @@
+# Create your views here.
+
+from django.core.context_processors import csrf
+from django.template import RequestContext
+from django.template.loader import render_to_string
+from django.shortcuts import render_to_response
+
+from plugins.simplelist import SimpleList
+
+def test_plugin_view (request):
+    
+    test_plugin = SimpleList (visible=True, hidable=True)
+    plugin_content = test_plugin.render ()
+
+    print '--------------------'
+    print plugin_content
+    print '--------------------'
+
+    return render_to_response ('test-plugin.html',
+                               {'content_main' : plugin_content},
+                               context_instance=RequestContext(request))
+                               
index 7785f04..d88a335 100644 (file)
@@ -134,6 +134,8 @@ INSTALLED_APPS = (
     'insert_above',
     'myslice',
     'auth',
+    'engine',
+    'plugins',
     'slice',
     # Uncomment the next line to enable the admin:
     # 'django.contrib.admin',
index c6fa979..ab67f55 100644 (file)
@@ -25,4 +25,5 @@ urlpatterns = patterns(
     (r'^slice/(?P<name>[\w\.]+)/?$', 'slice.views.fake_slice_view'),
     (r'^tab/?$', 'slice.views.tab_view'),
     (r'^scroll/?$', 'slice.views.scroll_view'),
+    (r'^plugin/?$', 'engine.views.test_plugin_view'),
 )
diff --git a/plugins/__init__.py b/plugins/__init__.py
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/plugins/simplelist.py b/plugins/simplelist.py
new file mode 100644 (file)
index 0000000..5a97fcb
--- /dev/null
@@ -0,0 +1,9 @@
+from engine.plugin import Plugin
+
+class SimpleList (Plugin) :
+    
+    def render_content (self):
+        return """<ul><li>hard-wired one</li><li>manual two</li>
+<li>this is defined</li>
+<li>in plugins.simplelist.py</li>
+</ul>"""
diff --git a/static/js/plugin.js b/static/js/plugin.js
new file mode 100644 (file)
index 0000000..cb04cfc
--- /dev/null
@@ -0,0 +1,26 @@
+/* upon document completion, we locate all the hide and show areas, 
+ * and configure their behaviour 
+ */
+/* xxx missing - see plugin.php 
+ * if (typeof jQuery('#$uuid').$title != 'undefined') {
+ * jQuery('#$uuid').$title('show');
+ * }; 
+ */
+
+$(document).ready(
+    function() {
+       $('.plugin-hide').each(function(p) {
+           plugin='#'+$(this).attr('id').replace('hide-','plugin-');
+           show='#'+$(this).attr('id').replace('hide-','show-');
+           type=$(this).attr('plugin-type');
+           $(this).click(function () { jQuery(plugin).hide(); jQuery(show).show(); $(this).hide();})
+       })
+       $('.plugin-show').each(function(p) {
+           plugin='#'+$(this).attr('id').replace('show-','plugin-');
+           hide='#'+$(this).attr('id').replace('show-','hide-');
+           type=$(this).attr('plugin-type');
+           $(this).click(function () { jQuery(plugin).show(); jQuery(hide).show(); $(this).hide();})
+       })
+           })
+
+
index 7e69a51..4fc6e85 100644 (file)
@@ -9,6 +9,7 @@
 {% 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" %}
 <script type="text/javascript">
 {# In case we need to add raw js code - use {% insert prelude_js %} ... {% endinsert %} #}
 {% container prelude_js %}
@@ -22,7 +23,7 @@
 {# let's add this one no matter what #}
 {% insert_str prelude "css/myslice.css" %}
 
-<body data-spy="scroll" data-target=".foo">
+<body>
 
 {% block container %}
 <div id='container'>
diff --git a/templates/render_plugin.html b/templates/render_plugin.html
new file mode 100644 (file)
index 0000000..64c117e
--- /dev/null
@@ -0,0 +1,49 @@
+{% if visible %}
+<div class='plugin-manage'>
+{% if hidable %}
+  {% if hidden %}
+<p id='show-{{ uuid }}' class='plugin-show'><a href='#'><span>&raquo; Show {{ title }}</span></a></p>
+<p id='hide-{{ uuid }}' class='plugin-hide' style='display:none;'><a href='#'><span>&laquo; Hide {{ title }}</span></a></p>
+  {% else %}
+<p id='show-{{ uuid }}' class='plugin-show' style='display:none;'><a href='#'><span>&raquo; Show {{ title }}</span></a></p>
+<p id='hide-{{ uuid }}' class='plugin-hide'><a href='#'><span>&laquo; Hide {{ title }}</span></a></p>
+  {% endif %}
+{% endif %}
+{% endif %}
+
+<div class='plugin {{ title }}' id='plugin-{{ uuid }}' plugin-type='{{ title }}'>
+{{ plugin_content|safe }}
+</div><!--plugin {{ title }}-->
+
+{% if visible %}
+</div>
+{% endif %}
+
+{% if visible and hidable %}
+{# xxx sounds like this could be optimized by a single call performed on document.ready #}
+{# that would do that on all DOM elements that require it #}
+
+{% insert prelude_js %}
+/* Show / hide plugin */
+
+jQuery('#show_{{ uuid }}').click(function() {
+jQuery('#{{ uuid }}').show();
+if (typeof jQuery('#{{ uuid }}').{{ title }} != 'undefined') {
+jQuery('#{{ uuid }}').{{ title }}('show');
+}; 
+jQuery('#hide_{{ uuid }}').show();
+jQuery('#show_{{ uuid }}').hide(); 
+event.preventDefault();
+}); 
+jQuery('#hide_{{ uuid }}').click(function() {
+jQuery('#{{ uuid }}').hide();
+jQuery('#hide_{{ uuid }}').hide();
+jQuery('#show_{{ uuid }}').show(); 
+event.preventDefault();
+});
+{% if hidden %}
+jQuery('#{{ uuid }}').hide()
+{% endif %}
+/* {{ optionstr }} optionstr needs more work */
+{% endinsert %}
+{% endif %}