From bec3fff190ae4a67a3e56fa9eaad027bfc990acb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Sun, 2 Jun 2024 11:38:06 +0200 Subject: [PATCH] Handle invalid email in trytond-admin command The email value is validated by the command line parser. And the interactive input loop until a valid email is entered. Closes #13294 --- trytond/trytond/admin.py | 13 +++++++++++-- trytond/trytond/commandline.py | 6 ++++-- trytond/trytond/tools/email_.py | 2 +- 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/trytond/trytond/admin.py b/trytond/trytond/admin.py index 862fffa606..db0b09e3fc 100644 --- a/trytond/trytond/admin.py +++ b/trytond/trytond/admin.py @@ -13,6 +13,7 @@ from trytond.config import config from trytond.pool import Pool from trytond.sendmail import send_test_email from trytond.tools import file_open +from trytond.tools.email_ import EmailNotValidError, validate_email from trytond.transaction import Transaction, TransactionError, inactive_records __all__ = ['run'] @@ -79,8 +80,16 @@ def run(options): if options.email is not None: email = options.email elif init[db_name]: - email = input( - '"admin" email for "%s": ' % db_name) + while True: + email = input( + '"admin" email for "%s" (empty for none): ' % db_name) + if email: + try: + validate_email(email) + except EmailNotValidError as e: + sys.stderr.write(str(e) + '\n') + continue + break else: email = None diff --git a/trytond/trytond/commandline.py b/trytond/trytond/commandline.py index 1c5f5eac77..5990077f91 100644 --- a/trytond/trytond/commandline.py +++ b/trytond/trytond/commandline.py @@ -101,6 +101,7 @@ def get_parser_cron(): def get_parser_admin(): + from trytond.tools.email_ import validate_email parser = get_parser() parser.add_argument( @@ -116,12 +117,13 @@ def get_parser_admin(): parser.add_argument("--activate-dependencies", dest="activatedeps", action="store_true", help="activate missing dependencies of updated modules") - parser.add_argument("--email", dest="email", help="set the admin email") + parser.add_argument("--email", dest="email", type=validate_email, + help="set the admin email") parser.add_argument("-p", "--password", dest="password", action='store_true', help="set the admin password") parser.add_argument("--reset-password", dest='reset_password', action='store_true', help="reset the admin password") - parser.add_argument("--test-email", dest='test_email', + parser.add_argument("--test-email", dest='test_email', type=validate_email, help="send a test email to the specified address") parser.add_argument("-m", "--update-modules-list", action="store_true", dest="update_modules_list", help="update the list of tryton modules") diff --git a/trytond/trytond/tools/email_.py b/trytond/trytond/tools/email_.py index 327b912c1a..05f7eed6b9 100644 --- a/trytond/trytond/tools/email_.py +++ b/trytond/trytond/tools/email_.py @@ -85,7 +85,7 @@ except ImportError: def convert_ascii_email(email): return email - class EmailNotValidError(Exception): + class EmailNotValidError(ValueError): pass