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
7 from django.template.loader import render_to_string
13 def __init__ (self, visible=True, hidable=True, hidden_by_default=False, **settings):
14 # xxx should generate some random id
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()
27 try: return self.__class__.__name__
28 except: return 'Plugin'
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]
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
39 # returns the html code for that plugin
40 # in essence, wraps the results of self.render_content ()
41 def render (self, request):
43 classname = self.classname()
44 plugin_content = self.render_content (request)
46 # expose _settings in json format to js
47 settings_json = json.dumps(self._settings, separators=(',',':'))
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
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,
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()
78 print "%s.render_content: END --------------------"%self.classname()
81 def render_env (self, request): return {}
82 ######################################## abstract interface
84 # your plugin is expected to implement either
85 # (*) def render_content(self, request) -> html fragment
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
92 def title (self): return "you should redefine title()"
94 # tell the framework about requirements in the document header
95 def media_js (self): pass