# Calendaring is a simple CMF/Plone calendaring implementation.
# Copyright (C) 2004 Enfold Systems
#
# 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
#
"""
$Id: traversal.py,v 1.1.1.1 2004/06/19 14:53:33 dreamcatcher Exp $
"""
from Acquisition import Implicit
from Products.CMFCore.utils import getToolByName
from Products.Calendaring.model import queryView

class ModelViewMixin(Implicit):

    def __bobo_traverse__(self, info, name=None):
        # Try to lookup a view first
        view = queryView(name, None)
        if view is not None:
            return view(REQUEST=self.REQUEST).__of__(self)
        # View not found, probably a UID.
        rc = getToolByName(self, 'reference_catalog')
        obj = rc.lookupObject(name)
        if obj is None:
            # Uhm, it wasn't a UID.
            # Use the normal means.
            return getattr(self, name, None)
        # Found an object by the given UID.
        # Try to get a view and wrap it around the object.
        stack = info.get('TraversalRequestNameStack')
        if stack:
            v_name = stack.pop()
            factory = queryView(v_name, None)
            if factory is None:
                # Nope, no view found. Return to stack.
                stack.append(v_name)
                return obj
            return factory(REQUEST=self.REQUEST).__of__(obj)
        return obj
