This repository has been archived by the owner on Jul 25, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 43
/
app.py
131 lines (99 loc) · 3.84 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
"""
app.py
~~~~~~
This simple sample application provides a bare-bones website that allows
you to:
- Create new user accounts.
- Log into existing user accounts.
- View a simple dashboard page that displays user information.
- Edit your user information on said dashboard page.
- Log out of the website.
Please see the README for more information of how to run and use this
sample application.
"""
from os import environ
from flask import Flask, redirect, render_template, request, url_for
from flask.ext.stormpath import StormpathManager, User, login_required, login_user, logout_user, user
from stormpath.error import Error as StormpathError
app = Flask(__name__)
app.debug = True
app.config['SECRET_KEY'] = 'ilovecookies'
app.config['STORMPATH_API_KEY_ID'] = environ.get('STORMPATH_API_KEY_ID')
app.config['STORMPATH_API_KEY_SECRET'] = environ.get('STORMPATH_API_KEY_SECRET')
app.config['STORMPATH_APPLICATION'] = environ.get('STORMPATH_APPLICATION')
stormpath_manager = StormpathManager(app)
stormpath_manager.login_view = '.login'
##### Website
@app.route('/')
def index():
"""Basic home page."""
return render_template('index.html')
@app.route('/register', methods=['GET', 'POST'])
def register():
"""
This view allows a user to register for the site.
This will create a new User in Stormpath, and then log the user into their
new account immediately (no email verification required).
"""
if request.method == 'GET':
return render_template('register.html')
try:
# Create a new Stormpath User.
_user = stormpath_manager.application.accounts.create({
'email': request.form.get('email'),
'password': request.form.get('password'),
'given_name': 'John',
'surname': 'Doe',
})
_user.__class__ = User
except StormpathError as err:
# If something fails, we'll display a user-friendly error message.
return render_template('register.html', error=err.message, email=request.form.get('email'))
login_user(_user, remember=True)
return redirect(url_for('dashboard'))
@app.route('/login', methods=['GET', 'POST'])
def login():
"""
This view logs in a user given an email address and password.
This works by querying Stormpath with the user's credentials, and either
getting back the User object itself, or an exception (in which case well
tell the user their credentials are invalid).
If the user is valid, we'll log them in, and store their session for later.
"""
if request.method == 'GET':
return render_template('login.html')
try:
_user = User.from_login(
request.form.get('email'),
request.form.get('password'),
)
except StormpathError as err:
return render_template('login.html', error=err.message, email=request.form.get('email'))
login_user(_user, remember=True)
return redirect(request.args.get('next') or url_for('dashboard'))
@app.route('/dashboard', methods=['GET', 'POST'])
@login_required
def dashboard():
"""
This view renders a simple dashboard page for logged in users.
Users can see their personal information on this page, as well as store
additional data to their account (if they so choose).
"""
if request.method == 'POST':
if request.form.get('birthday'):
user.custom_data['birthday'] = request.form.get('birthday')
if request.form.get('color'):
user.custom_data['color'] = request.form.get('color')
user.save()
return render_template('dashboard.html')
@app.route('/logout')
@login_required
def logout():
"""
Log out a logged in user. Then redirect them back to the main page of the
site.
"""
logout_user()
return redirect(url_for('index'))
if __name__ == '__main__':
app.run()