Merge pull request 'Ask for confirmation before creating a new category' (#100) from isti/linkulator2:master into master

Reviewed-on: #100
This commit is contained in:
asdf 2021-07-18 09:54:15 +00:00
commit 65c9a070ad
1 changed files with 10 additions and 0 deletions

View File

@ -273,6 +273,14 @@ def is_valid_input(entry: str) -> bool:
return False
return True
def is_correct_category(entry: str) -> bool:
"""Make sure the user purposefully created a new category and not by
accident (mistyped, tried to use category number instead of name)"""
if entry not in [ record["name"] for record in categories ]:
question = "Do you want to create a new category '{}'? Y/[N]".format(entry)
answer = input(question).lower().strip()
return answer != "" and answer[0] == "y"
return True
def get_input(item: str) -> str:
"""Get user input with the specified prompt, validate and return it, or
@ -312,6 +320,8 @@ def post_link() -> int:
try:
url = get_input("URL")
category = get_input("Category")
while not is_correct_category(category):
category = get_input("Category")
title = get_input("Title")
except ValueError:
print("Post cancelled")