mirror of
https://github.com/reddit-archive/reddit.git
synced 2026-01-27 15:58:06 -05:00
consolidate validators that do Thing._by_fullname lookups
This commit is contained in:
@@ -699,7 +699,6 @@ class ApiController(RedditController):
|
||||
spam = (c.user._spam or
|
||||
errors.BANNED_IP in c.errors or
|
||||
errors.CHEATER in c.errors)
|
||||
|
||||
if thing:
|
||||
dir = (True if dir > 0
|
||||
else False if dir < 0
|
||||
@@ -1206,7 +1205,7 @@ class ApiController(RedditController):
|
||||
tr._is_enabled = True
|
||||
|
||||
|
||||
@validatedForm(links = VFullNames('links'))
|
||||
@validatedForm(links = VByName('links', thing_cls = Link, multiple = True))
|
||||
def POST_fetch_links(self, form, jquery, links):
|
||||
b = IDBuilder([l._fullname for l in links],
|
||||
wrap = ListingController.builder_wrapper)
|
||||
@@ -1329,10 +1328,9 @@ class ApiController(RedditController):
|
||||
return UploadedImage(_('saved'), thumbnail_url(link), "",
|
||||
errors = errors).render()
|
||||
|
||||
@validatedForm(ids = VLinkFullnames('ids'))
|
||||
def POST_onload(self, form, jquery, ids, *a, **kw):
|
||||
@validatedForm(promoted = VByName('ids', thing_cls = Link, multiple = True))
|
||||
def POST_onload(self, form, jquery, promoted, *a, **kw):
|
||||
# make sure that they are really promoted
|
||||
promoted = Link._by_fullname(ids, data = True, return_dict = False)
|
||||
promoted = [ l for l in promoted if l.promoted ]
|
||||
|
||||
for l in promoted:
|
||||
|
||||
@@ -337,11 +337,11 @@ class ByIDController(ListingController):
|
||||
def query(self):
|
||||
return self.names
|
||||
|
||||
@validate(names = VLinkFullnames("names"))
|
||||
def GET_listing(self, names, **env):
|
||||
if not names:
|
||||
@validate(links = VByName("names", thing_cls = Link, multiple = True))
|
||||
def GET_listing(self, links, **env):
|
||||
if not links:
|
||||
return self.abort404()
|
||||
self.names = names
|
||||
self.names = [l._fullname for l in links]
|
||||
return ListingController.GET_listing(self, **env)
|
||||
|
||||
|
||||
|
||||
@@ -264,20 +264,6 @@ def chksrname(x):
|
||||
return None
|
||||
|
||||
|
||||
class VLinkFullnames(Validator):
|
||||
"A space- or comma-separated list of fullnames for Links"
|
||||
valid_re = re.compile(r'^(' + Link._type_prefix + str(Link._type_id) +
|
||||
r'_[0-9a-z]+[ ,]?)+$')
|
||||
splitter = re.compile('[ ,]+')
|
||||
|
||||
def __init__(self, item, *a, **kw):
|
||||
self.item = item
|
||||
Validator.__init__(self, item, *a, **kw)
|
||||
|
||||
def run(self, val):
|
||||
if val and self.valid_re.match(val):
|
||||
return self.splitter.split(val)
|
||||
|
||||
class VLength(Validator):
|
||||
def __init__(self, item, length = 10000,
|
||||
empty_error = errors.BAD_COMMENT,
|
||||
@@ -362,21 +348,36 @@ class VAccountByName(VRequired):
|
||||
except NotFound: pass
|
||||
return self.error()
|
||||
|
||||
class VByName(VRequired):
|
||||
fullname_re = re.compile(r'^' + Thing._type_prefix +
|
||||
r'[0-9a-z]+_[0-9a-z]+$')
|
||||
class VByName(Validator):
|
||||
splitter = re.compile('[ ,]+')
|
||||
def __init__(self, param, thing_cls = None, multiple = False,
|
||||
error = errors.NO_THING_ID, **kw):
|
||||
pattern = Thing._type_prefix
|
||||
if thing_cls:
|
||||
pattern += utils.to36(thing_cls._type_id)
|
||||
else:
|
||||
pattern += r"[0-9a-z]+"
|
||||
pattern += r"_[0-9a-z]+"
|
||||
if multiple:
|
||||
pattern = r"(%s *,? *)+" % pattern
|
||||
self.re = re.compile(r"^" + pattern + r"$")
|
||||
self.multiple = multiple
|
||||
self._error = error
|
||||
|
||||
Validator.__init__(self, param, **kw)
|
||||
|
||||
def __init__(self, param,
|
||||
error = errors.NO_THING_ID, *a, **kw):
|
||||
VRequired.__init__(self, param, error, *a, **kw)
|
||||
|
||||
def run(self, fullname):
|
||||
if fullname and self.fullname_re.match(fullname):
|
||||
def run(self, items):
|
||||
if items and self.re.match(items):
|
||||
if self.multiple:
|
||||
items = self.splitter.split(items)
|
||||
try:
|
||||
return Thing._by_fullname(fullname, False, data=True)
|
||||
return Thing._by_fullname(items, return_dict = False,
|
||||
data=True)
|
||||
except NotFound:
|
||||
pass
|
||||
return self.error()
|
||||
return self.set_error(self._error)
|
||||
|
||||
|
||||
|
||||
class VByNameIfAuthor(VByName):
|
||||
def run(self, fullname):
|
||||
@@ -428,12 +429,12 @@ class VSrModerator(Validator):
|
||||
or c.user_is_admin):
|
||||
abort(403, "forbidden")
|
||||
|
||||
class VSrCanBan(Validator):
|
||||
class VSrCanBan(VByName):
|
||||
def run(self, thing_name):
|
||||
if c.user_is_admin:
|
||||
return True
|
||||
elif c.user_is_loggedin:
|
||||
item = Thing._by_fullname(thing_name,data=True)
|
||||
item = VByName.run(self, thing_name)
|
||||
# will throw a legitimate 500 if this isn't a link or
|
||||
# comment, because this should only be used on links and
|
||||
# comments
|
||||
@@ -442,12 +443,12 @@ class VSrCanBan(Validator):
|
||||
return True
|
||||
abort(403,'forbidden')
|
||||
|
||||
class VSrSpecial(Validator):
|
||||
class VSrSpecial(VByName):
|
||||
def run(self, thing_name):
|
||||
if c.user_is_admin:
|
||||
return True
|
||||
elif c.user_is_loggedin:
|
||||
item = Thing._by_fullname(thing_name,data=True)
|
||||
item = VByName.run(self, thing_name)
|
||||
# will throw a legitimate 500 if this isn't a link or
|
||||
# comment, because this should only be used on links and
|
||||
# comments
|
||||
@@ -462,10 +463,10 @@ class VSRSubmitPage(Validator):
|
||||
c.site.can_submit(c.user)):
|
||||
abort(403, "forbidden")
|
||||
|
||||
class VSubmitParent(Validator):
|
||||
class VSubmitParent(VByName):
|
||||
def run(self, fullname):
|
||||
if fullname:
|
||||
parent = Thing._by_fullname(fullname, False, data=True)
|
||||
parent = VByName.run(self, fullname)
|
||||
if parent and parent._deleted:
|
||||
self.set_error(errors.DELETED_COMMENT)
|
||||
if isinstance(parent, Message):
|
||||
@@ -706,18 +707,6 @@ class VCommentIDs(Validator):
|
||||
comments = Comment._byID(cids, data=True, return_dict = False)
|
||||
return comments
|
||||
|
||||
class VFullNames(Validator):
|
||||
#id_str is a comma separated list of id36's
|
||||
def run(self, id_str):
|
||||
tids = id_str.split(',')
|
||||
return Thing._by_fullname(tids, data=True, return_dict = False)
|
||||
|
||||
class VSubreddits(Validator):
|
||||
#the subreddits are just in the post, this is for the my.reddit pref page
|
||||
def run(self):
|
||||
subreddits = Subreddit._by_fullname(request.post.keys())
|
||||
return subreddits.values()
|
||||
|
||||
class VCacheKey(Validator):
|
||||
def __init__(self, cache_prefix, param, *a, **kw):
|
||||
self.cache_prefix = cache_prefix
|
||||
|
||||
Reference in New Issue
Block a user