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):
|
74 | 74 | entry.all_privileges = default_privileges |
75 | 75 | entry.save() |
76 | 76 | |
77 | | print(u"User created (and email marked as verified)") |
| 77 | print(u"User created (and email marked as verified).") |
78 | 78 | |
79 | 79 | |
80 | 80 | def makeadmin_parser_setup(subparser): |
81 | 81 | subparser.add_argument( |
82 | 82 | 'username', |
83 | | help="Username to give admin level") |
| 83 | help="Username to give admin level", |
| 84 | type=six.text_type) |
84 | 85 | |
85 | 86 | |
86 | 87 | def makeadmin(args): |
… |
… |
def makeadmin(args):
|
89 | 90 | db = mg_globals.database |
90 | 91 | |
91 | 92 | user = db.User.query.filter( |
92 | | LocalUser.username==six.text_type(args.username.lower()) |
93 | | ).one() |
| 93 | LocalUser.username==args.username.lower() |
| 94 | ).first() |
94 | 95 | if user: |
95 | 96 | user.all_privileges.append( |
96 | 97 | db.Privilege.query.filter( |
97 | 98 | db.Privilege.privilege_name==u'admin').one() |
98 | 99 | ) |
99 | 100 | user.save() |
100 | | print(u'The user is now Admin') |
| 101 | print(u'The user %s is now an admin.' % args.username) |
101 | 102 | else: |
102 | | print(u'The user doesn\'t exist') |
| 103 | print(u'The user %s doesn\'t exist.' % args.username) |
103 | 104 | sys.exit(1) |
104 | 105 | |
105 | 106 | |
106 | 107 | def changepw_parser_setup(subparser): |
107 | 108 | subparser.add_argument( |
108 | 109 | 'username', |
109 | | help="Username used to login") |
| 110 | help="Username used to login", |
| 111 | type=six.text_type) |
110 | 112 | subparser.add_argument( |
111 | 113 | 'password', |
112 | 114 | help="Your NEW supersecret word to login") |
… |
… |
def changepw(args):
|
118 | 120 | db = mg_globals.database |
119 | 121 | |
120 | 122 | user = db.User.query.filter( |
121 | | LocalUser.username==six.text_type(args.username.lower()) |
122 | | ).one() |
| 123 | LocalUser.username==args.username.lower() |
| 124 | ).first() |
123 | 125 | if user: |
124 | 126 | user.pw_hash = auth.gen_password_hash(args.password) |
125 | 127 | user.save() |
126 | | print(u'Password successfully changed') |
| 128 | print(u'Password successfully changed for user %s.' % args.username) |
127 | 129 | else: |
128 | | print(u'The user doesn\'t exist') |
| 130 | print(u'The user %s doesn\'t exist.' % args.username) |
129 | 131 | sys.exit(1) |
130 | 132 | |
131 | 133 | |
… |
… |
def deleteuser(args):
|
146 | 148 | ).first() |
147 | 149 | if user: |
148 | 150 | user.delete() |
149 | | print('The user %s has been deleted' % args.username) |
| 151 | print('The user %s has been deleted.' % args.username) |
150 | 152 | else: |
151 | | print('The user %s doesn\'t exist' % args.username) |
| 153 | print('The user %s doesn\'t exist.' % args.username) |
152 | 154 | sys.exit(1) |