Skip to content
Navigation Menu
{{ message }}
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathweb.py
More file actions
executable file
·276 lines (232 loc) · 9.11 KB
/
Copy pathweb.py
File metadata and controls
executable file
·276 lines (232 loc) · 9.11 KB
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
#
# Copyright (c) 2013 Piston Cloud Computing, Inc.
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import os
import requests
from flask import Flask, abort, flash, request, redirect, url_for, \
render_template, g, session
from flask_openid import OpenID
from flask.ext.admin import Admin, BaseView, expose, AdminIndexView
from flask.ext.admin.contrib.sqlamodel import ModelView
from flask.ext.security import Security, SQLAlchemyUserDatastore, \
UserMixin, RoleMixin, login_required
from wtforms import Form, BooleanField, TextField, \
PasswordField, validators
from flask_mail import Mail
from refstack.app import app
from refstack.models import *
mail = Mail(app)
# setup flask-openid
oid = OpenID(app)
admin = Admin(app, base_template='admin/master.html')
class SecureView(ModelView):
""" """
def is_accessible(self):
""" """
return g.user.su is not False
admin.add_view(SecureView(Vendor, db))
admin.add_view(SecureView(Cloud, db))
admin.add_view(SecureView(User, db))
@app.before_request
def before_request():
"""Runs before the request it self"""
g.user = None
if 'openid' in session:
g.user = User.query.filter_by(openid=session['openid']).first()
@app.route('/', methods=['POST', 'GET'])
def index():
"""Index view"""
if g.user is not None:
# something else
clouds = Cloud.query.filter_by(user_id=g.user.id).all()
return render_template('home.html', clouds=clouds)
else:
vendors = Vendor.query.all()
return render_template('index.html', vendors=vendors)
@app.route('/login', methods=['GET', 'POST'])
@oid.loginhandler
def login():
"""Does the login via OpenID. Has to call into `oid.try_login`
to start the OpenID machinery.
"""
# if we are already logged in, go back to were we came from
if g.user is not None:
return redirect(oid.get_next_url())
return oid.try_login("https://login.launchpad.net/",
ask_for=['email', 'nickname'])
@oid.after_login
def create_or_login(resp):
"""This is called when login with OpenID succeeded and it's not
necessary to figure out if this is the users's first login or not.
This function has to redirect otherwise the user will be presented
with a terrible URL which we certainly don't want.
"""
session['openid'] = resp.identity_url
user = User.query.filter_by(openid=resp.identity_url).first()
if user is not None:
flash(u'Successfully signed in')
g.user = user
return redirect(oid.get_next_url())
return redirect(url_for('create_profile', next=oid.get_next_url(),
name=resp.fullname or resp.nickname,
email=resp.email))
@app.route('/create-profile', methods=['GET', 'POST'])
def create_profile():
"""If this is the user's first login, the create_or_login function
will redirect here so that the user can set up his profile.
"""
if g.user is not None or 'openid' not in session:
return redirect(url_for('index'))
if request.method == 'POST':
name = request.form['name']
email = request.form['email']
if not name:
flash(u'Error: you have to provide a name')
elif '@' not in email:
flash(u'Error: you have to enter a valid email address')
else:
flash(u'Profile successfully created')
db.add(User(name, email, session['openid']))
db.commit()
return redirect(oid.get_next_url())
return render_template(
'create_profile.html', next_url=oid.get_next_url())
@app.route('/delete-cloud/<int:cloud_id>', methods=['GET', 'POST'])
def delete_cloud(cloud_id):
""" delete function for clouds"""
c = Cloud.query.filter_by(id=cloud_id).first()
if not c:
flash(u'Not a valid Cloud ID!')
elif not c.user_id == g.user.id:
flash(u"This isn't your cloud!")
else:
db.delete(c)
db.commit()
return redirect('/')
@app.route('/edit-cloud/<int:cloud_id>', methods=['GET', 'POST'])
def edit_cloud(cloud_id):
c = Cloud.query.filter_by(id=cloud_id).first()
if not c:
flash(u'Not a valid Cloud ID!')
return redirect('/')
elif not c.user_id == g.user.id:
flash(u"This isn't your cloud!")
if request.method == 'POST':
#validate this biotch
if not request.form['label']:
flash(u'Error: All fields are required')
elif not request.form['endpoint']:
flash(u'Error: All fields are required')
elif not request.form['test_user']:
flash(u'Error: All fields are required')
elif not request.form['test_key']:
flash(u'Error: All fields are required')
elif not request.form['admin_endpoint']:
flash(u'Error: All fields are required')
elif not request.form['admin_user']:
flash(u'Error: All fields are required')
elif not request.form['admin_key']:
flash(u'Error: All fields are required')
else:
c.label = request.form['label']
c.endpoint = request.form['endpoint']
c.test_user = request.form['test_user']
c.test_key = request.form['test_key']
c.admin_endpoint = request.form['admin_endpoint']
c.admin_user = request.form['admin_user']
c.admin_key = request.form['admin_key']
db.commit()
flash(u'Cloud Saved!')
return redirect('/')
form = dict(label=c.label,
endpoint=c.endpoint,
test_user=c.test_user,
test_key=c.test_key,
admin_endpoint=c.admin_endpoint,
admin_user=c.admin_user,
admin_key=c.admin_key)
return render_template('edit_cloud.html',form=form)
@app.route('/create-cloud', methods=['GET', 'POST'])
def create_cloud():
"""This is the handler for creating a new cloud"""
#if g.user is None:
# abort(401)
if request.method == 'POST':
if not request.form['label']:
flash(u'Error: All fields are required')
elif not request.form['endpoint']:
flash(u'Error: All fields are required')
elif not request.form['test_user']:
flash(u'Error: All fields are required')
elif not request.form['test_key']:
flash(u'Error: All fields are required')
elif not request.form['admin_endpoint']:
flash(u'Error: All fields are required')
elif not request.form['admin_user']:
flash(u'Error: All fields are required')
elif not request.form['admin_key']:
flash(u'Error: All fields are required')
else:
c = Cloud()
c.user_id = g.user.id
c.label = request.form['label']
c.endpoint = request.form['endpoint']
c.test_user = request.form['test_user']
c.test_key = request.form['test_key']
c.admin_endpoint = request.form['admin_endpoint']
c.admin_user = request.form['admin_user']
c.admin_key = request.form['admin_key']
db.add(c)
db.commit()
return redirect('/')
return render_template('create_cloud.html', next_url='/')
@app.route('/profile/edit', methods=['GET', 'POST'])
def edit_profile():
"""Updates a profile"""
if g.user is None:
abort(401)
form = dict(name=g.user.name, email=g.user.email)
if request.method == 'POST':
if 'delete' in request.form:
db.delete(g.user)
db.commit()
session['openid'] = None
flash(u'Profile deleted')
return redirect(url_for('index'))
form['name'] = request.form['name']
form['email'] = request.form['email']
if not form['name']:
flash(u'Error: you have to provide a name')
elif '@' not in form['email']:
flash(u'Error: you have to enter a valid email address')
else:
flash(u'Profile successfully created')
g.user.name = form['name']
g.user.email = form['email']
db.commit()
return redirect(url_for('edit_profile'))
return render_template('edit_profile.html', form=form)
@app.route('/profile', methods=['GET', 'POST'])
def view_profile():
"""Updates a profile"""
if g.user is None:
abort(401)
return render_template('view_profile.html', user=g.user)
@app.route('/logout')
def logout():
"""logout route"""
session.pop('openid', None)
flash(u'You have been signed out')
return redirect(oid.get_next_url())
You can’t perform that action at this time.
