Logo Search packages:      
Sourcecode: zope-linguaplone version File versions

patches.py

# Copyright (C) 2004 Helge Tesdal <info@plonesolutions.com>
# Plone Solutions AS http://www.plonesolutions.com

# 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.

"""
Patches.

$Id: Patches.py,v 1.9.2.2 2004/04/21 14:39:23 tesdal Exp $
"""

__version__ = "$Revision: 1.9.2.2 $"

from Products.LinguaPlone.config import GLOBAL_REQUEST_PATCH
from Products.LinguaPlone.config import I18NAWARE_CATALOG
from Products.LinguaPlone.config import PKG_NAME


# PATCH 1
#
# Makes REQUEST available from the Globals module.
#
# It's needed because context is not available in the __of__ method,
# so we can't get REQUEST with acquisition. And we need REQUEST for
# local properties (see LocalPropertyManager.pu).
#
# This patch was taken from Localizer (http://www.localizer.org)
# so this software runs indepently of Localizer
#
# The patch is inspired in a similar patch by Tim McLaughlin, see
# "http://dev.zope.org/Wikis/DevSite/Proposals/GlobalGetRequest".
# Thanks Tim!!
#
if GLOBAL_REQUEST_PATCH:
    from thread import get_ident
    from Acquisition import aq_parent
    from ZPublisher import Publish, mapply
    from zLOG import LOG, PROBLEM
    import Globals

    def get_request():
        """Get a request object"""
        return Publish._requests.get(get_ident(), None)

    def new_publish(request, module_name, after_list, debug=0):
        id = get_ident()
        Publish._requests[id] = request
        x = Publish.old_publish(request, module_name, after_list, debug)
        try:
            del Publish._requests[id]
        except KeyError:
            # Some people has reported that sometimes a KeyError exception is
            # raised in the previous line, I haven't been able to reproduce it.
            # This try/except clause seems to work. I'd prefer to understand
            # what is happening.
            LOG(PKG_NAME, PROBLEM,
                "The thread number %s doesn't have an associated request object." % id)
        return x

    if not hasattr(Globals, 'get_request'):
        # Apply patch
        Publish._requests = {}
        Publish.old_publish = Publish.publish
        Publish.publish = new_publish
        Globals.get_request = get_request


# PATCH 2
#
# Patches the catalog tool to filter languages
#
if I18NAWARE_CATALOG:
    from Products.CMFPlone.CatalogTool import BaseTool
    from Products.CMFPlone.CatalogTool import CatalogTool
    from Products.CMFCore.utils import getToolByName

    def searchResults(self, REQUEST=None, **kw):
        """ Calls ZCatalog.searchResults with extra arguments that
            limit the results to what the user is allowed to see.

            This version only returns the results for the current
            language, unless you explicitly ask for all results by
            providing the Language="all" keyword.
        """
        languageTool = getToolByName(self, 'portal_languages', None)

        # When searching on certain indexes we don't want language filtering.
        nofilterkeys = ['Language', 'UID', 'id', 'getId']

        def filterSearch(dict, keys=nofilterkeys):
            if not dict:
                return 1
            for key in keys:
                if dict.has_key(key):
                    return 0
            return 1

        if languageTool is not None and filterSearch(REQUEST) and filterSearch(kw):
            try:
                kw['Language'] = [languageTool.getPreferredLanguage(), 'neutral']
            except AttributeError:
                pass
        # 'all' deletes the query key
        elif REQUEST and REQUEST.get('Language', '') == 'all':
            del REQUEST['Language']
        elif kw.get('Language', '') == 'all':
            del kw['Language']

        return BaseTool.searchResults(self, REQUEST, **kw)

    CatalogTool.searchResults = searchResults
    CatalogTool.__call__ = searchResults


Generated by  Doxygen 1.6.0   Back to index