login/logout stuff working
[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 from django.template.loader import render_to_string
6
7 class Plugin:
8
9     uid=0
10
11     def __init__ (self, **settings):
12         self.uuid=Plugin.uid
13         Plugin.uid += 1
14         # we store as a dictionary the arguments passed to constructor
15         # e.g. SimpleList (visible=True) => _settings = {'visible':True}
16         self._settings=settings
17
18     def get_class (self): return type(self).__name__
19
20     # shorthands to inspect _settings
21     def get_setting (self, setting, default):
22         if setting not in self._settings: return default
23         else:                             return self._settings[setting]
24
25     def is_visible (self): return self.get_setting ('visible',True)
26     def is_hidable (self): return self.get_setting ('hidable',False)
27     def is_hidden_by_default (self): return self.get_setting ('hidden_by_default', False)
28
29     # returns the html code for that plugin
30     # in essence, wraps the results of self.render_content ()
31     def render (self, request):
32         uuid = self.uuid
33         title = self.get_class()
34         plugin_content = self.render_content (request)
35
36         # xxx missing from the php version
37         # compute an 'optionstr' from the set of available settings/options as a json string
38         # that gets passed to jquery somehow
39         # see the bottom of 
40         result = render_to_string ('widget-plugin.html',
41                                    {'uuid':uuid, 'title':title,
42                                     'visible':self.is_visible(),
43                                     'hidable':self.is_hidable(),
44                                     'hidden':self.is_hidden_by_default(),
45                                     'plugin_content' : plugin_content,
46                                     'optionstr' : 'xxx-todo',
47                                     })
48
49         return result
50         
51     ### abstract interface
52     def render_content (self, request):
53         """Should return an HTML fragment"""
54         return "Your plugin needs to redefine 'render_content(self, request)'"