Skip to content
Browse files

Add rudimentary parameter documentation.

  • Loading branch information...
1 parent a584484 commit 4ce5a46349a04306016df9f64e2396635d80e3b2 @chromakode committed Mar 18, 2012
Showing with 61 additions and 8 deletions.
  1. +61 −8 r2/r2/controllers/validator/validator.py
View
69 r2/r2/controllers/validator/validator.py
@@ -44,6 +44,7 @@
from curses.ascii import isprint
import re, inspect
import pycountry
+from itertools import chain
def visible_promo(article):
is_promo = getattr(article, "promoted", None) is not None
@@ -86,6 +87,12 @@ def set_error(self, error, msg_params = {}, field = False):
c.errors.add(error, msg_params = msg_params, field = field)
+ def param_docs(self):
+ param_info = {}
+ for param in filter(None, tup(self.param)):
+ param_info[param] = None
+ return param_info
+
def __call__(self, url):
a = []
if self.param:
@@ -136,6 +143,18 @@ def _make_validated_kw(fn, simple_vals, param_vals, env):
kw[var] = validator(env)
return kw
+def set_api_docs(fn, newfn, simple_vals, param_vals):
+ newfn.__name__ = fn.__name__
+ newfn.__doc__ = fn.__doc__
+ if hasattr(fn, '_api_doc'):
+ newfn._api_doc = fn._api_doc
+ param_info = {}
+ for validator in chain(simple_vals, param_vals.itervalues()):
+ param_info.update(validator.param_docs())
+ newfn._api_doc['parameters'] = param_info
+ return newfn
+
+
def validate(*simple_vals, **param_vals):
def val(fn):
def newfn(self, *a, **env):
@@ -147,10 +166,7 @@ def newfn(self, *a, **env):
except VerifiedUserRequiredException:
return self.intermediate_redirect('/verify')
- newfn.__name__ = fn.__name__
- newfn.__doc__ = fn.__doc__
- if hasattr(fn, '_api_doc'):
- newfn._api_doc = fn._api_doc
+ set_api_docs(fn, newfn, simple_vals, param_vals)
return newfn
return val
@@ -196,10 +212,7 @@ def newfn(self, *a, **env):
responder.send_failure(errors.VERIFIED_USER_REQUIRED)
return self.api_wrapper(responder.make_response())
- newfn.__name__ = fn.__name__
- newfn.__doc__ = fn.__doc__
- if hasattr(fn, '_api_doc'):
- newfn._api_doc = fn._api_doc
+ set_api_docs(fn, newfn, simple_vals, param_vals)
return newfn
return val
return _api_validate
@@ -597,6 +610,11 @@ def run(self, items):
return self.set_error(self._error)
+ def param_docs(self):
+ return {
+ self.param: _('the id of an existing thing')
+ }
+
class VByNameIfAuthor(VByName):
def run(self, fullname):
thing = VByName.run(self, fullname)
@@ -633,6 +651,11 @@ def __init__(self, param=None, fatal=True, *a, **kw):
def run(self, uh):
pass
+ def param_docs(self):
+ return {
+ self.param: _('a modhash')
+ }
+
class VVotehash(Validator):
def run(self, vh, thing_name):
return True
@@ -813,6 +836,11 @@ def run(self, fullname, fullname2):
#else
abort(403, "forbidden")
+ def param_docs(self):
+ return {
+ self.param[0]: _('id of parent thing')
+ }
+
class VSubmitSR(Validator):
def __init__(self, srname_param, linktype_param=None, promotion=False):
self.require_linktype = False
@@ -994,6 +1022,16 @@ def run(self, url, sr = None, resubmit=False):
return url
return self.error(errors.BAD_URL)
+ def param_docs(self):
+ params = {}
+ try:
+ params[self.param[0]] = _('a valid URL')
+ params[self.param[1]] = _('a subreddit')
+ params[self.param[2]] = _('boolean value')
+ except IndexError:
+ pass
+ return params
+
class VOptionalExistingUname(VRequired):
def __init__(self, item, allow_deleted=False, prefer_existing=False,
*a, **kw):
@@ -1034,6 +1072,11 @@ def run(self, name):
self.error()
return user
+ def param_docs(self):
+ return {
+ self.param: _('the name of an existing user')
+ }
+
class VExistingUnameNotSelf(VExistingUname):
def run(self, name):
user = super(VExistingUnameNotSelf, self).run(name)
@@ -1084,6 +1127,11 @@ def run(self, val):
return False
return bool(val)
+ def param_docs(self):
+ return {
+ self.param: _('boolean value')
+ }
+
class VNumber(Validator):
def __init__(self, param, min=None, max=None, coerce = True,
error = errors.BAD_NUMBER, *a, **kw):
@@ -1358,6 +1406,11 @@ def run(self, val):
else:
return val
+ def param_docs(self):
+ return {
+ self.param: _('one of (%s)') % ', '.join(self.options)
+ }
+
class VImageType(Validator):
def run(self, img_type):
if not img_type in ('png', 'jpg'):

0 comments on commit 4ce5a46

Please sign in to comment.
Something went wrong with that request. Please try again.