move comments.requires to views package
This commit is contained in:
parent
1174259b27
commit
ab5d907172
@ -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
|
@ -26,28 +26,6 @@ FIELDS = set(['id', 'parent', 'text', 'author', 'website', 'email', 'mode',
|
||||
'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')
|
||||
def new(app, environ, request, uri):
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user