Back to index

plone3  3.1.7
Renderer.py
Go to the documentation of this file.
00001 import sys
00002 from Globals import InitializeClass
00003 from AccessControl import ClassSecurityInfo
00004 from Acquisition import ImplicitAcquisitionWrapper
00005 
00006 _marker = []
00007 
00008 class ArchetypesRenderer:
00009     
00010     security = ClassSecurityInfo()
00011     # TODO: more security
00012     
00013     def render(self, field_name, mode, widget, instance=None,
00014                field=None, accessor=None, **kwargs):
00015         if field is None:
00016             field = instance.Schema()[field_name]
00017 
00018         if accessor is None:
00019             accessor = field.getAccessor(instance)
00020 
00021         context = self.setupContext(field_name, mode, widget,
00022                                     instance, field, accessor, **kwargs)
00023 
00024         result = widget(mode, instance, context)
00025 
00026         del context
00027         return result    
00028     
00029     def setupContext(self, field_name, mode, widget, instance, field, \
00030                      accessor, **kwargs):
00031 
00032         # look for the context in the stack
00033         frame = sys._getframe()
00034         context = _marker
00035         while context is _marker and frame is not None:
00036             context = frame.f_locals.get('econtext', _marker)
00037             frame = frame.f_back
00038         if context is _marker:
00039             raise RuntimeError, 'Context not found'
00040         
00041         # for editing of multiple AT-based content at once we might want to 
00042         # prefix the field-name.
00043         if 'fieldprefix' in kwargs:
00044             field_name = '%s%s' % (kwargs['fieldprefix'], field_name)
00045 
00046         widget = ImplicitAcquisitionWrapper(widget, instance)
00047         field = ImplicitAcquisitionWrapper(field, instance)
00048         context.setLocal('here', instance)
00049         context.setLocal('fieldName', field_name)
00050         context.setLocal('accessor', accessor)
00051         context.setLocal('widget', widget)
00052         context.setLocal('field', field)
00053         context.setLocal('mode', mode)
00054 
00055         if kwargs:
00056             for k,v in kwargs.items():
00057                 context.setLocal(k, v)
00058 
00059         del frame
00060         return context
00061 
00062 InitializeClass(ArchetypesRenderer)
00063 
00064 renderer = ArchetypesRenderer()