idiomatic python

* make "process_fetched_list" private
* rename fetch_args to args
* a few logic simplifications
This commit is contained in:
Martin Zimmermann 2014-05-03 10:47:48 +02:00
parent 26ae30f76c
commit 59bfde7c03
2 changed files with 39 additions and 46 deletions

View File

@ -108,7 +108,7 @@ class Comments:
sql_args = [uri, mode, mode, after] sql_args = [uri, mode, mode, after]
if parent != 'any': if parent != 'any':
if parent is None or parent == 'NULL': if parent is None:
sql.append('AND comments.parent IS NULL') sql.append('AND comments.parent IS NULL')
else: else:
sql.append('AND comments.parent=?') sql.append('AND comments.parent=?')
@ -117,7 +117,7 @@ class Comments:
sql.append('ORDER BY ? ASC') sql.append('ORDER BY ? ASC')
sql_args.append(order_by) sql_args.append(order_by)
if limit != None and limit != 0: if limit:
sql.append('LIMIT ?') sql.append('LIMIT ?')
sql_args.append(limit) sql_args.append(limit)

View File

@ -320,62 +320,54 @@ class API(object):
@requires(str, 'uri') @requires(str, 'uri')
def fetch(self, environ, request, uri): def fetch(self, environ, request, uri):
fetch_args={'uri': uri} args = {
if request.args.get('after'): 'uri': uri,
fetch_args['after'] = request.args.get('after') 'after': request.args.get('after', 0)
after = request.args.get('after') }
else:
after = 0
if request.args.get('limit'):
try:
fetch_args['limit'] = int(request.args.get('limit'))
except ValueError:
return BadRequest("limit should be integer")
if request.args.get('parent'): try:
parent_arg = request.args.get('parent') args['limit'] = int(request.args.get('limit'))
if parent_arg == 'NULL': except TypeError:
fetch_args['parent'] = parent_arg args['limit'] = None
root_id = None except ValueError:
else: return BadRequest("limit should be integer")
try:
fetch_args['parent'] = int(parent_arg) if request.args.get('parent') is not None:
root_id = int(parent_arg) try:
except ValueError: args['parent'] = int(request.args.get('parent'))
return BadRequest("parent should be integer or NULL") root_id = args['parent']
except ValueError:
return BadRequest("parent should be integer")
else: else:
fetch_args['parent'] = 'NULL' args['parent'] = None
root_id = None root_id = None
if request.args.get('plain', '0') == '0': plain = request.args.get('plain', '0') == '0'
plain = True
else:
plain = False
reply_counts = self.comments.reply_count(uri, after) reply_counts = self.comments.reply_count(uri, args['after'])
if 'limit' in fetch_args and fetch_args['limit'] == 0: if args['limit'] == 0:
root_list = [] root_list = []
else: else:
root_list = list(self.comments.fetch(**fetch_args)) root_list = list(self.comments.fetch(**args))
if not root_list: if not root_list:
raise NotFound raise NotFound
if root_id not in reply_counts: if root_id not in reply_counts:
reply_counts[root_id] = 0 reply_counts[root_id] = 0
if request.args.get('nested_limit'): try:
try: nested_limit = int(request.args.get('nested_limit'))
nested_limit = int(request.args.get('nested_limit')) except TypeError:
except ValueError:
return BadRequest("nested_limit should be integer")
else:
nested_limit = None nested_limit = None
except ValueError:
return BadRequest("nested_limit should be integer")
rv = { rv = {
'id' : root_id, 'id' : root_id,
'total_replies' : reply_counts[root_id], 'total_replies' : reply_counts[root_id],
'hidden_replies' : reply_counts[root_id] - len(root_list), 'hidden_replies' : reply_counts[root_id] - len(root_list),
'replies' : self.process_fetched_list(root_list, plain) 'replies' : self._process_fetched_list(root_list, plain)
} }
# We are only checking for one level deep comments # We are only checking for one level deep comments
if root_id is None: if root_id is None:
@ -384,23 +376,24 @@ class API(object):
comment['total_replies'] = reply_counts[comment['id']] comment['total_replies'] = reply_counts[comment['id']]
if nested_limit is not None: if nested_limit is not None:
if nested_limit > 0: if nested_limit > 0:
fetch_args['parent'] = comment['id'] args['parent'] = comment['id']
fetch_args['limit'] = nested_limit args['limit'] = nested_limit
replies = list(self.comments.fetch(**fetch_args)) replies = list(self.comments.fetch(**args))
else: else:
replies = [] replies = []
else: else:
fetch_args['parent'] = comment['id'] args['parent'] = comment['id']
replies = list(self.comments.fetch(**fetch_args)) replies = list(self.comments.fetch(**args))
else: else:
comment['total_replies'] = 0 comment['total_replies'] = 0
replies = [] replies = []
comment['hidden_replies'] = comment['total_replies'] - len(replies) comment['hidden_replies'] = comment['total_replies'] - len(replies)
comment['replies'] = self.process_fetched_list(replies, plain) comment['replies'] = self._process_fetched_list(replies, plain)
return JSON(rv, 200) return JSON(rv, 200)
def process_fetched_list(self, fetched_list, plain=False): def _process_fetched_list(self, fetched_list, plain=False):
for item in fetched_list: for item in fetched_list:
key = item['email'] or item['remote_addr'] key = item['email'] or item['remote_addr']