Back to index

plone3  3.1.7
browser.py
Go to the documentation of this file.
00001 from zope.interface import implements
00002 from zope.component import getUtility, getMultiAdapter
00003 
00004 from Acquisition import aq_inner
00005 from Products.Five.browser import BrowserView
00006 from Products.CMFCore.utils import getToolByName
00007 
00008 from plone.app.redirector.interfaces import IFourOhFourView
00009 from plone.app.redirector.interfaces import IRedirectionStorage
00010 from plone.app.redirector.interfaces import IRedirectionPolicy
00011 
00012 from plone.memoize.instance import memoize
00013 
00014 class FourOhFourView(BrowserView):
00015     implements(IFourOhFourView)
00016 
00017     def attempt_redirect(self):
00018         url = self._url()
00019         if not url:
00020             return False
00021 
00022         try:
00023             old_path_elements = self.request.physicalPathFromURL(url)
00024         except ValueError:
00025             return False
00026 
00027         storage = getUtility(IRedirectionStorage)
00028 
00029         old_path = '/'.join(old_path_elements)
00030         new_path = storage.get(old_path)
00031 
00032         if not new_path:
00033 
00034             # If the last part of the URL was a template name, say, look for
00035             # the parent
00036 
00037             if len(old_path_elements) > 1:
00038                 old_path_parent = '/'.join(old_path_elements[:-1])
00039                 template_id = url.split('/')[-1]
00040                 new_path_parent = storage.get(old_path_parent)
00041                 if new_path_parent:
00042                     new_path = new_path_parent + '/' + template_id
00043 
00044         if not new_path:
00045             return False
00046 
00047         url = self.request.physicalPathToURL(new_path)
00048         self.request.response.redirect(url, status=301, lock=1)
00049         return True
00050 
00051     def find_first_parent(self):
00052         path_elements = self._path_elements()
00053         if not path_elements:
00054             return None
00055         portal_state = getMultiAdapter((aq_inner(self.context), self.request), name='plone_portal_state')
00056         portal = portal_state.portal()
00057         for i in range(len(path_elements)-1, 0, -1):
00058             obj = portal.restrictedTraverse('/'.join(path_elements[:i]), None)
00059             if obj is not None:
00060                 return obj
00061         return None
00062 
00063     def search_for_similar(self):
00064         path_elements = self._path_elements()
00065         if not path_elements:
00066             return None
00067         path_elements.reverse()
00068         policy = IRedirectionPolicy(self.context)
00069         ignore_ids = policy.ignore_ids
00070         portal_catalog = getToolByName(self.context, "portal_catalog")
00071         portal_state = getMultiAdapter((aq_inner(self.context), self.request), name='plone_portal_state')
00072         navroot = portal_state.navigation_root_path()
00073         for element in path_elements:
00074             if element not in ignore_ids:
00075                 result_set = portal_catalog(SearchableText=element,
00076                                             path = navroot,
00077                                             portal_type=portal_state.friendly_types(),
00078                                             sort_limit=10)
00079                 if result_set:
00080                     return result_set[:10]
00081         return []
00082 
00083     @memoize
00084     def _url(self):
00085         """Get the current, canonical URL
00086         """
00087         return self.request.get('ACTUAL_URL',
00088                  self.request.get('VIRTUAL_URL',
00089                    self.request.get('URL',
00090                      None)))
00091 
00092     @memoize
00093     def _path_elements(self):
00094         """Get the path to the object implied by the current URL, as a list
00095         of elements. Get None if it can't be calculated or it is not under
00096         the current portal path.
00097         """
00098         url = self._url()
00099         if not url:
00100             return None
00101 
00102         try:
00103             path = '/'.join(self.request.physicalPathFromURL(url))
00104         except ValueError:
00105             return None
00106 
00107         portal_state = getMultiAdapter((aq_inner(self.context), self.request), name='plone_portal_state')
00108         portal_path = '/'.join(portal_state.portal().getPhysicalPath())
00109         if not path.startswith(portal_path):
00110             return None
00111 
00112         return path.split('/')