From be2621a43c44b385a72b4d9905b4dc11435afe67 Mon Sep 17 00:00:00 2001 From: Ben Sturmfels Date: Sun, 23 Aug 2015 20:37:15 +1000 Subject: [PATCH] Prevent unhandled exception on non-existant user. These changes bring `makeadmin` and `changepw` in-line with the approach used in `deleteuser`. I've also made the error messages more consistent: list the username and full sentences. --- mediagoblin/gmg_commands/users.py | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/mediagoblin/gmg_commands/users.py b/mediagoblin/gmg_commands/users.py index 54c4ec94..d1a8b72d 100644 --- a/mediagoblin/gmg_commands/users.py +++ b/mediagoblin/gmg_commands/users.py @@ -74,13 +74,14 @@ def adduser(args): entry.all_privileges = default_privileges entry.save() - print(u"User created (and email marked as verified)") + print(u"User created (and email marked as verified).") def makeadmin_parser_setup(subparser): subparser.add_argument( 'username', - help="Username to give admin level") + help="Username to give admin level", + type=six.text_type) def makeadmin(args): @@ -89,24 +90,25 @@ def makeadmin(args): db = mg_globals.database user = db.LocalUser.query.filter( - LocalUser.username==six.text_type(args.username.lower()) - ).one() + LocalUser.username==args.username.lower() + ).first() if user: user.all_privileges.append( db.Privilege.query.filter( db.Privilege.privilege_name==u'admin').one() ) user.save() - print(u'The user is now Admin') + print(u'The user %s is now an admin.' % args.username) else: - print(u'The user doesn\'t exist') + print(u'The user %s doesn\'t exist.' % args.username) sys.exit(1) def changepw_parser_setup(subparser): subparser.add_argument( 'username', - help="Username used to login") + help="Username used to login", + type=six.text_type) subparser.add_argument( 'password', help="Your NEW supersecret word to login") @@ -118,14 +120,14 @@ def changepw(args): db = mg_globals.database user = db.LocalUser.query.filter( - LocalUser.username==six.text_type(args.username.lower()) - ).one() + LocalUser.username==args.username.lower() + ).first() if user: user.pw_hash = auth.gen_password_hash(args.password) user.save() - print(u'Password successfully changed') + print(u'Password successfully changed for user %s.' % args.username) else: - print(u'The user doesn\'t exist') + print(u'The user %s doesn\'t exist.' % args.username) sys.exit(1) @@ -146,7 +148,7 @@ def deleteuser(args): ).first() if user: user.delete() - print('The user %s has been deleted' % args.username) + print('The user %s has been deleted.' % args.username) else: - print('The user %s doesn\'t exist' % args.username) + print('The user %s doesn\'t exist.' % args.username) sys.exit(1) -- 2.25.1