From b63081cb48969603ad572f4960b79fd6251c79be Mon Sep 17 00:00:00 2001 From: Alexander Graf Date: Mon, 13 Sep 2021 14:49:49 +0200 Subject: [PATCH 1/2] display error (not exception) when creating admin repleace misleading python exception (mailu broken) with error message stating that the admin user is already present --- core/admin/mailu/manage.py | 37 +++++++++++++++++++------------------ 1 file changed, 19 insertions(+), 18 deletions(-) diff --git a/core/admin/mailu/manage.py b/core/admin/mailu/manage.py index 5708327e..d83efa9d 100644 --- a/core/admin/mailu/manage.py +++ b/core/admin/mailu/manage.py @@ -48,44 +48,45 @@ def advertise(): @click.argument('localpart') @click.argument('domain_name') @click.argument('password') -@click.option('-m', '--mode') +@click.option('-m', '--mode', default='create') @with_appcontext -def admin(localpart, domain_name, password, mode='create'): +def admin(localpart, domain_name, password, mode): """ Create an admin user 'mode' can be: - - 'create' (default) Will try to create user and will raise an exception if present + - 'create': (default) create user. it's an error if user already exists - 'ifmissing': if user exists, nothing happens, else it will be created - 'update': user is created or, if it exists, its password gets updated """ + + if not mode in ('create', 'update', 'ifmissing'): + raise click.ClickException(f'invalid mode: {mode!r}') + domain = models.Domain.query.get(domain_name) if not domain: domain = models.Domain(name=domain_name) db.session.add(domain) - user = None - if mode == 'ifmissing' or mode == 'update': - email = f'{localpart}@{domain_name}' - user = models.User.query.get(email) - - if user and mode == 'ifmissing': - print('user %s exists, not updating' % email) + email = f'{localpart}@{domain_name}' + if user := models.User.query.get(email): + if mode == 'ifmissing': + print(f'user {email!r} exists, not updating') return - - if not user: + elif mode == 'update': + user.set_password(password) + db.session.commit() + print("updated admin password") + else: + raise click.ClickException(f'user {email!r} exists, not created') + else: user = models.User( localpart=localpart, domain=domain, global_admin=True ) - user.set_password(password) db.session.add(user) + user.set_password(password) db.session.commit() print("created admin user") - elif mode == 'update': - user.set_password(password) - db.session.commit() - print("updated admin password") - @mailu.command() From 25cf8b53589a746e8ad45757dcb747c4ca7215ee Mon Sep 17 00:00:00 2001 From: Alexander Graf Date: Mon, 13 Sep 2021 15:13:29 +0200 Subject: [PATCH 2/2] better help formatting --- core/admin/mailu/manage.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/core/admin/mailu/manage.py b/core/admin/mailu/manage.py index d83efa9d..54f4b826 100644 --- a/core/admin/mailu/manage.py +++ b/core/admin/mailu/manage.py @@ -48,14 +48,13 @@ def advertise(): @click.argument('localpart') @click.argument('domain_name') @click.argument('password') -@click.option('-m', '--mode', default='create') +@click.option('-m', '--mode', default='create', metavar='MODE', help='''\b'create' (default): create user. it's an error if user already exists +'ifmissing': only update password if user is missing +'update': create user or update password if user exists +''') @with_appcontext def admin(localpart, domain_name, password, mode): """ Create an admin user - 'mode' can be: - - 'create': (default) create user. it's an error if user already exists - - 'ifmissing': if user exists, nothing happens, else it will be created - - 'update': user is created or, if it exists, its password gets updated """ if not mode in ('create', 'update', 'ifmissing'):