From ab5d9071724628b66fee01598ceadca4cde46b72 Mon Sep 17 00:00:00 2001 From: Martin Zimmermann Date: Fri, 8 Nov 2013 19:00:01 +0100 Subject: [PATCH] move comments.requires to views package --- isso/views/__init__.py | 35 +++++++++++++++++++++++++++++++++++ isso/views/comments.py | 22 ---------------------- 2 files changed, 35 insertions(+), 22 deletions(-) diff --git a/isso/views/__init__.py b/isso/views/__init__.py index e69de29..6031793 100644 --- a/isso/views/__init__.py +++ b/isso/views/__init__.py @@ -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 diff --git a/isso/views/comments.py b/isso/views/comments.py index 9173e07..52a3f9c 100644 --- a/isso/views/comments.py +++ b/isso/views/comments.py @@ -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):