-
Notifications
You must be signed in to change notification settings - Fork 2
/
web.py
279 lines (206 loc) · 6.99 KB
/
web.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
import json
import os
import logging
from flask import Flask, render_template, jsonify, redirect, url_for, session, request
from flask_sslify import SSLify
from flask_oauth import OAuth
from urllib2 import Request, urlopen, URLError
from mongo_agent import MongoAgent
# Initialize logging and setting to INFO level
logging.basicConfig(format='%(asctime)s [%(levelname)s]: %(message)s', level=logging.INFO)
# Environment variables
GOOGLE_CLIENT_ID = os.environ['GOOGLE_CLIENT_ID']
GOOGLE_CLIENT_SECRET = os.environ['GOOGLE_CLIENT_SECRET']
SECRET_KEY = os.environ['SECRET_KEY']
ENVIRONMENT_TYPE = os.environ['ENVIRONMENT_TYPE']
MONGO_DB = os.environ['MONGO_DB']
MONGO_CONN = os.environ['MONGO_CONN']
# Flask app configuration
app = Flask(__name__)
app.secret_key = SECRET_KEY
# Initialize Mongo agent
mongo = MongoAgent(MONGO_CONN, MONGO_DB)
# Google OAuth
REDIRECT_URI = '/gCallback'
oauth = OAuth()
google = oauth.remote_app('google',
base_url='https://www.google.com/accounts/',
authorize_url='https://accounts.google.com/o/oauth2/auth',
request_token_url=None,
request_token_params={'scope': 'https://www.googleapis.com/auth/userinfo.email',
'response_type': 'code'},
access_token_url='https://accounts.google.com/o/oauth2/token',
access_token_method='POST',
access_token_params={'grant_type': 'authorization_code'},
consumer_key=GOOGLE_CLIENT_ID,
consumer_secret=GOOGLE_CLIENT_SECRET)
# global
name = None
email = None
picture = None
@app.route("/")
def index(doc="dashboard.html"):
access_token = session.get('access_token')
if access_token is None:
return redirect(url_for('login'))
access_token = access_token[0]
headers = {'Authorization': 'OAuth ' + access_token}
req = Request('https://www.googleapis.com/oauth2/v1/userinfo', None, headers)
try:
res = urlopen(req)
except URLError:
# Unauthorized - bad token
session.pop('access_token', None)
return redirect(url_for('login'))
google_user_info = json.loads(res.read())
email = google_user_info['email']
name = google_user_info['name']
picture = google_user_info['picture']
session['email'] = email
if email[-11:] == "@pivotal.io":
return render_template(doc, email=email, name=name, token=access_token,
picture=picture)
else:
return render_template('error.html', email=email)
@app.route('/login')
def login():
callback = url_for('authorized', _external=True)
return google.authorize(callback=callback)
@app.route(REDIRECT_URI)
@google.authorized_handler
def authorized(resp):
access_token = resp['access_token']
session['access_token'] = access_token, ''
return redirect(url_for('index'))
@google.tokengetter
def get_access_token():
return session.get('access_token')
# Page Redirects
@app.route('/license')
def get_license():
return index("license.html")
@app.route('/dashboard')
def get_dashboard():
return index("dashboard.html")
@app.route('/metrics')
def get_metrics():
return index("metrics.html")
@app.route('/historical')
def get_historical():
return index("historical.html")
@app.route('/trends')
def get_trends():
return index("trends.html")
@app.route('/weblinks')
def get_weblinks():
return index("weblinks.html")
@app.route('/mykb')
def get_mykb():
return index("mykb.html")
@app.route('/newkb')
def get_newkb():
return index("newkb.html")
@app.route('/updatedkb')
def get_updatedkb():
return index("updatedkb.html")
@app.route('/analyticskb')
def get_analyticskb():
return index("analyticskb.html")
# Data Extractor
@app.route('/get_user_data')
def get_user_data():
email = session.get('email')
data = mongo.get_dashboard_data(email)
return jsonify(user=data['dash_data'])
@app.route('/get_historical_data')
def get_historical_data():
email = session.get('email')
data = mongo.get_historical_data(email)
return jsonify(historical=data['hist_data'])
@app.route('/get_metrics_data')
def get_metrics_data():
email = session.get('email')
data = mongo.get_metrics_data(email)
return jsonify(metrics=data['metrics_data'])
@app.route('/post_weblink_form_data', methods=['POST'])
def post_weblink_form_data():
id = request.form['weblink_row_id']
product_category = request.form['pivotal_products']
short_heading = request.form['short_heading']
website_url = request.form['website_url']
contact_person = request.form['contact_person']
contact_person_email = request.form['contact_person_email']
long_description = request.form['long_description']
if not id:
result = mongo.add_form_data(
product_category,
short_heading,
website_url,
contact_person,
contact_person_email,
long_description
)
else:
result = mongo.update_form_data(
id,
product_category,
short_heading,
website_url,
contact_person,
contact_person_email,
long_description
)
if result:
return redirect("/weblinks")
else:
return "failure"
@app.route('/get_weblink_data')
def get_weblink_data():
data = {'support_services': [],
'pivotal_greenplum': [],
'pivotal_hdb': [],
'pivotal_gemfire': [],
'pivotal_cloud_foundry': []}
for product in data.keys():
data[product] = mongo.get_weblink_data(product)
return jsonify(weblinks=data)
@app.route('/get_kbanalytics_data')
def get_kbanalytics_data():
data = mongo.get_kbanalytics_data()
return jsonify(kbanalytics=data)
@app.route('/get_trends_data')
def get_trends_data():
data = mongo.get_trends_data()
return jsonify(trends=data)
@app.route('/get_kb_data')
def get_kb_data():
data = mongo.get_kb_data()
return jsonify(kbdata=data)
@app.route('/post_mykb_data', methods=['POST'])
def post_mykb_data():
id = request.form['Id']
result = mongo.update_mykb_data(id)
if result:
return "success"
else:
return "failure"
@app.route('/get_mykb_data')
def get_mykb_data():
email = session.get('email')
data = mongo.get_mykb_data(email)
return jsonify(mykb=data['mykb_data'])
@app.route('/insert_backed_up_weblinks')
def insert_backed_up_weblinks():
# data = <INSERT BACKED UP LINKS HERE AS LIST>
# mongo.insert_backed_up_links(data)
return redirect("/weblinks")
if __name__ == "__main__":
# Apply production configuration
if ENVIRONMENT_TYPE == 'prod':
SSLify(app)
port = int(os.getenv("PORT"))
app.run(host='0.0.0.0', port=port)
else:
app.port = int(os.getenv("PORT"))
app.debug = True
app.run()