mirror of
https://github.com/wassname/flask-security.git
synced 2026-08-19 12:10:07 +08:00
Merge branch 'develop' of git://github.com/mattupstate/flask-security into template_list
Conflicts: requirements.txt
This commit is contained in:
+13
-2
@@ -49,6 +49,10 @@ Datastores
|
||||
:members:
|
||||
:inherited-members:
|
||||
|
||||
.. autoclass:: flask_security.datastore.PeeweeUserDatastore
|
||||
:members:
|
||||
:inherited-members:
|
||||
|
||||
|
||||
Signals
|
||||
-------
|
||||
@@ -83,7 +87,13 @@ sends the following signals.
|
||||
|
||||
.. data:: password_reset
|
||||
|
||||
Sent when a user completes a password. It is passed the `user`.
|
||||
Sent when a user completes a password reset. It is passed the
|
||||
`user`.
|
||||
|
||||
.. data:: password_changed
|
||||
|
||||
Sent when a user completes a password change. It is passed the
|
||||
`user`.
|
||||
|
||||
.. data:: reset_password_instructions_sent
|
||||
|
||||
@@ -91,6 +101,7 @@ sends the following signals.
|
||||
with the `user` and `token`, the user being logged in and
|
||||
the (if so configured) the reset token issued.
|
||||
|
||||
All signals are also passed a `app` keyword argument, which is the current application.
|
||||
All signals are also passed a `app` keyword argument, which is the
|
||||
current application.
|
||||
|
||||
.. _Flask documentation on signals: http://flask.pocoo.org/docs/signals/
|
||||
|
||||
@@ -18,6 +18,7 @@ following is a list of view templates:
|
||||
* `security/login_user.html`
|
||||
* `security/register_user.html`
|
||||
* `security/reset_password.html`
|
||||
* `security/change_password.html`
|
||||
* `security/send_confirmation.html`
|
||||
* `security/send_login.html`
|
||||
|
||||
@@ -57,6 +58,7 @@ The following is a list of all the available context processor decorators:
|
||||
* ``login_context_processor``: Login view
|
||||
* ``register_context_processor``: Register view
|
||||
* ``reset_password_context_processor``: Reset password view
|
||||
* ``change_password_context_processor``: Reset password view
|
||||
* ``send_confirmation_context_processor``: Send confirmation view
|
||||
* ``send_login_context_processor``: Send login view
|
||||
|
||||
@@ -77,6 +79,18 @@ register form or override validators::
|
||||
security = Security(app, user_datastore,
|
||||
register_form=ExtendedRegisterForm)
|
||||
|
||||
For the ``register_form`` and ``confirm_register_form``, each field is
|
||||
passed to the user model (as kwargs) when a user is created. In the
|
||||
above case, the ``first_name`` and ``last_name`` fields are passed
|
||||
directly to the model, so the model should look like::
|
||||
|
||||
class User(db.Model, UserMixin):
|
||||
id = db.Column(db.Integer, primary_key=True)
|
||||
email = db.Column(db.String(255), unique=True)
|
||||
password = db.Column(db.String(255))
|
||||
first_name = db.Column(db.String(255))
|
||||
last_name = db.Column(db.String(255))
|
||||
|
||||
The following is a list of all the available form overrides:
|
||||
|
||||
* ``login_form``: Login form
|
||||
@@ -84,6 +98,7 @@ The following is a list of all the available form overrides:
|
||||
* ``register_form``: Register form
|
||||
* ``forgot_password_form``: Forgot password form
|
||||
* ``reset_password_form``: Reset password form
|
||||
* ``change_password_form``: Reset password form
|
||||
* ``send_confirmation_form``: Send confirmation form
|
||||
* ``passwordless_login_form``: Passwordless login form
|
||||
|
||||
@@ -102,6 +117,8 @@ The following is a list of email templates:
|
||||
* `security/mail/reset_instructions.html`
|
||||
* `security/mail/reset_instructions.txt`
|
||||
* `security/mail/reset_notice.html`
|
||||
* `security/mail/change_notice.txt`
|
||||
* `security/mail/change_notice.html`
|
||||
* `security/mail/reset_notice.txt`
|
||||
* `security/mail/welcome.html`
|
||||
* `security/mail/welcome.txt`
|
||||
|
||||
+6
-5
@@ -10,7 +10,7 @@ Flask application. They include:
|
||||
4. Basic HTTP authentication
|
||||
5. Token based authentication
|
||||
6. Token based account activation (optional)
|
||||
7. Token based password recovery/resetting (optional)
|
||||
7. Token based password recovery / resetting (optional)
|
||||
8. User registration (optional)
|
||||
9. Login tracking (optional)
|
||||
|
||||
@@ -27,10 +27,11 @@ and libraries. They include:
|
||||
|
||||
Additionally, it assumes you'll be using a common library for your database
|
||||
connections and model definitions. Flask-Security supports the following Flask
|
||||
extensions out of the box for data persistance:
|
||||
extensions out of the box for data persistence:
|
||||
|
||||
1. `Flask-SQLAlchemy <http://packages.python.org/Flask-SQLAlchemy/>`_
|
||||
2. `Flask-MongoEngine <http://packages.python.org/Flask-MongoEngine/>`_
|
||||
1. `Flask-SQLAlchemy <http://pypi.python.org/pypi/flask-sqlalchemy/>`_
|
||||
2. `Flask-MongoEngine <http://pypi.python.org/pypi/flask-mongoengine/>`_
|
||||
3. `Flask-Peewee <http://pypi.python.org/pypi/flask-peewee/>`_
|
||||
|
||||
|
||||
.. include:: contents.rst.inc
|
||||
.. include:: contents.rst.inc
|
||||
|
||||
+7
-7
@@ -1,12 +1,12 @@
|
||||
Models
|
||||
======
|
||||
|
||||
Flask-Security assumes you'll be using libraries such as SQLAlchemy or
|
||||
MongoEngine to define a data model that includes a `User` and `Role` model. The
|
||||
fields on your models must follow a particular convention depending on the
|
||||
functionality your app requires. Aside from this, you're free to add any
|
||||
additional fields to your model(s) if you want. At the bear minimum your `User`
|
||||
and `Role` model should include the following fields:
|
||||
Flask-Security assumes you'll be using libraries such as SQLAlchemy,
|
||||
MongoEngine or Peewee to define a data model that includes a `User` and
|
||||
`Role` model. The fields on your models must follow a particular convention
|
||||
depending on the functionality your app requires. Aside from this, you're
|
||||
free to add any additional fields to your model(s) if you want. At the bear
|
||||
minimum your `User` and `Role` model should include the following fields:
|
||||
|
||||
**User**
|
||||
|
||||
@@ -48,4 +48,4 @@ additional fields:
|
||||
* ``current_login_at``
|
||||
* ``last_login_ip``
|
||||
* ``current_login_ip``
|
||||
* ``login_count``
|
||||
* ``login_count``
|
||||
|
||||
+84
-3
@@ -3,6 +3,7 @@ Quick Start
|
||||
|
||||
- `Basic SQLAlchemy Application <#basic-sqlalchemy-application>`_
|
||||
- `Basic MongoEngine Application <#basic-mongoengine-application>`_
|
||||
- `Basic Peewee Application <#basic-peewee-application>`_
|
||||
- `Mail Configuration <#mail-configuration>`_
|
||||
|
||||
Basic SQLAlchemy Application
|
||||
@@ -21,7 +22,7 @@ SQLAlchemy Application
|
||||
~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
The following code sample illustrates how to get started as quickly as
|
||||
possible using SQLAlchemy.
|
||||
possible using SQLAlchemy:
|
||||
|
||||
::
|
||||
|
||||
@@ -94,7 +95,9 @@ MongoEngine Application
|
||||
~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
The following code sample illustrates how to get started as quickly as
|
||||
possible using MongoEngine::
|
||||
possible using MongoEngine:
|
||||
|
||||
::
|
||||
|
||||
from flask import Flask, render_template
|
||||
from flask.ext.mongoengine import MongoEngine
|
||||
@@ -144,6 +147,84 @@ possible using MongoEngine::
|
||||
app.run()
|
||||
|
||||
|
||||
Basic Peewee Application
|
||||
========================
|
||||
|
||||
Peewee Install requirements
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
::
|
||||
|
||||
$ mkvirtualenv <your-app-name>
|
||||
$ pip install flask-security flask-peewee
|
||||
|
||||
Peewee Application
|
||||
~~~~~~~~~~~~~~~~~~
|
||||
|
||||
The following code sample illustrates how to get started as quickly as
|
||||
possible using Peewee:
|
||||
|
||||
::
|
||||
|
||||
from flask import Flask, render_template
|
||||
from flask_peewee.db import Database
|
||||
from peewee import *
|
||||
from flask.ext.security import Security, PeeweeUserDatastore, \
|
||||
UserMixin, RoleMixin, login_required
|
||||
|
||||
# Create app
|
||||
app = Flask(__name__)
|
||||
app.config['DEBUG'] = True
|
||||
app.config['SECRET_KEY'] = 'super-secret'
|
||||
app.config['DATABASE'] = {
|
||||
'name': 'example.db',
|
||||
'engine': 'peewee.SqliteDatabase',
|
||||
}
|
||||
|
||||
# Create database connection object
|
||||
db = Database(app)
|
||||
|
||||
class Role(db.Model, RoleMixin):
|
||||
name = TextField(unique=True)
|
||||
description = TextField(null=True)
|
||||
|
||||
class User(db.Model, UserMixin):
|
||||
email = TextField()
|
||||
password = TextField()
|
||||
active = BooleanField(default=True)
|
||||
confirmed_at = DateTimeField(null=True)
|
||||
|
||||
class UserRoles(db.Model):
|
||||
# Because peewee does not come with built-in many-to-many
|
||||
# relationships, we need this intermediary class to link
|
||||
# user to roles.
|
||||
user = ForeignKeyField(User, related_name='roles')
|
||||
role = ForeignKeyField(Role, related_name='users')
|
||||
name = property(lambda self: self.role.name)
|
||||
description = property(lambda self: self.role.description)
|
||||
|
||||
# Setup Flask-Security
|
||||
user_datastore = PeeweeUserDatastore(db, User, Role, UserRoles)
|
||||
security = Security(app, user_datastore)
|
||||
|
||||
# Create a user to test with
|
||||
@app.before_first_request
|
||||
def create_user():
|
||||
for Model in (Role, User, UserRoles):
|
||||
Model.drop_table(fail_silently=True)
|
||||
Model.create_table(fail_silently=True)
|
||||
user_datastore.create_user(email='matt@nobien.net', password='password')
|
||||
|
||||
# Views
|
||||
@app.route('/')
|
||||
@login_required
|
||||
def home():
|
||||
return render_template('index.html')
|
||||
|
||||
if __name__ == '__main__':
|
||||
app.run()
|
||||
|
||||
|
||||
Mail Configuration
|
||||
===================
|
||||
|
||||
@@ -168,4 +249,4 @@ the basic application code in the previous section::
|
||||
|
||||
To learn more about the various Flask-Mail settings to configure it to
|
||||
work with your particular email server configuration, please see the
|
||||
`Flask-Mail documentation <http://packages.python.org/Flask-Mail/>`_.
|
||||
`Flask-Mail documentation <http://packages.python.org/Flask-Mail/>`_.
|
||||
|
||||
Reference in New Issue
Block a user