[Zope-Checkins] CVS: Zope3/lib/python/Zope/Publisher - BaseRequest.py:1.1.2.3

Shane Hathaway shane@digicool.com
Thu, 15 Nov 2001 10:11:27 -0500


Update of /cvs-repository/Zope3/lib/python/Zope/Publisher
In directory cvs.zope.org:/tmp/cvs-serv30765

Modified Files:
      Tag: Zope-3x-branch
	BaseRequest.py 
Log Message:
- Code reformatting

- Put PARENTS, PUBLISHED, and AUTHENTICAT* back into self.other since
  BaseRequest doesn't use them directly anymore.

- Clear traversed at close to avoid circ. refs

- Simplified get(), __getitem__(), has_key() pattern

- Made __repr__ return a *short* string

- Allowed the publication object to perform authorization during traversal

- Creation of PARENTS now occurs after traversal.  Simpler contract.
  Virtual hosting will use the traversed attribute instead.


=== Zope3/lib/python/Zope/Publisher/BaseRequest.py 1.1.2.2 => 1.1.2.3 ===
 
 
-_marker=[]
+_marker = []
 
 class BaseRequest:
     """Represents a publishing request.
@@ -33,7 +33,7 @@
     # TODO: add security assertion declaring this to be public
 
     body_instream = None  # The body input stream
-    common = {}   # Common request data
+    common = {}           # Data common to all requests
 
     # _held contains objects kept until the request is closed,
     # such as the database connection closer.
@@ -48,12 +48,8 @@
     steps = ()         # A sequence of names built up during traversal.
     quoted_steps = ()  # Same as steps but quoted.
     
-    # PARENTS contains the objects traversed, innermost parents
-    # first and the root object last.
-    PARENTS = ()
-    PUBLISHED = None          # Object traversed to
-    AUTHENTICATED_USER = None # Authorized user object
-    AUTHENTICATION_PATH = ''  # Unquoted path to parent of user folder
+    # traversed contains the objects traversed in order.
+    traversed = ()
 
     def __init__(self, response, body_instream=None):
         """
@@ -70,6 +66,7 @@
     def close(self):
         self.other.clear()
         self._held = None  # Release held objects
+        self.traversed = None
 
     def processInputs(self):
         """Do any input processing that could raise errors
@@ -106,7 +103,7 @@
         return self.body_instream
     _key_handlers['BODYFILE'] = getBodyFile
 
-    def __getitem__(self, key, default=_marker):
+    def get(self, key, default=None):
         """Get a variable value
 
         Return a value for the required variable name.
@@ -127,20 +124,21 @@
         if v is not _marker:
             return v
 
-        if default is not _marker:
-            return default
-        else:
-            raise KeyError, key
+        return default
 
-    def get(self, key, default=None):
-        return self.__getitem__(key, default)
+    def __getitem__(self, key):
+        res = self.get(key, _marker)
+        if res is _marker:
+            raise KeyError, key
+        else:
+            return res
 
-    def has_key(self,key):
+    def has_key(self, key):
         return self.get(key, _marker) is not _marker
 
     def keys(self):
-        keys = {}
-        #keys.update(self.common)
+        keys = {'URL':1}
+        keys.update(self.common)
         keys.update(self.other)
         return keys.keys()
 
@@ -163,7 +161,9 @@
         L1.sort()
         return "\n".join(map(lambda item: "%s:\t%s" % item, L1))
 
-    __repr__=__str__
+    def __repr__(self):
+        # Returns a *short* string.
+        return '<%s instance, URL=%s>' % (str(self.__class__), self.URL)
 
     def splitPath(self, path):
         # Split and clean up the path.
@@ -216,15 +216,18 @@
 
     def changeTraversalStack(self, names):
         """
-        Private.
-        names should be in reverse.
+        Private.  Can be called by a traversal hook.
+        'names' argument should be in reverse.
         """
         self.to_traverse = names
 
 
     def setAuthenticatedUser(self, user):
-        self.AUTHENTICATED_USER = user
-        self.AUTHENTICATION_PATH = '/'.join(self.steps)
+        """
+        Private.  Should be called by an authorization hook.
+        """
+        self.other['AUTHENTICATED_USER'] = user
+        self.other['AUTHENTICATION_PATH'] = '/'.join(self.steps)
 
 
     def traverse(self, publication, object, path_str):
@@ -235,12 +238,12 @@
         added_default = 0
         to_traverse = self.splitPath(path_str)
 
-        if hasattr(object, '__of__'):
-            # Try to bind the top-level object to the request.
-            object = object.__of__(RequestContainer(self))
+##        if hasattr(object, '__of__'):
+##            # Try to bind the top-level object to the request.
+##            object = object.__of__(RequestContainer(self))
 
-        parents = []
-        parents.append(object)
+        self.traversed = traversed = []
+        traversed.append(object)
         steps = self.steps
         self.quoted_steps = quoted_steps = map(quote, steps)
         to_traverse.reverse()
@@ -267,10 +270,8 @@
                         self.URL = '%s/%s' % (self.URL, qstep)
                     subobject = publication.traverseName(
                         self, object, entry_name)
-                    publication.checkAuthorization(
-                        self, object, entry_name, subobject)
                     object = subobject
-                    parents.append(object)
+                    traversed.append(object)
                     steps.append(entry_name)
 
             elif not added_default:
@@ -289,9 +290,13 @@
         if added_default:
             self.response.setBase(self.URL)
 
+        self.traversed = tuple(traversed)  # No more changes allowed
+        parents = traversed[:]
+        parents.pop()
+        del parents[-1]
         parents.reverse()
-        self.PARENTS = parents
-        self.PUBLISHED = object
+        self.other['PARENTS'] = tuple(parents)
+        self.other['PUBLISHED'] = object
 
         return object