4d6e4aab9a398a8e1befbd4f919bb5a52d0bddc2
[unfold.git] / engine / plugin.py
1 # this is the abstract interface for Plugin instances
2 # so it should be specialized in real plugin classes
3 # like e.g. plugins.simplelist.SimpleList
4
5 import json
6
7 from django.template.loader import render_to_string
8
9 class Plugin:
10
11     uid=0
12
13     def __init__ (self, visible=True, hidable=True, hidden_by_default=False, **settings):
14         # xxx should generate some random id
15         self.uuid=Plugin.uid
16         Plugin.uid += 1
17         self.visible=visible
18         self.hidable=hidable
19         self.hidden_by_default=hidden_by_default
20         # we store as a dictionary the arguments passed to constructor
21         # e.g. SimpleList (list=[1,2,3]) => _settings = { 'list':[1,2,3] }
22         # our own settings are not made part of _settings but could be..
23         self._settings=settings
24 #        print "Created plugin with settings %s"%self._settings.keys()
25
26     def classname (self): 
27         try:    return self.__class__.__name__
28         except: return 'Plugin'
29
30     # shorthands to inspect _settings
31     def get_setting (self, setting, default):
32         if setting not in self._settings: return default
33         else:                             return self._settings[setting]
34
35     def is_visible (self): return self.visible
36     def is_hidable (self): return self.hidable
37     def is_hidden_by_default (self): return self.hidden_by_default
38
39     # returns the html code for that plugin
40     # in essence, wraps the results of self.render_content ()
41     def render (self, request):
42         uuid = self.uuid
43         classname = self.classname()
44         plugin_content = self.render_content (request)
45
46         # expose _settings in json format to js
47         settings_json = json.dumps(self._settings, separators=(',',':'))
48
49         # xxx missing from the php version
50         # compute an 'optionstr' from the set of available settings/options as a json string
51         # that gets passed to jquery somehow
52         # see the bottom of 
53         result = render_to_string ('widget-plugin.html',
54                                    {'uuid':uuid, 'classname':classname,
55                                     'visible':self.is_visible(),
56                                     'hidable':self.is_hidable(),
57                                     'hidden':self.is_hidden_by_default(),
58                                     'plugin_content' : plugin_content,
59                                     'settings_json' : settings_json,
60                                     })
61
62         return result
63         
64     # you may redefine this completely, but if you don't we'll just use method 
65     # template() to find out which template to use, and env() to find out which 
66     # dictionary to pass the templating system
67     def render_content (self, request):
68         """Should return an HTML fragment"""
69         template = self.template()
70         env=self.render_env(request)
71         if not isinstance (env,dict):
72             raise Exception, "%s.render_env returns wrong type"%self.classname()
73         env.update(self._settings)
74         result=render_to_string (template, env)
75         print "%s.render_content: BEG --------------------"%self.classname()
76         print "env=%s"%env.keys()
77         print result
78         print "%s.render_content: END --------------------"%self.classname()
79         return result
80
81     def render_env (self, request): return {}
82     ######################################## abstract interface
83
84     # your plugin is expected to implement either 
85     # (*) def render_content(self, request) -> html fragment
86     # -- or --
87     # (*) def template(self) -> filename
88     # (*) def render_env (self, request) -> dict
89     #   this is the variable->value association used to render the template
90     # in which case the html template will be used
91
92     def title (self): return "you should redefine title()"
93
94     # tell the framework about requirements in the document header
95     def media_js (self): pass