Ticket #969: issue_696.patch

File issue_696.patch, 3.1 KB (added by Ben Sturmfels, 9 years ago)
  • mediagoblin/gmg_commands/users.py

    From 05f86a0add370200f9caca0fbb47db69129853ce Mon Sep 17 00:00:00 2001
    From: Ben Sturmfels <ben@sturm.com.au>
    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 0916308..4f8a9f7 100644
    a b def adduser(args):  
    7474        entry.all_privileges = default_privileges
    7575        entry.save()
    7676
    77         print(u"User created (and email marked as verified)")
     77        print(u"User created (and email marked as verified).")
    7878
    7979
    8080def makeadmin_parser_setup(subparser):
    8181    subparser.add_argument(
    8282        'username',
    83         help="Username to give admin level")
     83        help="Username to give admin level",
     84        type=six.text_type)
    8485
    8586
    8687def makeadmin(args):
    def makeadmin(args):  
    8990    db = mg_globals.database
    9091
    9192    user = db.User.query.filter(
    92         LocalUser.username==six.text_type(args.username.lower())
    93     ).one()
     93        LocalUser.username==args.username.lower()
     94    ).first()
    9495    if user:
    9596        user.all_privileges.append(
    9697            db.Privilege.query.filter(
    9798                db.Privilege.privilege_name==u'admin').one()
    9899        )
    99100        user.save()
    100         print(u'The user is now Admin')
     101        print(u'The user %s is now an admin.' % args.username)
    101102    else:
    102         print(u'The user doesn\'t exist')
     103        print(u'The user %s doesn\'t exist.' % args.username)
    103104        sys.exit(1)
    104105
    105106
    106107def changepw_parser_setup(subparser):
    107108    subparser.add_argument(
    108109        'username',
    109         help="Username used to login")
     110        help="Username used to login",
     111        type=six.text_type)
    110112    subparser.add_argument(
    111113        'password',
    112114        help="Your NEW supersecret word to login")
    def changepw(args):  
    118120    db = mg_globals.database
    119121
    120122    user = db.User.query.filter(
    121         LocalUser.username==six.text_type(args.username.lower())
    122     ).one()
     123        LocalUser.username==args.username.lower()
     124    ).first()
    123125    if user:
    124126        user.pw_hash = auth.gen_password_hash(args.password)
    125127        user.save()
    126         print(u'Password successfully changed')
     128        print(u'Password successfully changed for user %s.' % args.username)
    127129    else:
    128         print(u'The user doesn\'t exist')
     130        print(u'The user %s doesn\'t exist.' % args.username)
    129131        sys.exit(1)
    130132
    131133
    def deleteuser(args):  
    146148    ).first()
    147149    if user:
    148150        user.delete()
    149         print('The user %s has been deleted' % args.username)
     151        print('The user %s has been deleted.' % args.username)
    150152    else:
    151         print('The user %s doesn\'t exist' % args.username)
     153        print('The user %s doesn\'t exist.' % args.username)
    152154        sys.exit(1)