Commits

ianb  committed 37547e8

Rename queryvars and postvars to GET and POST (as sole spelling)

  • Participants
  • Parent commits 4b907ae

Comments (0)

Files changed (6)

File docs/index.txt

 .. code-block::
 
     >>> req = Request.blank('/test?check=a&check=b&name=Bob')
-    >>> req.queryvars
+    >>> req.GET
     MultiDict([('check', 'a'), ('check', 'b'), ('name', 'Bob')])
-    >>> req.queryvars['check']
+    >>> req.GET['check']
     'b'
-    >>> req.queryvars.getall('check')
+    >>> req.GET.getall('check')
     ['a', 'b']
-    >>> req.queryvars.items()
+    >>> req.GET.items()
     [('check', 'a'), ('check', 'b'), ('name', 'Bob')]
 
 We'll have to create a request body and change the method to get
-postvars.  Until we do that, the variables are boring:
+POST.  Until we do that, the variables are boring:
 
 .. code-block::
 
-    >>> req.postvars
+    >>> req.POST
     <NoVars: Not a POST request>
-    >>> req.postvars.items()  # NoVars can be read like a dict, but not written
+    >>> req.POST.items()  # NoVars can be read like a dict, but not written
     []
     >>> req.method = 'POST'
     >>> req.body = 'name=Joe&email=joe@example.com'
-    >>> req.postvars
+    >>> req.POST
     MultiDict([('name', 'Joe'), ('email', 'joe@example.com')])
-    >>> req.postvars['name']
+    >>> req.POST['name']
     'Joe'
 
 Often you won't care where the variables come from.  (Even if you care
     name: 'Joe'
     email: 'joe@example.com'
 
-There are aliases available as well: ``req.GET`` and ``req.POST``.
-These are not entirely accurate, as *all* requests can have query
-strings (not just GET requests), and not all POST requests use a form
-body (if you do a POST as with `Atompub/APP <http://atompub.org/>`_,
-you'll have an XML body instead of variables, and req.postvars will be
-empty).
-
 Unicode Variables
 ~~~~~~~~~~~~~~~~~
 
 .. code-block::
 
     >>> req.charset = 'utf8'
-    >>> req.queryvars
+    >>> req.GET
     UnicodeMultiDict([(u'check', u'a'), (u'check', u'b'), (u'name', u'Bob')])
 
-If you always want ``str`` values, you can use ``req.str_queryvars``
-and ``str_postvars`` (or ``req.str_GET`` and ``req.str_POST``).
+If you always want ``str`` values, you can use ``req.str_GET``
+and ``str_POST``.
 
 Cookies
 -------

File docs/news.txt

 * Made separate ``.exception`` attribute on ``webob.exc`` objects,
   since new-style classes can't be raised as exceptions.
 
+* Deprecate ``req.postvars`` and ``req.queryvars``, instead using the
+  sole names ``req.GET`` and ``req.POST`` (also ``req.str_GET`` and
+  ``req.str_POST``).  The old names give a warning; will give an error
+  in next release, and be completely gone in the following release.
+
 0.8.1
 -----
 

File tests/test_request.py

     request.remote_user = 'bob'
     return [
         'Hello world!\n',
-        'The get is %r' % request.queryvars,
-        ' and Val is %s\n' % request.queryvars.get('name'),
+        'The get is %r' % request.GET,
+        ' and Val is %s\n' % request.GET.get('name'),
         'The languages are: %s\n' % request.accept_language.best_matches('en-US'),
         'The accepttypes is: %s\n' % request.accept.best_match(['text/html', 'application/xml']),
-        'post is %r\n' % request.postvars,
+        'post is %r\n' % request.POST,
         'params is %r\n' % request.params,
         'cookies is %r\n' % request.cookies,
         'body: %r\n' % request.body,

File tests/test_request.txt

     >>> req.method = 'POST'
     >>> req.body_file = StringIO(body)
     >>> req.environ['CONTENT_LENGTH'] = str(len(body))
-    >>> vars = req.str_postvars
+    >>> vars = req.str_POST
     >>> vars
     MultiDict([('var1', 'value1'), ('var2', 'value2'), ('rep', '1'), ('rep', '2')])
-    >>> vars is req.str_postvars
+    >>> vars is req.str_POST
     True
-    >>> req.postvars
+    >>> req.POST
     MultiDict([('var1', 'value1'), ('var2', 'value2'), ('rep', '1'), ('rep', '2')])
     >>> req.charset = 'utf8'
-    >>> req.postvars
+    >>> req.POST
     UnicodeMultiDict([(u'var1', u'value1'), (u'var2', u'value2'), (u'rep', u'1'), (u'rep', u'2')])
 
 Note that the variables are there for GET requests and non-form POST
 requests, but they are empty and read-only:
 
     >>> req = Request.blank('/')
-    >>> req.str_postvars
+    >>> req.str_POST
     <NoVars: Not a POST request>
-    >>> req.str_postvars.items()
+    >>> req.str_POST.items()
     []
-    >>> req.str_postvars['x'] = 'y'
+    >>> req.str_POST['x'] = 'y'
     Traceback (most recent call last):
         ...
     KeyError: 'Cannot add variables: Not a POST request'
     >>> req.method = 'POST'
-    >>> req.str_postvars
+    >>> req.str_POST
     MultiDict([])
     >>> req.content_type = 'text/xml'
     >>> req.body_file = StringIO('<xml></xml>')
-    >>> req.str_postvars
+    >>> req.str_POST
     <NoVars: Not an HTML form submission (Content-Type: text/xml)>
     >>> req.body
     '<xml></xml>'
 You can also get access to the query string variables, of course:
 
     >>> req = Request.blank('/?a=b&d=e&d=f')
-    >>> req.queryvars
+    >>> req.GET
     MultiDict([('a', 'b'), ('d', 'e'), ('d', 'f')])
-    >>> req.queryvars['d']
+    >>> req.GET['d']
     'f'
-    >>> req.queryvars.getall('d')
+    >>> req.GET.getall('d')
     ['e', 'f']
     >>> req.method = 'POST'
     >>> req.body = 'x=y&d=g'
 Some query tests:
 
     >>> req = Request.blank('/')
-    >>> req.queryvars.get('unknown')
-    >>> req.queryvars.get('unknown', '?')
+    >>> req.GET.get('unknown')
+    >>> req.GET.get('unknown', '?')
     '?'
-    >>> req.postvars.get('unknown')
-    >>> req.postvars.get('unknown', '?')
+    >>> req.POST.get('unknown')
+    >>> req.POST.get('unknown', '?')
     '?'
     >>> req.params.get('unknown')
     >>> req.params.get('unknown', '?')

File webob/__init__.py

 import time
 import calendar
 import tempfile
+import warnings
 from webob.util.dictmixin import DictMixin
 from webob.datastruct import EnvironHeaders
 from webob.multidict import MultiDict, UnicodeMultiDict, NestedMultiDict, NoVars
     return "  For more information on %s see `section %s <%s>`_." % (
         header, section, link)
 
+class deprecated_property(object):
+    """
+    Wraps a decorator, with a deprecation warning or error
+    """
+    def __init__(self, decorator, attr, message, warning=True):
+        self.decorator = decorator
+        self.attr = attr
+        self.message = message
+        self.warning = warning
+
+    def __get__(self, obj, type=None):
+        if obj is None:
+            return self
+        self.warn()
+        return self.decorator.__get__(obj, type)
+
+    def __set__(self, obj, value):
+        self.warn()
+        self.decorator.__set__(obj, value)
+
+    def __delete__(self, obj):
+        self.warn()
+        self.decorator.__delete__(obj)
+
+    def __repr__(self):
+        return '<Deprecated attribute %s: %r>' % (
+            self.attr,
+            self.decorator)
+
+    def warn(self):
+        if not self.warning:
+            raise DeprecationWarning(
+                'The attribute %s is deprecated: %s' % (self.attr, self.message))
+        else:
+            warnings.warn(
+                'The attribute %s is deprecated: %s' % (self.attr, self.message),
+                DeprecationWarning,
+                stacklevel=3)
+
 def _parse_date(value):
     if not value:
         return None
 
     body = property(_body__get, _body__set, _body__del, doc=_body__get.__doc__)
 
-    def str_postvars(self):
+    def str_POST(self):
         """
         Return a MultiDict containing all the variables from a POST
         form request.  Does *not* return anything for non-POST
         env['webob._parsed_post_vars'] = (vars, self.body_file)
         return vars
 
-    str_postvars = property(str_postvars, doc=str_postvars.__doc__)
+    str_POST = property(str_POST, doc=str_POST.__doc__)
 
-    str_POST = str_postvars
+    str_postvars = deprecated_property(str_POST, 'str_postvars',
+                                       'use str_POST instead')
 
-    def postvars(self):
+    def POST(self):
         """
-        Like ``.str_postvars``, but may decode values and keys
+        Like ``.str_POST``, but may decode values and keys
         """
-        vars = self.str_postvars
+        vars = self.str_POST
         if self.charset:
             vars = UnicodeMultiDict(vars, encoding=self.charset,
                                     errors=self.unicode_errors,
                                     decode_keys=self.decode_param_names)
         return vars
 
-    postvars = property(postvars, doc=postvars.__doc__)
+    POST = property(POST, doc=POST.__doc__)
 
-    POST = postvars
+    postvars = deprecated_property(POST, 'postvars',
+                                   'use POST instead')
 
-    def str_queryvars(self):
+    def str_GET(self):
         """
         Return a MultiDict containing all the variables from the
         QUERY_STRING.
         env['webob._parsed_query_vars'] = (vars, source)
         return vars
 
-    str_queryvars = property(str_queryvars, doc=str_queryvars.__doc__)
+    str_GET = property(str_GET, doc=str_GET.__doc__)
 
-    str_GET = str_queryvars
+    str_queryvars = deprecated_property(str_GET, 'str_queryvars',
+                                        'use str_GET instead')
+                                        
 
-    def queryvars(self):
+    def GET(self):
         """
-        Like ``.str_queryvars``, but may decode values and keys
+        Like ``.str_GET``, but may decode values and keys
         """
-        vars = self.str_queryvars
+        vars = self.str_GET
         if self.charset:
             vars = UnicodeMultiDict(vars, encoding=self.charset,
                                     errors=self.unicode_errors,
                                     decode_keys=self.decode_param_names)
         return vars
 
-    queryvars = property(queryvars, doc=queryvars.__doc__)
+    GET = property(GET, doc=GET.__doc__)
 
-    GET = queryvars
+    queryvars = deprecated_property(GET, 'queryvars',
+                                    'use GET instead')
 
     def str_params(self):
         """
         A dictionary-like object containing both the parameters from
         the query string and request body.
         """
-        return NestedMultiDict(self.queryvars, self.postvars)
+        return NestedMultiDict(self.str_GET, self.str_POST)
 
     str_params = property(str_params, doc=str_params.__doc__)
 

File webob/exc.py

 import urlparse
 import sys
 from string import Template
+import types
 from webob import Response, Request, html_escape
 
 tag_re = re.compile(r'<.*?>', re.S)
     def exception(self):
         return self
 
-class WSGIHTTPException(HTTPException, Response):
+class WSGIHTTPException(Response, HTTPException):
 
     ## You should set in subclasses:
     # code = 200
         else:
             args['html_comment'] = ''
         body_tmpl = self.body_template_obj
-        if HTTPException.body_template_obj is not self.body_template_obj:
+        if WSGIHTTPException.body_template_obj is not self.body_template_obj:
             # Custom template; add headers to args
             for k, v in environ.items():
                 args[k] = escape(v)
         if sys.version_info >= (2, 5):
             return self
         else:
-            return RealHTTPException(self.detail, self)
+            return HTTPException(self.detail, self)
 
-class HTTPError(HTTPException):
+class HTTPError(WSGIHTTPException):
     """
     base class for status codes in the 400's and 500's
 
     typically results in the 400's and 500's.
     """
 
-class HTTPRedirection(HTTPException):
+class HTTPRedirection(WSGIHTTPException):
     """
     base class for 300's status code (redirections)
 
     condition.
     """
 
-class HTTPOk(HTTPException):
+class HTTPOk(WSGIHTTPException):
     """
     Base class for the 200's status code (successful responses)
     """
     for name in dir(httpexceptions):
         obj = globals().get(name)
         if (obj and isinstance(obj, type) and issubclass(obj, HTTPException)
-            and obj is not HTTPException):
+            and obj is not HTTPException
+            and obj is not WSGIHTTPException):
             obj.__bases__ = obj.__bases__ + (getattr(httpexceptions, name),)
     del name, obj, httpexceptions
 
 __all__ = ['HTTPExceptionMiddleware', 'status_map']
 status_map={}
 for name, value in globals().items():
-    if (isinstance(value, type) and issubclass(value, HTTPException)
+    if (isinstance(value, (type, types.ClassType)) and issubclass(value, HTTPException)
         and not name.startswith('_')):
         __all__.append(name)
-        if value.code:
+        if getattr(value, 'code', None):
             status_map[value.code]=value
 del name, value