fix plugin class name
[myslice.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, **settings):
14         self.uuid=Plugin.uid
15         Plugin.uid += 1
16         # we store as a dictionary the arguments passed to constructor
17         # e.g. SimpleList (visible=True) => _settings = {'visible':True}
18         self._settings=settings
19
20     def get_class (self): 
21         try:    return self.__class__.__name__
22         except: return 'Plugin'
23
24     # shorthands to inspect _settings
25     def get_setting (self, setting, default):
26         if setting not in self._settings: return default
27         else:                             return self._settings[setting]
28
29     def is_visible (self): return self.get_setting ('visible',True)
30     def is_hidable (self): return self.get_setting ('hidable',False)
31     def is_hidden_by_default (self): return self.get_setting ('hidden_by_default', False)
32
33     # returns the html code for that plugin
34     # in essence, wraps the results of self.render_content ()
35     def render (self, request):
36         uuid = self.uuid
37         title = self.get_class()
38         plugin_content = self.render_content (request)
39
40         # expose _settings in json format to js
41         settings_json = json.dumps(self._settings, separators=(',',':'))
42
43         # xxx missing from the php version
44         # compute an 'optionstr' from the set of available settings/options as a json string
45         # that gets passed to jquery somehow
46         # see the bottom of 
47         result = render_to_string ('widget-plugin.html',
48                                    {'uuid':uuid, 'title':title,
49                                     'visible':self.is_visible(),
50                                     'hidable':self.is_hidable(),
51                                     'hidden':self.is_hidden_by_default(),
52                                     'plugin_content' : plugin_content,
53                                     'settings' : settings_json,
54                                     })
55
56         return result
57         
58     ### abstract interface
59     # you may redefine this completely, but if you don't we'll just use method 
60     # template() to find out which template to use, and env() to find out which 
61     # dictionary to pass the templating system
62     def render_content (self, request):
63         """Should return an HTML fragment"""
64         template = self.template()
65         env=self.env()
66         return render_to_string (template, env)