Move truthystring to stringtools.
This commit is contained in:
parent
fc8bcb9502
commit
0c51201901
6 changed files with 19 additions and 47 deletions
|
@ -226,9 +226,6 @@ You can\'t order "{column}" by "{direction}". Defaulting to descending.
|
|||
# Janitorial stuff #################################################################################
|
||||
|
||||
FILENAME_BADCHARS = '\\/:*?<>|"'
|
||||
TRUTHYSTRING_TRUE = {s.lower() for s in ('1', 'true', 't', 'yes', 'y', 'on')}
|
||||
TRUTHYSTRING_FALSE = {s.lower() for s in ('0', 'false', 'f', 'no', 'n', 'off')}
|
||||
TRUTHYSTRING_NONE = {s.lower() for s in ('null', 'none')}
|
||||
|
||||
USER_ID_CHARACTERS = string.digits + string.ascii_uppercase
|
||||
|
||||
|
|
|
@ -459,32 +459,6 @@ def split_easybake_string(ebstring) -> tuple[str, str, str]:
|
|||
tagname = tagname.strip('.')
|
||||
return (tagname, synonym, rename_to)
|
||||
|
||||
def truthystring(s, fallback=False) -> typing.Union[bool, None]:
|
||||
'''
|
||||
If s is already a boolean, int, or None, return a boolean or None.
|
||||
If s is a string, return True, False, or None based on the options presented
|
||||
in constants.TRUTHYSTRING_TRUE, TRUTHYSTRING_FALSE, TRUTHYSTRING_NONE where
|
||||
s is treated case-insensitively.
|
||||
If s is not in any of those sets, return the fallback.
|
||||
'''
|
||||
if s is None:
|
||||
return None
|
||||
|
||||
if isinstance(s, (bool, int)):
|
||||
return bool(s)
|
||||
|
||||
if not isinstance(s, str):
|
||||
return fallback
|
||||
|
||||
s = s.lower()
|
||||
if s in constants.TRUTHYSTRING_TRUE:
|
||||
return True
|
||||
if s in constants.TRUTHYSTRING_FALSE:
|
||||
return False
|
||||
if s in constants.TRUTHYSTRING_NONE:
|
||||
return None
|
||||
return False
|
||||
|
||||
def zip_album(album, recursive=True) -> zipstream.ZipFile:
|
||||
'''
|
||||
Given an album, return a zipstream zipfile that contains the album's
|
||||
|
|
|
@ -193,21 +193,21 @@ def normalize_filename(filename_terms):
|
|||
|
||||
def normalize_has_tags(has_tags):
|
||||
'''
|
||||
See etiquette.helpers.truthystring.
|
||||
See voussoirkit.stringtools.truthystring.
|
||||
'''
|
||||
return helpers.truthystring(has_tags)
|
||||
return stringtools.truthystring(has_tags, None)
|
||||
|
||||
def normalize_has_thumbnail(has_thumbnail):
|
||||
'''
|
||||
See etiquette.helpers.truthystring.
|
||||
See voussoirkit.stringtools.truthystring.
|
||||
'''
|
||||
return helpers.truthystring(has_thumbnail)
|
||||
return stringtools.truthystring(has_thumbnail, None)
|
||||
|
||||
def normalize_is_searchhidden(is_searchhidden):
|
||||
'''
|
||||
See etiquette.helpers.truthystring.
|
||||
See voussoirkit.stringtools.truthystring.
|
||||
'''
|
||||
return helpers.truthystring(is_searchhidden)
|
||||
return stringtools.truthystring(is_searchhidden, None)
|
||||
|
||||
def _limit_offset(number, warning_bag):
|
||||
if number is None:
|
||||
|
@ -447,15 +447,15 @@ def normalize_within_directory(paths, warning_bag=None):
|
|||
|
||||
def normalize_yield_albums(yield_albums):
|
||||
'''
|
||||
See etiquette.helpers.truthystring.
|
||||
See voussoirkit.stringtools.truthystring.
|
||||
'''
|
||||
return helpers.truthystring(yield_albums)
|
||||
return stringtools.truthystring(yield_albums, None)
|
||||
|
||||
def normalize_yield_photos(yield_photos):
|
||||
'''
|
||||
See etiquette.helpers.truthystring.
|
||||
See voussoirkit.stringtools.truthystring.
|
||||
'''
|
||||
return helpers.truthystring(yield_photos)
|
||||
return stringtools.truthystring(yield_photos, None)
|
||||
|
||||
EXIST_FORMAT = '''
|
||||
{operator} (
|
||||
|
|
|
@ -37,7 +37,7 @@ def get_album_zip(album_id):
|
|||
album = common.P_album(album_id, response_type='html')
|
||||
|
||||
recursive = request.args.get('recursive', True)
|
||||
recursive = etiquette.helpers.truthystring(recursive)
|
||||
recursive = stringtools.truthystring(recursive)
|
||||
|
||||
streamed_zip = etiquette.helpers.zip_album(album, recursive=recursive)
|
||||
|
||||
|
@ -152,7 +152,7 @@ def post_album_add_tag(album_id):
|
|||
response = exc.jsonify()
|
||||
return flasktools.make_json_response(response, status=404)
|
||||
recursive = request.form.get('recursive', False)
|
||||
recursive = etiquette.helpers.truthystring(recursive)
|
||||
recursive = stringtools.truthystring(recursive)
|
||||
album.add_tag_to_all(tag, nested_children=recursive, commit=True)
|
||||
response['action'] = 'add_tag'
|
||||
response['tagname'] = tag.name
|
||||
|
|
|
@ -37,10 +37,10 @@ def get_file(photo_id, basename=None):
|
|||
photo = common.P.get_photo(photo_id)
|
||||
|
||||
do_download = request.args.get('download', False)
|
||||
do_download = etiquette.helpers.truthystring(do_download)
|
||||
do_download = stringtools.truthystring(do_download)
|
||||
|
||||
use_original_filename = request.args.get('original_filename', False)
|
||||
use_original_filename = etiquette.helpers.truthystring(use_original_filename)
|
||||
use_original_filename = stringtools.truthystring(use_original_filename)
|
||||
|
||||
if do_download:
|
||||
if use_original_filename:
|
||||
|
@ -73,7 +73,7 @@ def post_photo_delete(photo_id):
|
|||
print(photo_id)
|
||||
photo = common.P_photo(photo_id, response_type='json')
|
||||
delete_file = request.form.get('delete_file', False)
|
||||
delete_file = etiquette.helpers.truthystring(delete_file)
|
||||
delete_file = stringtools.truthystring(delete_file)
|
||||
photo.delete(delete_file=delete_file, commit=True)
|
||||
return flasktools.make_json_response({})
|
||||
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
import flask; from flask import request
|
||||
|
||||
from voussoirkit import flasktools
|
||||
from voussoirkit import stringtools
|
||||
|
||||
import etiquette
|
||||
|
||||
|
@ -36,7 +37,7 @@ def get_tag_json(specific_tag_name):
|
|||
return flask.redirect(new_url)
|
||||
|
||||
include_synonyms = request.args.get('synonyms')
|
||||
include_synonyms = include_synonyms is None or etiquette.helpers.truthystring(include_synonyms)
|
||||
include_synonyms = include_synonyms is None or stringtools.truthystring(include_synonyms)
|
||||
|
||||
response = specific_tag.jsonify(include_synonyms=include_synonyms)
|
||||
return flasktools.make_json_response(response)
|
||||
|
@ -117,7 +118,7 @@ def get_tags_html(specific_tag_name=None):
|
|||
return flask.redirect(new_url)
|
||||
|
||||
include_synonyms = request.args.get('include_synonyms')
|
||||
include_synonyms = include_synonyms is None or etiquette.helpers.truthystring(include_synonyms)
|
||||
include_synonyms = include_synonyms is None or stringtools.truthystring(include_synonyms)
|
||||
|
||||
if specific_tag is None:
|
||||
tags = common.P.get_root_tags()
|
||||
|
@ -147,7 +148,7 @@ def get_tags_html(specific_tag_name=None):
|
|||
@site.route('/tags.json')
|
||||
def get_tags_json():
|
||||
include_synonyms = request.args.get('synonyms')
|
||||
include_synonyms = include_synonyms is None or etiquette.helpers.truthystring(include_synonyms)
|
||||
include_synonyms = include_synonyms is None or stringtools.truthystring(include_synonyms)
|
||||
|
||||
tags = list(common.P.get_tags())
|
||||
response = [tag.jsonify(include_synonyms=include_synonyms) for tag in tags]
|
||||
|
|
Loading…
Reference in a new issue