def create_user(auth_token): try: post_data = request.get_json(force=True) except BadRequest as e: raise ApiError(e.description) if not post_data.get('password', False): raise ApiError('Missing or empty password.') post_data['password'] = md5(post_data['password'].encode()).hexdigest() new_user = User() new_tag.update(post_data) db_session.add(new_user) db_session.commit() return jsonify(new_user.to_dict(max_depth=2))
def __call__(self): # Create an application context. app = create_app(__name__, []) ctx = app.test_request_context() ctx.push() parser = argparse.ArgumentParser( prog=self.argv[0], description="Manage the user's account.") subparsers = parser.add_subparsers(dest='subcommand') subparsers.required = True sub = subparsers.add_parser('add', help='add a user') sub.add_argument('email', action='store', help="the email of the new user's account") sub.add_argument( '-n', '--name', dest='name', action='store', help='the full name of the user (default: email address)') sub.add_argument( '-p', '--password', dest='password', action='store', help='the full name of the user (will be asked if not provided)') sub = subparsers.add_parser('list', help='list users') args = parser.parse_args(self.argv[1:]) if args.subcommand == 'add': new_user = User() new_user.email = args.email new_user.name = args.name or args.email if args.password: password = args.password else: password = getpass('password: '******'confirm: ') != password: raise InvalidArgumentError('Password do not match.') new_user.password = md5(password.encode()).hexdigest() db_session.add(new_user) db_session.commit() elif args.subcommand == 'list': for user in db_session.query(User): print('name: {:>15}, email: {:>15}'.format(user.name, user.email)) ctx.pop()