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
This commit is contained in:
Cédric Krier
2024-06-02 11:38:06 +02:00
parent d011e4149e
commit bec3fff190
3 changed files with 16 additions and 5 deletions
+11 -2
View File
@@ -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
+4 -2
View File
@@ -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")
+1 -1
View File
@@ -85,7 +85,7 @@ except ImportError:
def convert_ascii_email(email):
return email
class EmailNotValidError(Exception):
class EmailNotValidError(ValueError):
pass