Added site folder's views to handle automatic redirections
authorThierry Florac <thierry.florac@onf.fr>
Mon, 05 Nov 2018 13:21:33 +0100
changeset 199 efe8a1c07d82
parent 198 f75645bf1002
child 200 0f13488cf469
Added site folder's views to handle automatic redirections
src/pyams_default_theme/shared/site/__init__.py
--- a/src/pyams_default_theme/shared/site/__init__.py	Tue Oct 30 12:32:31 2018 +0100
+++ b/src/pyams_default_theme/shared/site/__init__.py	Mon Nov 05 13:21:33 2018 +0100
@@ -12,14 +12,19 @@
 
 __docformat__ = 'restructuredtext'
 
+from pyramid.httpexceptions import HTTPSeeOther
 from zope.interface import Interface
 
-from pyams_content.shared.site.interfaces import ISiteFolder, ISiteManager
+from pyams_content.shared.site.interfaces import FOLDER_REDIRECT_DISPLAY_MODE, ISiteFolder, ISiteManager
 from pyams_i18n.interfaces import II18n
+from pyams_pagelet.pagelet import pagelet_config
+from pyams_portal.views import PortalContextIndexPage, PortalContextPreviewPage
 from pyams_skin.interfaces.metas import IHTMLContentMetas
 from pyams_skin.layer import IPyAMSUserLayer
 from pyams_skin.metas import ContentMeta, HTMLTagMeta
 from pyams_utils.adapter import ContextRequestViewAdapter, adapter_config
+from pyams_utils.interfaces import VIEW_SYSTEM_PERMISSION
+from pyams_utils.url import relative_url
 
 
 @adapter_config(name='title', context=(ISiteManager, IPyAMSUserLayer, Interface), provides=IHTMLContentMetas)
@@ -38,3 +43,27 @@
             description = i18n.query_attribute('header', request=self.request)
         if description:
             yield ContentMeta('description', description)
+
+
+@pagelet_config(name='', context=ISiteFolder, layer=IPyAMSUserLayer)
+class SiteFolderIndexPage(PortalContextIndexPage):
+    """Site folder index page"""
+
+    def __call__(self, **kwargs):
+        if self.context.navigation_mode == FOLDER_REDIRECT_DISPLAY_MODE:
+            target = next(self.context.get_visible_items(self.request), None)
+            if target is not None:
+                return HTTPSeeOther(relative_url(target, request=self.request))
+        return super(SiteFolderIndexPage, self).__call__(**kwargs)
+
+
+@pagelet_config(name='preview.html', context=ISiteFolder, layer=IPyAMSUserLayer, permission=VIEW_SYSTEM_PERMISSION)
+class SiteFolderPreviewPage(PortalContextPreviewPage):
+    """Site folder preview page"""
+
+    def __call__(self, **kwargs):
+        if self.context.navigation_mode == FOLDER_REDIRECT_DISPLAY_MODE:
+            target = next(self.context.get_visible_items(self.request), None)
+            if target is not None:
+                return HTTPSeeOther(relative_url(target, request=self.request, view_name='preview.html'))
+        return super(SiteFolderPreviewPage, self).__call__(**kwargs)