Skip to content

Commit 3b56b36

Browse files
committed
added example using blueprints/factories
1 parent fe26cdf commit 3b56b36

File tree

10 files changed

+122
-0
lines changed

10 files changed

+122
-0
lines changed

examples/blueprints/blueprints/__init__.py

Whitespace-only changes.
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
from flask import Flask, g, session
2+
3+
from .config import BaseConfig
4+
from .extensions import ldap
5+
from .core import core
6+
from .foo import foo
7+
8+
DEFAULT_BLUEPRINTS = (
9+
core,
10+
foo
11+
)
12+
13+
14+
def create_app(config=None, app_name=None, blueprints=None):
15+
if app_name is None:
16+
app_name = BaseConfig.PROJECT
17+
if blueprints is None:
18+
blueprints = DEFAULT_BLUEPRINTS
19+
app = Flask(app_name)
20+
configure_app(app, config)
21+
register_hooks(app)
22+
register_blueprints(app, blueprints)
23+
register_extensions(app)
24+
return app
25+
26+
27+
def configure_app(app, config=None):
28+
if config:
29+
app.config.from_object(config)
30+
31+
32+
def register_hooks(app):
33+
@app.before_request
34+
def before_request():
35+
g.user = None
36+
if 'user_id' in session:
37+
# This is where you'd query your database to get the user info.
38+
g.user = {}
39+
# Create a global with the LDAP groups the user is a member of.
40+
g.ldap_groups = ldap.get_user_groups(user=session['user_id'])
41+
42+
43+
def register_blueprints(app, blueprints):
44+
for blueprint in blueprints:
45+
app.register_blueprint(blueprint)
46+
47+
48+
def register_extensions(app):
49+
ldap.init_app(app)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class BaseConfig(object):
2+
PROJECT = 'foo'
3+
SECRET_KEY = 'dev key'
4+
DEBUG = True
5+
6+
# LDAP
7+
LDAP_HOST = 'ldap.example.org'
8+
LDAP_BASE_DN = 'OU=users,dc=example,dc=org'
9+
LDAP_USERNAME = 'CN=user,OU=Users,DC=example,DC=org'
10+
LDAP_PASSWORD = 'password'
11+
LDAP_LOGIN_VIEW = 'core.login'
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from .views import core
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
from flask import Blueprint, g, request, session, redirect, url_for
2+
from ..extensions import ldap
3+
4+
core = Blueprint('core', __name__)
5+
6+
7+
@core.route('/')
8+
@ldap.login_required
9+
def index():
10+
return 'Successfully logged in!'
11+
12+
13+
@core.route('/login', methods=['GET', 'POST'])
14+
def login():
15+
if g.user:
16+
return redirect(url_for('index'))
17+
if request.method == 'POST':
18+
user = request.form['user']
19+
passwd = request.form['passwd']
20+
test = ldap.bind_user(user, passwd)
21+
if test is None:
22+
return 'Invalid credentials'
23+
else:
24+
session['user_id'] = request.form['user']
25+
return redirect('/')
26+
return """<form action="" method="post">
27+
user: <input name="user"><br>
28+
password:<input type="password" name="passwd"><br>
29+
<input type="submit" value="Submit"></form>"""
30+
31+
32+
@core.route('/group')
33+
@ldap.group_required(groups=['Web Developers', 'QA'])
34+
def group():
35+
return 'Group restricted page'
36+
37+
38+
@core.route('/logout')
39+
def logout():
40+
session.pop('user_id', None)
41+
return redirect(url_for('index'))
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
from flask.ext.simpleldap import LDAP
2+
ldap = LDAP()
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from .views import foo
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from flask import Blueprint
2+
from ..extensions import ldap
3+
4+
foo = Blueprint('foo', __name__, url_prefix='/foo')
5+
6+
7+
@foo.route('/group')
8+
@ldap.group_required(groups=['Web Developers', 'QA'])
9+
def group():
10+
return 'Group restricted page in foo module'

examples/blueprints/run.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from blueprints.app import create_app
2+
from blueprints.config import BaseConfig
3+
4+
app = create_app(BaseConfig)
5+
6+
if __name__ == "__main__":
7+
app.run()
File renamed without changes.

0 commit comments

Comments
 (0)