+ template = self.template_file()
+ env=self.template_env(request)
+ if not isinstance (env,dict):
+ raise Exception, "%s.template_env returns wrong type"%self.classname
+ # expose this class's settings to the template
+ # xxx we might need to check that this does not overwrite env..
+ env.update(self._settings)
+ result=render_to_string (template, env)
+ if self.need_debug():
+ print "%s.render_content: BEG --------------------"%self.classname
+ print "template=%s"%template
+ print "env.keys=%s"%env.keys()
+ #print "env=%s"%env
+ #print result
+ print "%s.render_content: END --------------------"%self.classname
+ return result
+
+ #################### requirements/prelude management
+ def _init_prelude (self, request):
+ if not hasattr (request, 'plugin_prelude'):
+ # include css/plugins.css
+ request.plugin_prelude=Prelude(css_files='css/plugin.css')
+
+ def inspect_request (self, request, message):
+ has=hasattr(request,'plugin_prelude')
+ get=getattr(request,'plugin_prelude','none-defined')
+ print "INSPECT (%s), hasattr %s, getattr %s"%(message,has,get)
+
+ # can be used directly in render_content()
+ def add_js_files (self, request, files):
+ self._init_prelude (request)
+ request.plugin_prelude.add_js_files (files)
+ def add_css_files (self, request, files):
+ self._init_prelude (request)
+ request.plugin_prelude.add_css_files (files)
+ def add_js_chunks (self, request, chunks):
+ self._init_prelude (request)
+ request.plugin_prelude.add_js_chunks (chunks)
+ def add_css_chunks (self, request, chunks):
+ self._init_prelude (request)
+ request.plugin_prelude.add_css_chunks (chunks)
+
+ # or from the result of self.requirements()
+ def handle_requirements (self, request):
+ try:
+ d=self.requirements()
+ for (k,v) in d.iteritems():
+ if self.need_debug():
+ print "%s: handling requirement %s"%(self.classname,v)
+ method_name='add_'+k
+ method=Plugin.__dict__[method_name]
+ method(self,request,v)
+ except AttributeError:
+ # most likely the object does not have that method defined, which is fine
+ pass
+ except:
+ import traceback
+ traceback.print_exc()
+ pass
+
+ ######################################## abstract interface
+ # your plugin is expected to implement either
+ # (*) def render_content(self, request) -> html fragment
+ # -- or --
+ # (*) def template_file (self) -> filename
+ # relative to STATIC
+ # (*) def template_env (self, request) -> dict
+ # this is the variable->value association used to render the template
+ # in which case the html template will be used
+
+ # if you see this string somewhere your template_file() code is not kicking in
+ def template_file (self): return "undefined_template"
+ def template_env (self, request): return {}
+
+# # tell the framework about requirements (for the document <header>)
+# # the notion of 'Media' in django provides for medium-dependant
+# # selection of css files
+# # as a first attempt however we keep a flat model for now
+# # can use one string instead of a list or tuple if needed,
+# # see requirements.py for details
+# def requirements (self):
+# return { 'js_files' : [], # a list of relative paths for js input files
+# 'css_files': [], # ditto for css, could have been a dict keyed on
+# # media instead
+# 'js_chunk' : [], # (lines of) verbatim javascript code
+# 'css_chunk': [], # likewise for css scripts
+# }
+
+ # for better performance
+ # you can specify a list of keys that won't be exposed as json attributes
+ def exclude_from_json (self): return []