Back to index

plone3  3.1.7
TemplateMixin.py
Go to the documentation of this file.
00001 from Products.Archetypes import PloneMessageFactory as _
00002 from Products.Archetypes.Schema import Schema
00003 from Products.Archetypes.Field import StringField
00004 from Products.Archetypes.Widget import SelectionWidget
00005 from Products.Archetypes.config import TOOL_NAME
00006 from Products.Archetypes.interfaces.ITemplateMixin import ITemplateMixin
00007 
00008 from Products.CMFCore import permissions, utils
00009 from AccessControl import ClassSecurityInfo
00010 from Globals import InitializeClass
00011 from Acquisition import aq_base
00012 from Acquisition import aq_inner
00013 from ExtensionClass import Base
00014 
00015 TemplateMixinSchema = Schema((
00016     # TemplateMixin
00017     StringField('layout',
00018         write_permission=permissions.ModifyPortalContent,
00019         default_method="getDefaultLayout",
00020         vocabulary="_voc_templates",
00021         widget=SelectionWidget(label = _(u'label_template_mixin',
00022                                          default=u'View template'),
00023                                description=_(u'help_template_mixin',
00024                                              default=u'Choose a template that will be used for viewing this item.'),
00025                                visible={'view' : 'hidden',
00026                                         'edit' : 'visible'},)
00027                ),
00028     ))
00029 
00030 
00031 class TemplateMixin(Base):
00032     __implements__ = ITemplateMixin
00033 
00034     schema = TemplateMixinSchema
00035 
00036     actions = (
00037         { 'id': 'view',
00038           'name': 'View',
00039           'action': 'string:${object_url}/',
00040           'permissions': (permissions.View,),
00041         },
00042         )
00043 
00044     aliases = {
00045         '(Default)': '',
00046         'index_html': '',
00047         'view': '',
00048         'gethtml': 'source_html',
00049         }
00050 
00051     # if default_view is None TemplateMixin is using the immediate_view from
00052     # the type information
00053     default_view = None
00054     suppl_views = ()
00055 
00056     security = ClassSecurityInfo()
00057 
00058     index_html = None # setting index_html to None forces the usage of __call__
00059 
00060     def __call__(self):
00061         """return a view based on layout"""
00062         v = self.getTemplateFor(self.getLayout())
00063         # rewrap the template in the right context
00064         context = aq_inner(self)
00065         v = v.__of__(context)
00066         return v(context, context.REQUEST)
00067 
00068     def _voc_templates(self):
00069         at = utils.getToolByName(self, TOOL_NAME)
00070         return at.lookupTemplates(self)
00071 
00072     # BBB backward compatibility
00073     templates = _voc_templates
00074 
00075     security.declareProtected(permissions.View, 'getLayout')
00076     def getLayout(self, **kw):
00077         """Get the current layout or the default layout if the current one is None
00078         """
00079         if kw.has_key('schema'):
00080             schema = kw['schema']
00081         else:
00082             schema = self.Schema()
00083             kw['schema'] = schema
00084         value = schema['layout'].get(self, **kw)
00085         if value:
00086             return value
00087         else:
00088             return self.getDefaultLayout()
00089 
00090     security.declareProtected(permissions.View, 'getDefaultLayout')
00091     def getDefaultLayout(self):
00092         """Get the default layout used for TemplateMixin.
00093 
00094         Check the class definition for a attribute called 'default_view' then
00095         check the Factory Type Information (portal_types) for an attribute
00096         immediate_view else finally return the 'base_view' string which is a
00097         autogenerated form from Archetypes.
00098         """
00099         default_view = getattr(aq_base(self), 'default_view', None)
00100         if default_view:
00101             return default_view
00102         immediate_view = getattr(self.getTypeInfo(), 'immediate_view', None)
00103         if immediate_view:
00104             return immediate_view
00105         return 'base_view'
00106 
00107     def getTemplateFor(self, pt, default='base_view'):
00108         """Let the SkinManager handle this.
00109 
00110         But always try to show something.
00111         """
00112         pt = getattr(self, pt, None)
00113         if not pt:
00114             # default is the value of obj.default_view or base_view
00115             default_pt = getattr(self, 'default_view', None)
00116             if not default_pt:
00117                 default_pt = default
00118             return getattr(self, default_pt)
00119         else:
00120             return pt
00121 
00122 InitializeClass(TemplateMixin)
00123 
00124 # BBB backward compatibility
00125 schema = TemplateMixinSchema
00126 getTemplateFor = TemplateMixin.getTemplateFor
00127 
00128 __all__ = ('TemplateMixinSchema', 'TemplateMixin', )