def _render_obj(self, obj): """ Render object to string. """ if isinstance(obj, basestring): return obj ## 'plone.global_sections' viewlet uses request['URL'] highlight ## selected tab, so it must be overridden but only for a while initial_url = self.request['URL'] try: obj_url = obj.absolute_url() except AttributeError: try: obj_url = obj.context.absolute_url() except AttributeError: obj_url = None if obj_url: self.request['URL'] = obj_url ## breadcrumb implementation in quills uses 'PARENTS', so it must ## be overriden but ony for a while initial_parents = self.request['PARENTS'] if hasattr(obj, 'aq_chain'): self.request['PARENTS'] = obj.aq_chain try: if IResource.providedBy(obj): try: f = open(obj.context.path) result = f.read() f.close() except IOError: log.error("Couldn't open '%s' file with resource" % obj.context.path) return None return result if isinstance(obj, (BrowserView, FSPageTemplate, PythonScript)): try: return obj() except NotFound: log.error("Resource '%s' not found" % repr(obj)) return None if isinstance(obj, (FSFile, FSImage)): return self._render_obj(obj._readFile(None)) if isinstance(obj, FSDTMLMethod): return self._render_obj(obj.read()) mt = None try: mt = obj.aq_base.meta_type except AttributeError: pass if mt in self.file_types or isinstance(obj, (ImageField, OFSImage, Pdata, File)): return self._render_obj(obj.data) if PLONE_RESOURCE_INSTALLED and isinstance(obj, FilesystemFile): if not obj.request: obj.request = self.request return obj().read() if PLONE_APP_BLOB_INSTALLED and IBlobWrapper.providedBy(obj): return obj.data if IBaseObject.providedBy(obj) or isinstance(obj, PloneSite): def_page_id = obj.getDefaultPage() if def_page_id: def_page = obj[def_page_id] return self._render_obj(def_page) view_name = obj.getLayout() view = queryMultiAdapter((obj, self.request), name=view_name) if view_name == 'language-switcher': lang = self.request.get('LANGUAGE') def_page = getattr(obj, lang, None) if def_page: return self._render_obj(def_page) if view: try: return view.context() except (ContentProviderLookupError, TypeError): pass view = obj.restrictedTraverse(view_name, None) if view: try: return view.context() except (AttributeError, TypeError): try: return view() except Exception, error: log.warning("Unable to render view: '%s'! Error occurred: %s" % (view, error)) pass else: try: return obj() except AttributeError: pass finally: ## back to initial url if obj_url: self.request['URL'] = initial_url ## back to initial parents self.request['PARENTS'] = initial_parents log.warning("Not recognized object '%s'!" % repr(obj)) return None
def _render_obj(self, obj, new_req=None): """ Render object to string. """ if isinstance(obj, basestring): return obj if new_req is None: new_req = self.request ## 'plone.global_sections' viewlet uses request['URL'] highlight ## selected tab, so it must be overridden but only for a while try: if IResource.providedBy(obj): try: f = open(obj.context.path) result = f.read() f.close() except AttributeError: result = obj.context.data except IOError: log.error("Couldn't open '%s' file with resource" % ( obj.context.path)) return None return result if isinstance(obj, (BrowserView, FSPageTemplate, PythonScript)): try: return obj() except NotFound: log.error("Resource '%s' not found" % repr(obj)) return None if isinstance(obj, (FSFile, FSImage)): return self._render_obj(obj._readFile(None)) if isinstance(obj, FSDTMLMethod): return self._render_obj(obj.read()) mt = None try: mt = obj.aq_base.portal_type except AttributeError: pass if mt in self.file_types or isinstance(obj, (ImageField, OFSImage, Pdata, File)): return self._render_obj(obj.data) if PLONE_RESOURCE_INSTALLED and isinstance(obj, FilesystemFile): if not obj.request: obj.request = new_req return obj().read() if PLONE_APP_BLOB_INSTALLED and IBlobWrapper.providedBy(obj): return obj.data if PLONE_NAMEDFILE_INSTALLED and \ INamed.providedBy(obj): return obj.data if IBaseObject.providedBy(obj) or isinstance(obj, PloneSite) or \ DEXTERITY_INSTALLED and IDexterityContent.providedBy(obj): default_page_helper = getMultiAdapter((obj, self.request), name='default_page') def_page_id = default_page_helper.getDefaultPage() if def_page_id: def_page = obj[def_page_id] return self._render_obj(def_page) view_name = obj.getLayout() view = queryMultiAdapter((obj, new_req), name=view_name) if view_name == 'language-switcher': lang = new_req.get('LANGUAGE') def_page = getattr(obj, lang, None) if def_page: return self._render_obj(def_page) if view: try: return view.context() except (ContentProviderLookupError, TypeError): pass view = obj.restrictedTraverse(view_name, None) if view: try: return view.context() except (AttributeError, TypeError): try: return view() except Exception, error: log.warning( "Unable to render view: '%s'! Error occurred: %s" % ( view, error)) else: try: return obj() except AttributeError: pass finally: pass log.warning("Not recognized object '%s'!" % repr(obj)) return None