-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflaskr.py
109 lines (87 loc) · 2.75 KB
/
flaskr.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
from datetime import datetime
from ConfigParser import ConfigParser
import os.path
from flask import flash, Flask, redirect, render_template, request, url_for
from flask_wtf.csrf import CSRFProtect
from flask_stormpath import login_required, StormpathManager, user
"""
COnfiguration check
"""
env_files = ['flaskr.ini', 'apiKey.properties']
for file in env_files:
if not os.path.isfile(file):
raise IOError('Generate env files before running the application.')
"""
Environment settings.
"""
config = ConfigParser()
config.read('flaskr.ini')
"""
Application settings.
"""
flaskr_credentials = {
'DEBUG': True,
'SECRET_KEY': 'travis_secret',
'STORMPATH_API_KEY_FILE': 'apiKey.properties',
'STORMPATH_APPLICATION': 'flaskr_travis',
'STORMPATH_CONFIG_PATH': os.path.join(
os.path.dirname(os.path.abspath(__file__)),
'config', 'default-config' + '.yml'),
# Social login
'STORMPATH_SOCIAL': {
'FACEBOOK': {
'app_id': config.get('env', 'FACEBOOK_APP_ID'),
'app_secret': config.get('env', 'FACEBOOK_APP_SECRET')},
'GOOGLE': {
'client_id': config.get('env', 'GOOGLE_CLIENT_ID'),
'client_secret': config.get('env', 'GOOGLE_CLIENT_SECRET')}
},
'STORMPATH_ENABLE_FACEBOOK': True,
'STORMPATH_ENABLE_GOOGLE': True
}
app = Flask(__name__)
csrf = CSRFProtect(app)
app.config.update(flaskr_credentials)
stormpath_manager = StormpathManager(app)
"""
Views
"""
@app.route('/')
@login_required
def index():
from helpers import development
flash('Use this view for testing out any flask / python-sdk stuff.')
params = {
'login': '[email protected]',
'password': 'Thesouprecha1'
}
development(params)
return render_template('layout.html')
@app.route('/posts')
@login_required
def show_posts():
posts = []
for account in stormpath_manager.application.accounts.search(user.email):
if account.custom_data.get('posts'):
posts.extend(account.custom_data['posts'])
posts = sorted(posts, key=lambda k: k['date'], reverse=True)
return render_template('show_posts.html', posts=posts)
@app.route('/add', methods=['POST'])
@login_required
def add_post():
if not user.custom_data.get('posts'):
user.custom_data['posts'] = []
user.custom_data['posts'].append({
'date': datetime.utcnow().isoformat(),
'title': request.form['title'],
'text': request.form['text'],
})
user.save()
flash('New post successfully added.')
return redirect(url_for('show_posts'))
@app.route('/invalid_request')
def invalid_request():
flash('This is how I handle the response.')
return render_template('show_posts.html', posts=[])
if __name__ == '__main__':
app.run()