From dd6ba0995d7a25ebe59dadec6438d26b84a01f6b Mon Sep 17 00:00:00 2001 From: Matt Wright Date: Tue, 27 Mar 2012 14:26:45 -0400 Subject: [PATCH 1/2] Random fixes --- .gitignore | 2 ++ LICENSE | 17 ++++++++++++++--- docs/index.rst | 22 +++++++++++++++++++--- 3 files changed, 35 insertions(+), 6 deletions(-) diff --git a/.gitignore b/.gitignore index 8af5f8c..f63cb54 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,8 @@ .DS_Store *.pyc *.egg +*egg-info +dist .project .pydevproject .settings diff --git a/LICENSE b/LICENSE index 2e8ddd0..bbf6b57 100644 --- a/LICENSE +++ b/LICENSE @@ -2,8 +2,19 @@ MIT License Copyright (C) 2012 by Matt Wright -Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies +of the Software, and to permit persons to whom the Software is furnished to do +so, subject to the following conditions: -The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. \ No newline at end of file +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR +COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER +IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. \ No newline at end of file diff --git a/docs/index.rst b/docs/index.rst index e0478a5..a7df7f5 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -28,6 +28,7 @@ Contents * :ref:`overview` * :ref:`installation` * :ref:`getting-started` +* :ref:`flask-script-commands` * :ref:`api` * :doc:`Changelog ` @@ -58,7 +59,7 @@ Installation First, install Flask-Security:: $ mkvirtualenv app-name - $ pip install https://github.com/mattupstate/flask-security/tarball/master + $ pip install Flask-Security Then install your datastore requirement. @@ -81,8 +82,8 @@ First thing you'll want to do is setup your application and datastore:: from flask import Flask, render_template from flask.ext.sqlalchemy import SQLAlchemy - from flask.ext.security import User, Security, LoginForm, - login_required, roles_accepted, user_datastore + from flask.ext.security import (User, Security, LoginForm, login_required, + roles_accepted, user_datastore) from flask.ext.security.datastore.sqlalchemy import SQLAlchemyUserDataStore app = Flask(__name__) @@ -150,6 +151,21 @@ specific role:: {% if current_user.has_role('admin') %} Admin Panel {$ endif %} + + +.. _flask-script-commands: + +Flask-Script Commands +--------------------- +Flask-Security comes packed with a few Flask-Script commands. They are: + +* :class:`flask.ext.security.script.CreateUserCommand` +* :class:`flask.ext.security.script.AddRoleCommand` +* :class:`flask.ext.security.script.RemoveRoleCommand` +* :class:`flask.ext.security.script.DeactivateUserCommand` +* :class:`flask.ext.security.script.ActivateUserCommand` + +Register these on your script manager for pure convenience. .. _configuration: From 5a7f4dff47f0b9a19dde8aace598840d7d76d7cd Mon Sep 17 00:00:00 2001 From: Matt Wright Date: Tue, 27 Mar 2012 14:27:12 -0400 Subject: [PATCH 2/2] Initial idea for specifying a user account mixin for user model --- example/app.py | 14 ++++++++++++-- flask_security/__init__.py | 1 + flask_security/datastore/__init__.py | 3 ++- flask_security/datastore/mongoengine.py | 2 +- flask_security/datastore/sqlalchemy.py | 2 +- 5 files changed, 17 insertions(+), 5 deletions(-) diff --git a/example/app.py b/example/app.py index a1df2d2..3f86072 100644 --- a/example/app.py +++ b/example/app.py @@ -84,7 +84,12 @@ def create_sqlalchemy_app(auth_config=None): app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////tmp/flask_security_example.sqlite' db = SQLAlchemy(app) - Security(app, SQLAlchemyUserDatastore(db)) + + class UserAccountMixin(): + first_name = db.Column(db.String(120)) + last_name = db.Column(db.String(120)) + + Security(app, SQLAlchemyUserDatastore(db, UserAccountMixin)) @app.before_first_request def before_first_request(): @@ -101,7 +106,12 @@ def create_mongoengine_app(auth_config=None): app.config['MONGODB_PORT'] = 27017 db = MongoEngine(app) - Security(app, MongoEngineUserDatastore(db)) + + class UserAccountMixin(): + first_name = db.StringField(max_length=120) + last_name = db.StringField(max_length=120) + + Security(app, MongoEngineUserDatastore(db, UserAccountMixin)) @app.before_first_request def before_first_request(): diff --git a/flask_security/__init__.py b/flask_security/__init__.py index 0ed3995..49a98bb 100644 --- a/flask_security/__init__.py +++ b/flask_security/__init__.py @@ -256,6 +256,7 @@ class Security(object): """ if app is None or datastore is None: return + # TODO: change blueprint name blueprint = Blueprint('auth', __name__) configured = {} diff --git a/flask_security/datastore/__init__.py b/flask_security/datastore/__init__.py index 2bf605b..ef02910 100644 --- a/flask_security/datastore/__init__.py +++ b/flask_security/datastore/__init__.py @@ -21,8 +21,9 @@ class UserDatastore(object): :param db: An instance of a configured databse manager from a Flask extension such as Flask-SQLAlchemy or Flask-MongoEngine""" - def __init__(self, db): + def __init__(self, db, user_account_mixin=None): self.db = db + self.user_account_mixin = user_account_mixin or object def get_models(self): """Returns configured `User` and `Role` models for the datastore diff --git a/flask_security/datastore/mongoengine.py b/flask_security/datastore/mongoengine.py index 8894a21..7e28c39 100644 --- a/flask_security/datastore/mongoengine.py +++ b/flask_security/datastore/mongoengine.py @@ -41,7 +41,7 @@ class MongoEngineUserDatastore(UserDatastore): name = db.StringField(required=True, unique=True, max_length=80) description = db.StringField(max_length=255) - class User(db.Document, UserMixin): + class User(db.Document, UserMixin, self.user_account_mixin): """MongoEngine User model""" username = db.StringField(unique=True, max_length=255) diff --git a/flask_security/datastore/sqlalchemy.py b/flask_security/datastore/sqlalchemy.py index 5fe3d55..e239b51 100644 --- a/flask_security/datastore/sqlalchemy.py +++ b/flask_security/datastore/sqlalchemy.py @@ -48,7 +48,7 @@ class SQLAlchemyUserDatastore(UserDatastore): self.name = name self.description = description - class User(db.Model, UserMixin): + class User(db.Model, UserMixin, self.user_account_mixin): """SQLAlchemy User model""" id = db.Column(db.Integer, primary_key=True)