[Zope3-checkins] SVN: Zope3/trunk/src/zope/publisher/ Fixed representation for Record objects to be compatible with Zope 2

Dmitry Vasiliev dima at hlabs.spb.ru
Mon Mar 20 04:11:11 EST 2006


Log message for revision 66109:
  Fixed representation for Record objects to be compatible with Zope 2
  

Changed:
  U   Zope3/trunk/src/zope/publisher/browser.py
  U   Zope3/trunk/src/zope/publisher/tests/test_browserrequest.py

-=-
Modified: Zope3/trunk/src/zope/publisher/browser.py
===================================================================
--- Zope3/trunk/src/zope/publisher/browser.py	2006-03-20 03:53:25 UTC (rev 66108)
+++ Zope3/trunk/src/zope/publisher/browser.py	2006-03-20 09:11:11 UTC (rev 66109)
@@ -21,8 +21,8 @@
 $Id$
 """
 import re
-from types import ListType, TupleType, StringType, StringTypes
-from cgi import FieldStorage, escape
+from types import ListType, TupleType, StringType
+from cgi import FieldStorage
 
 from zope.interface import implements, directlyProvides
 from zope.i18n.interfaces import IUserPreferredLanguages
@@ -31,10 +31,9 @@
 from zope.publisher.interfaces.browser import IDefaultBrowserLayer
 from zope.publisher.interfaces.browser import IBrowserApplicationRequest
 
-from zope.publisher import contenttype
 from zope.publisher.http import HTTPRequest, HTTPResponse
-from zope.publisher.base import BaseRequest
 
+
 __ArrayTypes = (ListType, TupleType)
 
 start_of_header_search=re.compile('(<head[^>]*>)', re.I).search
@@ -42,7 +41,6 @@
 isRelative = re.compile("[-_.!~*a-zA-z0-9'()@&=+$,]+(/|$)").match
 newlines = re.compile('\r\n|\n\r|\r')
 
-
 def is_text_html(content_type):
     return content_type.startswith('text/html')
 
@@ -175,12 +173,13 @@
     'HTTP_CGI_AUTHORIZATION': 1,
     }.has_key
 
-
 class Record(object):
 
+    _attrs = frozenset(('get', 'keys', 'items', 'values', 'copy',
+                       'has_key', '__contains__'))
+
     def __getattr__(self, key, default=None):
-        if key in ('get', 'keys', 'items', 'values', 'copy',
-                   'has_key', '__contains__'):
+        if key in self._attrs:
             return getattr(self.__dict__, key)
         raise AttributeError(key)
 
@@ -188,16 +187,17 @@
         return self.__dict__[key]
 
     def __str__(self):
-        L1 = self.__dict__.items()
-        L1.sort()
-        return ", ".join(["%s: %s" % item for item in L1])
+        items = self.__dict__.items()
+        items.sort()
+        return "{" + ", ".join(["%s: %s" % item for item in items]) + "}"
 
     def __repr__(self):
-        L1 = self.__dict__.items()
-        L1.sort()
-        return ', '.join(["%s: %s" % (key, repr(value)) for key, value in L1])
+        items = self.__dict__.items()
+        items.sort()
+        return ("{"
+            + ", ".join(["%s: %s" % (key, repr(value))
+            for key, value in items]) + "}")
 
-
 class BrowserRequest(HTTPRequest):
     implements(IBrowserRequest, IBrowserApplicationRequest)
 

Modified: Zope3/trunk/src/zope/publisher/tests/test_browserrequest.py
===================================================================
--- Zope3/trunk/src/zope/publisher/tests/test_browserrequest.py	2006-03-20 03:53:25 UTC (rev 66108)
+++ Zope3/trunk/src/zope/publisher/tests/test_browserrequest.py	2006-03-20 09:11:11 UTC (rev 66109)
@@ -239,8 +239,9 @@
         self.assertEqual(request.form[u'b'], u'1')
         self.assertEqual(request.form[u'a'].keys(), [u'x'])
         self.assertEqual(request.form[u'a'][u'x'], (u'5',u'6'))
-        self.assertEqual(str(request.form[u'a']), "x: (u'5', u'6')")
-        self.assertEqual(repr(request.form[u'a']), "x: (u'5', u'6')")
+        self.assertEqual(request.form[u'a'].x, (u'5',u'6'))
+        self.assertEqual(str(request.form[u'a']), "{x: (u'5', u'6')}")
+        self.assertEqual(repr(request.form[u'a']), "{x: (u'5', u'6')}")
 
     def testFormRecordsTypes(self):
         extra = {'QUERY_STRING':'a.x:records=5&a.x:records=6&b=1'}
@@ -252,10 +253,35 @@
         self.assertEqual(request.form[u'b'], u'1')
         self.assertEqual(len(request.form[u'a']), 2)
         self.assertEqual(request.form[u'a'][0][u'x'], u'5')
+        self.assertEqual(request.form[u'a'][0].x, u'5')
         self.assertEqual(request.form[u'a'][1][u'x'], u'6')
-        self.assertEqual(str(request.form[u'a']), "[x: u'5', x: u'6']")
-        self.assertEqual(repr(request.form[u'a']), "[x: u'5', x: u'6']")
+        self.assertEqual(request.form[u'a'][1].x, u'6')
+        self.assertEqual(str(request.form[u'a']), "[{x: u'5'}, {x: u'6'}]")
+        self.assertEqual(repr(request.form[u'a']), "[{x: u'5'}, {x: u'6'}]")
 
+    def testFormMultipleRecordsTypes(self):
+        extra = {'QUERY_STRING':'a.x:records:int=5&a.y:records:int=51'
+            '&a.x:records:int=6&a.y:records:int=61&b=1'}
+        request = self._createRequest(extra)
+        publish(request)
+        keys = request.form.keys()
+        keys.sort()
+        self.assertEqual(keys, [u'a',u'b'])
+        self.assertEqual(request.form[u'b'], u'1')
+        self.assertEqual(len(request.form[u'a']), 2)
+        self.assertEqual(request.form[u'a'][0][u'x'], 5)
+        self.assertEqual(request.form[u'a'][0].x, 5)
+        self.assertEqual(request.form[u'a'][0][u'y'], 51)
+        self.assertEqual(request.form[u'a'][0].y, 51)
+        self.assertEqual(request.form[u'a'][1][u'x'], 6)
+        self.assertEqual(request.form[u'a'][1].x, 6)
+        self.assertEqual(request.form[u'a'][1][u'y'], 61)
+        self.assertEqual(request.form[u'a'][1].y, 61)
+        self.assertEqual(str(request.form[u'a']),
+            "[{x: 5, y: 51}, {x: 6, y: 61}]")
+        self.assertEqual(repr(request.form[u'a']),
+            "[{x: 5, y: 51}, {x: 6, y: 61}]")
+
     def testFormListRecordTypes(self):
         extra = {'QUERY_STRING':'a.x:list:record=5&a.x:list:record=6&b=1'}
         request = self._createRequest(extra)
@@ -266,8 +292,9 @@
         self.assertEqual(request.form[u'b'], u'1')
         self.assertEqual(request.form[u'a'].keys(), [u'x'])
         self.assertEqual(request.form[u'a'][u'x'], [u'5',u'6'])
-        self.assertEqual(str(request.form[u'a']), "x: [u'5', u'6']")
-        self.assertEqual(repr(request.form[u'a']), "x: [u'5', u'6']")
+        self.assertEqual(request.form[u'a'].x, [u'5',u'6'])
+        self.assertEqual(str(request.form[u'a']), "{x: [u'5', u'6']}")
+        self.assertEqual(repr(request.form[u'a']), "{x: [u'5', u'6']}")
 
     def testFormListTypes2(self):
         extra = {'QUERY_STRING':'a=5&a=6&b=1'}



More information about the Zope3-Checkins mailing list