document.erp5.StaticWebSection.py 5.07 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
# -*- coding: utf-8 -*-
##############################################################################
#
# Copyright (c) 2016 Nexedi SA and Contributors. All Rights Reserved.
#                    Cédric Le Ninivin <cedric.leninivin@nexedi.com>
#
# WARNING: This program as such is intended to be used by professional
# programmers who take the whole responsability of assessing all potential
# consequences resulting from its eventual inadequacies and bugs
# End users who are looking for a ready-to-use solution with commercial
# garantees and support are strongly adviced to contract a Free Software
# Service Company
#
# This program is Free Software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
#
##############################################################################

from AccessControl import ClassSecurityInfo
from Acquisition import aq_base
from OFS.Traversable import NotFound

34
from erp5.component.mixin.DocumentExtensibleTraversableMixin import DocumentExtensibleTraversableMixin
35
from erp5.component.document.WebSection import WebSection
36 37 38 39
from Products.ERP5Type import Permissions

from webdav.NullResource import NullResource

40 41
import urllib

42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
MARKER = []

class StaticWebSection(WebSection):
  """
  This Web Section only get resource from the DMS.
  The standard acquisition is disabled here.
  """

  portal_type = 'Static Web Section'
  security = ClassSecurityInfo()
  security.declareObjectProtected(Permissions.AccessContentsInformation)

  def getExtensibleContent(self, request, name):
    stack = request['TraversalRequestNameStack']

    if isinstance(name, list):
      name = name[0]
59 60 61 62
    if not name or name in ("/",):
      url_list = []
    else:
      url_list = [name]
63
    while len(stack):
64 65 66 67
      if stack[-1] not in ('/', ''):
        url_list.append(stack.pop())
      else:
        stack.pop()
68 69 70 71

    # Drop the automatically added VirtualHostMonster object ID
    virtual_url_part_tuple = request.get('VIRTUAL_URL_PARTS', None)
    if (virtual_url_part_tuple is not None) and \
72
       (not urllib.unquote(virtual_url_part_tuple[-1]).endswith("/".join(url_list))):
73 74
      url_list.pop(0)

75
    if request.get('ACTUAL_URL', '').endswith("/"): # or len(url_list) == 0:
76
      url_list.append("index.html")
77

78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
    return DocumentExtensibleTraversableMixin.getExtensibleContent(self, request, "/".join(url_list))

  def _getStaticDocument(self, request, name):
    try:
      return self[name]
    except KeyError:
      pass
    document = self.getExtensibleContent(request, name)
    if document is not None:
      return aq_base(document).__of__(self)

    try:
      return getattr(self, name)
    except AttributeError:
      pass

    # Not found section
    method = request.get('REQUEST_METHOD', 'GET')
    if not method in ('GET', 'POST'):
      return NullResource(self, name, request).__of__(self)
    # Waaa. unrestrictedTraverse calls us with a fake REQUEST.
    # There is proabably a better fix for this.
    try:
      request.RESPONSE.notFoundError("%s\n%s" % (name, method))
    except AttributeError:
103
      raise KeyError(name)
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138


  security.declareProtected(Permissions.View, '__bobo_traverse__')
  def __bobo_traverse__(self, request, name):
    """
      Taken from WebSection Bobo Traverse, the difference is that
      __bobo_traverse__ from DocumentExtensibleTraversableMixin is not called
    """
    # Register current web site physical path for later URL generation
    if request.get(self.web_section_key, MARKER) is MARKER:
      request[self.web_section_key] = self.getPhysicalPath()
      # Normalize web parameter in the request
      # Fix common user mistake and transform '1' string to boolean
      for web_param in ['ignore_layout', 'editable_mode']:
        if hasattr(request, web_param):
          param = getattr(request, web_param, None)
          if isinstance(param, (list, tuple)):
            param = param[0]
          if param in ('1', 1, True):
            request.set(web_param, True)
          else:
            request.set(web_param, False)

    document = None
    try:
      document = self._getStaticDocument(request, name)
    except NotFound:
      not_found_page_ref = self.getLayoutProperty('layout_not_found_page_reference')
      if not_found_page_ref:
        document = DocumentExtensibleTraversableMixin.getDocumentValue(self, name=not_found_page_ref)
      if document is None:
        # if no document found, fallback on default page template
        document = DocumentExtensibleTraversableMixin.__bobo_traverse__(self, request,
          '404.error.page')
    return document