Feat: question prompts

This commit is contained in:
Bertrand Benjamin 2020-10-04 18:10:43 +02:00
parent 5e0f2d92ef
commit 1886deb430
2 changed files with 133 additions and 11 deletions

View File

@ -9,13 +9,39 @@ from functools import wraps
from .getconfig import config from .getconfig import config
class CancelError(Exception):
pass
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":
ans = func(*args, **ans, **kwrd)
if confirm == "Annuler cet élément":
raise CancelError
return ans
return wrapper
def prompt_until_validate(func): def prompt_until_validate(func):
@wraps(func) @wraps(func)
def wrapper(*args, **kwrd): def wrapper(*args, **kwrd):
ans = func(*args, **kwrd) ans = func(*args, **kwrd)
question = [{"type": "confirm", "name": "confirm", "message": "C'est bon?"}] question = [{"type": "confirm", "name": "confirm", "message": "C'est bon?"}]
while not prompt(question)["confirm"]: while not prompt(question)["confirm"]:
ans = func(ans) ans = func(*args, **ans, **kwrd)
return ans return ans
return wrapper return wrapper
@ -25,6 +51,12 @@ def prompt_until_validate(func):
def prompt_exam(**kwrd): def prompt_exam(**kwrd):
""" Prompt questions to edit an exam """ """ Prompt questions to edit an exam """
questions = [ questions = [
{
"message": "Nom de l'évaluation",
"type": "input",
"name": "name",
"default": kwrd.get("name", "DS"),
},
{ {
"message": "Nom de la classe", "message": "Nom de la classe",
"type": "list", "type": "list",
@ -32,12 +64,6 @@ def prompt_exam(**kwrd):
"choices": [t["name"] for t in config["tribes"]], "choices": [t["name"] for t in config["tribes"]],
"default": kwrd.get("tribe", ""), "default": kwrd.get("tribe", ""),
}, },
{
"message": "Type d'évaluation",
"type": "input",
"name": "type",
"default": kwrd.get("type", "DS"),
},
{ {
"message": "Date (%y%m%d)", "message": "Date (%y%m%d)",
"type": "input", "type": "input",
@ -45,6 +71,13 @@ def prompt_exam(**kwrd):
"validate": lambda x: (len(x) == 6) and x.isdigit(), "validate": lambda x: (len(x) == 6) and x.isdigit(),
"default": kwrd.get("_date", datetime.today().strftime("%y%m%d")), "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", "message": "Nombre d'exercice",
"type": "input", "type": "input",
@ -53,6 +86,7 @@ def prompt_exam(**kwrd):
"default": kwrd.get("ExQty", "1"), "default": kwrd.get("ExQty", "1"),
}, },
] ]
print("Nouvelle évaluation")
exam = prompt(questions) exam = prompt(questions)
exam["tribe"] = [t for t in config["tribes"] if exam["_tribe"] == t["name"]][0] exam["tribe"] = [t for t in config["tribes"] if exam["_tribe"] == t["name"]][0]
exam["date"] = datetime.strptime(exam["_date"], "%y%m%d") exam["date"] = datetime.strptime(exam["_date"], "%y%m%d")
@ -62,6 +96,13 @@ def prompt_exam(**kwrd):
@prompt_until_validate @prompt_until_validate
def prompt_exercise(number=1, **kwrd): def prompt_exercise(number=1, **kwrd):
try:
kwrd["name"]
except KeyError:
print("Nouvel exercice")
else:
print(f"Modification de la questions {kwrd['name']}")
questions = [ questions = [
{ {
"message": "Nom de l'exercice", "message": "Nom de l'exercice",
@ -73,6 +114,18 @@ def prompt_exercise(number=1, **kwrd):
exercise = prompt(questions) exercise = prompt(questions)
exercise["questions"] = [] exercise["questions"] = []
try:
kwrd["questions"]
except KeyError:
last_question_id = "1a"
else:
for ques in kwrd["questions"]:
try:
exercise["questions"].append(prompt_question())
except CancelError:
print("Cette question a été supprimée")
last_question_id = exercise["questions"][-1]["id"]
append = [ append = [
{ {
"message": "Ajouter un élément de notation", "message": "Ajouter un élément de notation",
@ -81,10 +134,62 @@ def prompt_exercise(number=1, **kwrd):
} }
] ]
while prompt(append)["append"]: while prompt(append)["append"]:
exercise["questions"].append(prompt_question()) try:
exercise["questions"].append(prompt_question(last_question_id))
except CancelError:
print("Cette question a été supprimée")
else:
last_question_id = exercise["questions"][-1]["id"]
return exercise return exercise
def prompt_question(): @prompt_until_validate_or_cancel
pass def prompt_question(last_question_id="1a", **kwrd):
try:
kwrd["id"]
except KeyError:
print("Nouvelle question")
else:
print(f"Modification de la questions {kwrd['id']}")
questions = [
{
"message": "Identifiant de la question",
"type": "input",
"name": "id",
"default": kwrd.get("id", "1a"),
},
{
"message": "Competence",
"type": "input",
"name": "competence",
"default": kwrd.get("competence", "Cal"),
},
{
"message": "Domaine",
"type": "input",
"name": "theme",
"default": kwrd.get("domain", ""),
},
{
"message": "Commentaire",
"type": "input",
"name": "comment",
"default": kwrd.get("comment", ""),
},
{
"message": "Évaluation par niveau",
"type": "confirm",
"name": "is_leveled",
"default": kwrd.get("is_leveled", True),
},
{
"message": "Bareme",
"type": "input",
"name": "score_rate",
"default": kwrd.get("score_rate", "1"),
"validate": lambda x: x.isdigit(),
},
]
return prompt(questions)

View File

@ -32,6 +32,23 @@ def setup():
print(f"The file {tribe['students']} does not exists") print(f"The file {tribe['students']} does not exists")
def exam_dict2element(exam):
""" Transform an exam in dictionnary for into list of element to evaluate"""
elements = []
for ex in exam["exercices"]:
for q in ex["questions"]:
elements.append(
{
"term": exam["term"],
"assessment": exam["name"],
"date": exam["date"].strftime("%d/%m/%Y"),
"exercice": ex["name"],
**q,
}
)
return elements
@cli.command() @cli.command()
def new_exam(): def new_exam():
""" Create new exam csv file """ """ Create new exam csv file """
@ -39,7 +56,7 @@ def new_exam():
exam["exercices"] = [] exam["exercices"] = []
for ex in range(int(exam["ExQty"])): for ex in range(int(exam["ExQty"])):
exam["exercices"].append(prompt_exercise(ex + 1)) exam["exercices"].append(prompt_exercise(ex + 1))
print(exam) print(exam_dict2element(exam))
@cli.command() @cli.command()