Combine normalize_limit, _offset to normalize_positive_integer.

This commit is contained in:
voussoir 2018-03-21 11:25:21 -07:00
parent 1d76b4511b
commit afc5f38a8d

View file

@ -283,48 +283,10 @@ def normalize_is_searchhidden(is_searchhidden):
return helpers.truthystring(is_searchhidden) return helpers.truthystring(is_searchhidden)
def normalize_limit(limit, warning_bag=None): def normalize_limit(limit, warning_bag=None):
if not limit and limit != 0: return normalize_positive_integer(limit, warning_bag)
return None
if isinstance(limit, str):
limit = limit.strip()
if limit.isdigit():
limit = int(limit)
if isinstance(limit, float):
limit = int(limit)
if not isinstance(limit, int):
message = 'Invalid limit "%s"' % limit
if warning_bag:
warning_bag.add(message)
limit = None
else:
raise ValueError(message)
return limit
def normalize_offset(offset, warning_bag=None): def normalize_offset(offset, warning_bag=None):
if not offset: return normalize_positive_integer(limit, warning_bag)
return None
if isinstance(offset, str):
offset = offset.strip()
if offset.isdigit():
offset = int(offset)
if isinstance(offset, float):
offset = int(offset)
if not isinstance(offset, int):
message = 'Invalid offset "%s"' % offset
if warning_bag:
warning_bag.add(message)
offset = None
else:
raise ValueError(message)
return offset
def normalize_orderby(orderby, warning_bag=None): def normalize_orderby(orderby, warning_bag=None):
if not orderby: if not orderby:
@ -386,6 +348,36 @@ def normalize_orderby(orderby, warning_bag=None):
return final_orderby return final_orderby
def normalize_positive_integer(number, warning_bag=None):
if not number:
number = 0
elif isinstance(number, str):
number = number.strip()
try:
number = int(number)
except ValueError as exc:
if warning_bag:
warning_bag.add(exc)
else:
raise
elif isinstance(number, float):
number = int(number)
if not isinstance(number, int):
message = 'Invalid number "%s"' % number
if warning_bag:
warning_bag.add(message)
number = None
else:
raise ValueError(message)
if number < 0:
raise ValueError('Invalid number %d' % number)
return number
def normalize_tag_expression(expression): def normalize_tag_expression(expression):
if not expression: if not expression:
return None return None