Extract string-to-bool logic into utils.py

This commit is contained in:
Simon Willison 2018-06-16 09:44:31 -07:00
commit 7e0caa1e62
No known key found for this signature in database
GPG key ID: 17E2DEA2588B7F52
2 changed files with 25 additions and 5 deletions

View file

@ -800,3 +800,13 @@ def path_with_format(request, format, extra_qs=None):
elif request.query_string:
path = "{}?{}".format(path, request.query_string)
return path
def value_as_boolean(value):
if value.lower() not in ('on', 'off', 'true', 'false', '1', '0'):
raise ValueAsBooleanError
return value.lower() in ('on', 'true', '1')
class ValueAsBooleanError(ValueError):
pass