From 7ddc132af5931777f37a323d2134a50f596dceda Mon Sep 17 00:00:00 2001 From: Matt Wright Date: Fri, 24 Aug 2012 00:47:07 -0400 Subject: [PATCH] Update script commands a bit --- flask_security/script.py | 30 +++++++++++++++++++++++++----- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/flask_security/script.py b/flask_security/script.py index f81159d..9e19e1f 100644 --- a/flask_security/script.py +++ b/flask_security/script.py @@ -1,18 +1,25 @@ +# -*- coding: utf-8 -*- +""" + flask.ext.security.script + ~~~~~~~~~~~~~~~~~~~~~~~~~ + Flask-Security script module + + :copyright: (c) 2012 by Matt Wright. + :license: MIT, see LICENSE for more details. +""" try: import simplejson as json except ImportError: import json -import inspect -import os import re from flask import current_app -from flask.ext.script import Command, Option, prompt_bool +from flask.ext.script import Command, Option from werkzeug.local import LocalProxy -from flask_security import views, utils +from .utils import encrypt_password _datastore = LocalProxy(lambda: current_app.extensions['security'].datastore) @@ -22,6 +29,13 @@ def pprint(obj): print json.dumps(obj, sort_keys=True, indent=4) +def commit(fn): + def wrapper(*args, **kwargs): + fn(*args, **kwargs) + _datastore._commit() + return wrapper + + class CreateUserCommand(Command): """Create a user""" @@ -32,6 +46,7 @@ class CreateUserCommand(Command): Option('-r', '--roles', dest='roles', default=''), ) + @commit def run(self, **kwargs): # sanitize active input ai = re.sub(r'\s', '', str(kwargs['active'])) @@ -40,7 +55,7 @@ class CreateUserCommand(Command): # sanitize role input a bit ri = re.sub(r'\s', '', kwargs['roles']) kwargs['roles'] = [] if ri == '' else ri.split(',') - kwargs['password'] = utils.encrypt_password(kwargs['password']) + kwargs['password'] = encrypt_password(kwargs['password']) _datastore.create_user(**kwargs) @@ -57,6 +72,7 @@ class CreateRoleCommand(Command): Option('-d', '--desc', dest='description', default=None), ) + @commit def run(self, **kwargs): _datastore.create_role(**kwargs) print 'Role "%(name)s" created successfully.' % kwargs @@ -72,6 +88,7 @@ class _RoleCommand(Command): class AddRoleCommand(_RoleCommand): """Add a role to a user""" + @commit def run(self, user_identifier, role_name): _datastore.add_role_to_user(user_identifier, role_name) print "Role '%s' added to user '%s' successfully" % (role_name, user_identifier) @@ -80,6 +97,7 @@ class AddRoleCommand(_RoleCommand): class RemoveRoleCommand(_RoleCommand): """Add a role to a user""" + @commit def run(self, user_identifier, role_name): _datastore.remove_role_from_user(user_identifier, role_name) print "Role '%s' removed from user '%s' successfully" % (role_name, user_identifier) @@ -94,6 +112,7 @@ class _ToggleActiveCommand(Command): class DeactivateUserCommand(_ToggleActiveCommand): """Deactive a user""" + @commit def run(self, user_identifier): _datastore.deactivate_user(user_identifier) print "User '%s' has been deactivated" % user_identifier @@ -102,6 +121,7 @@ class DeactivateUserCommand(_ToggleActiveCommand): class ActivateUserCommand(_ToggleActiveCommand): """Deactive a user""" + @commit def run(self, user_identifier): _datastore.activate_user(user_identifier) print "User '%s' has been activated" % user_identifier