move comments.requires to views package

This commit is contained in:
Martin Zimmermann 2013-11-08 19:00:01 +01:00
parent 1174259b27
commit ab5d907172
2 changed files with 35 additions and 22 deletions

View File

@ -0,0 +1,35 @@
# -*- encoding: utf-8 -*-
from werkzeug.exceptions import BadRequest
class requires:
"""Verify that the request URL contains and can parse the parameter.
.. code-block:: python
@requires(int, "id")
def view(..., id):
assert isinstance(id, int)
Returns a 400 Bad Request that contains a specific error message.
"""
def __init__(self, type, param):
self.param = param
self.type = type
def __call__(self, func):
def dec(app, env, req, *args, **kwargs):
if self.param not in req.args:
raise BadRequest("missing %s query" % self.param)
try:
kwargs[self.param] = self.type(req.args[self.param])
except TypeError:
raise BadRequest("invalid type for %s, expected %s" % (self.param, self.type))
return func(app, env, req, *args, **kwargs)
return dec

View File

@ -26,28 +26,6 @@ FIELDS = set(['id', 'parent', 'text', 'author', 'website', 'email', 'mode',
'created', 'modified', 'likes', 'dislikes', 'hash']) 'created', 'modified', 'likes', 'dislikes', 'hash'])
class requires:
def __init__(self, type, param):
self.param = param
self.type = type
def __call__(self, func):
def dec(app, env, req, *args, **kwargs):
if self.param not in req.args:
raise BadRequest("missing %s query" % self.param)
try:
kwargs[self.param] = self.type(req.args[self.param])
except TypeError:
raise BadRequest("invalid type for %s, expected %s" % (self.param, self.type))
return func(app, env, req, *args, **kwargs)
return dec
@requires(str, 'uri') @requires(str, 'uri')
def new(app, environ, request, uri): def new(app, environ, request, uri):