Feat: rewrite new_exam prompt without Pyinquier

This commit is contained in:
Bertrand Benjamin 2020-12-31 18:00:42 +01:00
parent 77c358b0c1
commit 04a2506d86

View File

@ -2,12 +2,27 @@
# encoding: utf-8
from PyInquirer import prompt
from prompt_toolkit import prompt
from prompt_toolkit.validation import Validator
from prompt_toolkit.completion import WordCompleter
from unidecode import unidecode
from datetime import datetime
from functools import wraps
from .getconfig import config
VALIDATE = [
"o",
"ok",
"OK",
"oui",
"OUI",
"yes",
"YES",
]
REFUSE = ["n", "non", "NON", "no", "NO"]
CANCEL = ["a", "annuler"]
class CancelError(Exception):
pass
@ -17,18 +32,22 @@ def prompt_until_validate_or_cancel(func):
@wraps(func)
def wrapper(*args, **kwrd):
ans = func(*args, **kwrd)
question = [
{
"type": "list",
"name": "confirm",
"message": "C'est bon?",
"choices": ["Oui", "Non", "Annuler cet élément"],
}
]
confirm = prompt(question)["confirm"]
while confirm == "Non":
confirm = prompt(
"C'est ok? (a ou annuler pour sortir) ",
completer=WordCompleter(VALIDATE + REFUSE + CANCEL),
).lower()
if confirm in CANCEL:
raise CancelError
while confirm not in VALIDATE:
ans = func(*args, **ans, **kwrd)
if confirm == "Annuler cet élément":
confirm = prompt(
"C'est ok? (a ou annuler pour sortir) ",
completer=WordCompleter(VALIDATE + REFUSE + CANCEL),
).lower()
if confirm in ["a", "annuler"]:
raise CancelError
return ans
@ -39,9 +58,18 @@ def prompt_until_validate(func):
@wraps(func)
def wrapper(*args, **kwrd):
ans = func(*args, **kwrd)
question = [{"type": "confirm", "name": "confirm", "message": "C'est bon?"}]
while not prompt(question)["confirm"]:
confirm = prompt(
"C'est ok? ",
completer=WordCompleter(VALIDATE + REFUSE),
).lower()
while confirm not in VALIDATE:
ans = func(*args, **ans, **kwrd)
confirm = prompt(
"C'est ok? ",
completer=WordCompleter(VALIDATE + REFUSE),
).lower()
return ans
return wrapper
@ -50,46 +78,36 @@ def prompt_until_validate(func):
@prompt_until_validate
def prompt_exam(**kwrd):
""" Prompt questions to edit an exam """
questions = [
{
"message": "Nom de l'évaluation",
"type": "input",
"name": "name",
"default": kwrd.get("name", "DS"),
},
{
"message": "Nom de la classe",
"type": "list",
"name": "_tribe",
"choices": [t["name"] for t in config["tribes"]],
"default": kwrd.get("tribe", ""),
},
{
"message": "Date (%y%m%d)",
"type": "input",
"name": "_date",
"validate": lambda x: (len(x) == 6) and x.isdigit(),
"default": kwrd.get("_date", datetime.today().strftime("%y%m%d")),
},
{
"message": "Trimestre",
"type": "input",
"name": "term",
"validate": lambda x: x.isdigit(),
"default": kwrd.get("term", "1"),
},
{
"message": "Nombre d'exercice",
"type": "input",
"name": "ExQty",
"validate": lambda x: x.isdigit(),
"default": kwrd.get("ExQty", "1"),
},
]
print("Nouvelle évaluation")
exam = prompt(questions)
exam["tribe"] = [t for t in config["tribes"] if exam["_tribe"] == t["name"]][0]
exam["date"] = datetime.strptime(exam["_date"], "%y%m%d")
exam = {}
exam["name"] = prompt("Nom de l'évaluation: ", default=kwrd.get("name", "DS"))
tribes_name = [t["name"] for t in config["tribes"]]
exam["tribe"] = prompt(
"Nom de la classe: ",
default=kwrd.get("tribe", ""),
completer=WordCompleter(tribes_name),
validator=Validator.from_callable(lambda x: x in tribes_name),
)
exam["date"] = prompt(
"Date de l'évaluation (%y%m%d): ",
default=kwrd.get("date", datetime.today()).strftime("%y%m%d"),
validator=Validator.from_callable(lambda x: (len(x) == 6) and x.isdigit()),
)
exam["date"] = datetime.strptime(exam["date"], "%y%m%d")
exam["term"] = prompt(
"Trimestre: ",
validator=Validator.from_callable(lambda x: x.isdigit()),
default=kwrd.get("term", "1"),
)
exam["ExQty"] = prompt(
"Nombre d'exercices: ",
validator=Validator.from_callable(lambda x: x.isdigit()),
default=kwrd.get("ExQty", "1"),
)
return exam
@ -103,15 +121,11 @@ def prompt_exercise(number=1, **kwrd):
else:
print(f"Modification de la questions {kwrd['name']}")
questions = [
{
"message": "Nom de l'exercice",
"type": "input",
"name": "name",
"default": kwrd.get("name", f"Exercice {number}"),
}
]
exercise = prompt(questions)
exercise = {}
exercise["name"] = prompt(
"Nom de l'exercice", default=kwrd.get("name", f"Exercice {number}")
)
exercise["questions"] = []
try:
@ -126,13 +140,13 @@ def prompt_exercise(number=1, **kwrd):
print("Cette question a été supprimée")
last_question_id = exercise["questions"][-1]["id"]
append = [
{
"message": "Ajouter un élément de notation",
"type": "confirm",
"name": "append",
}
]
# append = prompt("Ajouter une élément de notation",
# {
# "message": "Ajouter un élément de notation",
# "type": "confirm",
# "name": "append",
#
# ]
while prompt(append)["append"]:
try:
exercise["questions"].append(prompt_question(last_question_id))