├── config
├── __init__.py
└── email.py.example
├── .gitignore
├── templates
├── index.html
├── security
│ └── login.html
└── base.html
├── requirements.txt
├── README.md
└── app.py
/config/__init__.py:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | *.pyc
2 | config/*.py
3 | !config/__init__.py
4 | !config/*.example
5 |
--------------------------------------------------------------------------------
/templates/index.html:
--------------------------------------------------------------------------------
1 | {% extends "base.html" %}
2 |
3 | {% block content %}
4 | Home Page
5 | {% endblock %}
--------------------------------------------------------------------------------
/requirements.txt:
--------------------------------------------------------------------------------
1 | https://github.com/mattupstate/flask-security/tarball/messages
2 | https://github.com/mrjoes/flask-babel/tarball/master
3 | Flask-SQLAlchemy==0.16
4 | Flask-Mail==0.7.1
--------------------------------------------------------------------------------
/config/email.py.example:
--------------------------------------------------------------------------------
1 | MAIL_SERVER = 'smtp.gmail.com'
2 | MAIL_PORT = 465
3 | MAIL_USE_TLS = False
4 | MAIL_USE_SSL = True
5 | MAIL_USERNAME = 'your-gmail-username'
6 | MAIL_PASSWORD = 'your-gmail-password'
7 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Flask-Security Example
2 |
3 | 1. Clone repo:
4 |
5 | $ git clone git://github.com/mattupstate/flask-security-example.git
6 |
7 | 2. Change directory:
8 |
9 | $ cd flask-security-example
10 |
11 | 3. Install dependencies:
12 |
13 | $ pip install -r requirements.text
14 |
15 | 4. Copy the example email config and edit the values:
16 |
17 | $ cp config/email.py.example config/email.py
18 |
19 | 5. Start the app:
20 |
21 | $ python app.py
22 |
23 |
--------------------------------------------------------------------------------
/templates/security/login.html:
--------------------------------------------------------------------------------
1 | {% extends "base.html" %}
2 | {% from "security/_macros.html" import render_field_with_errors, render_field %}
3 |
4 | {% block content %}
5 | {% include "security/_messages.html" %}
6 |
Custom Login Form
7 |
15 | {% include "security/_menu.html" %}
16 | {% endblock %}
--------------------------------------------------------------------------------
/templates/base.html:
--------------------------------------------------------------------------------
1 |
2 |
3 | Flask-Security Example
4 |
5 |
6 | {%- with messages = get_flashed_messages(with_categories=true) -%}
7 | {% if messages %}
8 |
9 | {% for category, message in messages %}
10 | - {{ message }}
11 | {% endfor %}
12 |
13 | {% endif %}
14 | {%- endwith %}
15 |
16 | {% if current_user.is_authenticated() %}
17 | - Hello {{ current_user.email }}
18 | - Logout
19 | {% else %}
20 | - Login
21 | - Register
22 | {% endif %}
23 |
24 |
25 | {% block content %}
26 | {% endblock %}
27 |
28 |
29 |
--------------------------------------------------------------------------------
/app.py:
--------------------------------------------------------------------------------
1 |
2 | from flask import Flask, render_template, request, session
3 | from flask.ext.babel import Babel
4 | from flask.ext.mail import Mail
5 | from flask.ext.sqlalchemy import SQLAlchemy
6 | from flask.ext.security import Security, SQLAlchemyUserDatastore, \
7 | UserMixin, RoleMixin
8 |
9 | # Create app
10 | app = Flask(__name__)
11 | app.config['DEBUG'] = True
12 | app.config['SECRET_KEY'] = 'super-secret'
13 | app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////tmp/db.sqlite'
14 | app.config['DEFAULT_MAIL_SENDER'] = 'info@site.com'
15 | app.config['SECURITY_REGISTERABLE'] = True
16 | app.config['SECURITY_CONFIRMABLE'] = True
17 | app.config['SECURITY_RECOVERABLE'] = True
18 | app.config.from_object('config.email')
19 |
20 | # Setup mail extension
21 | mail = Mail(app)
22 |
23 | # Setup babel
24 | babel = Babel(app)
25 |
26 | @babel.localeselector
27 | def get_locale():
28 | override = request.args.get('lang')
29 |
30 | if override:
31 | session['lang'] = override
32 |
33 | rv = session.get('lang', 'en')
34 | return rv
35 |
36 | # Create database connection object
37 | db = SQLAlchemy(app)
38 |
39 | # Define models
40 | roles_users = db.Table('roles_users',
41 | db.Column('user_id', db.Integer(), db.ForeignKey('user.id')),
42 | db.Column('role_id', db.Integer(), db.ForeignKey('role.id')))
43 |
44 | class Role(db.Model, RoleMixin):
45 | id = db.Column(db.Integer(), primary_key=True)
46 | name = db.Column(db.String(80), unique=True)
47 | description = db.Column(db.String(255))
48 |
49 | class User(db.Model, UserMixin):
50 | id = db.Column(db.Integer, primary_key=True)
51 | email = db.Column(db.String(255), unique=True)
52 | password = db.Column(db.String(255))
53 | active = db.Column(db.Boolean())
54 | confirmed_at = db.Column(db.DateTime())
55 | roles = db.relationship('Role', secondary=roles_users,
56 | backref=db.backref('users', lazy='dynamic'))
57 |
58 | def __str__(self):
59 | return '' % (self.id, self.email)
60 |
61 | # Setup Flask-Security
62 | user_datastore = SQLAlchemyUserDatastore(db, User, Role)
63 | security = Security(app, user_datastore)
64 |
65 | db.create_all()
66 |
67 | # Views
68 | @app.route('/')
69 | def home():
70 | return render_template('index.html')
71 |
72 | if __name__ == '__main__':
73 | app.run()
74 |
--------------------------------------------------------------------------------