-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
425 lines (337 loc) · 12.6 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
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
# This program sets up the server and routes requests to server to html or json data.
from flask import Flask, render_template, jsonify, request, redirect, flash, url_for, session, logging
from flask.ext.pymongo import PyMongo
# Used for Data from MongoDB ( Saving Data )
from bson import json_util
from bson.objectid import ObjectId
import json
import bson
from pymongo import Connection, MongoClient
# Used for forms
from flask_wtf.file import FileField, FileRequired
from flask_wtf import FlaskForm
from wtforms import Form, StringField, TextAreaField, PasswordField, validators
from werkzeug.utils import secure_filename
# Middleware used for data encryption and templates
from passlib.hash import sha256_crypt
from functools import wraps
# Used for the File upload function
UPLOAD_FOLDER = '/Users/Michael/Desktop/School/c3po_final/Gatoraid Server/temp'
ALLOWED_EXTENSIONS = set(['txt', 'pdf','tsv'])
import os
import os.path
import time
# Used for multithreading for file upload in add_regions route
from threading import Thread
from FileUpload import get_data
# First part of router is for website.
# Second part is the API used for CRUD and get data for other clients.
#Flask is the code used for routing, render_template to display html etc, file content
#functions
def toJson(data):
"""Convert Mongo object(s) to JSON"""
return json.dumps(data, default=json_util.default)
#initializes server object
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
MONGO_URL= "localhost"
MONGO_PORT = 27017
# Initializing Mongo
print "Connecting to DB: ", MONGO_URL,MONGO_PORT, "\n"
client = MongoClient(MONGO_URL,MONGO_PORT) # 27017 is the default port number for mongod
db = client.test
# Routes
# Main Route
@app.route('/')
def index():
return render_template('home.html')
# About
@app.route('/about')
def about():
return render_template('about.html')
# Register Form Class
class RegisterForm(Form):
name = StringField('Name', [validators.Length(min=1, max=50)])
username = StringField('Username', [validators.Length(min=4, max=25)])
email = StringField('Email', [validators.Length(min=6, max=50)])
password = PasswordField('Password', [
validators.DataRequired(),
validators.EqualTo('confirm', message='Passwords do not match.')
])
confirm = PasswordField('Confirm Password.')
# Register Route
@app.route('/register', methods=['GET', 'POST'])
def register():
form = RegisterForm(request.form)
if request.method == 'POST' and form.validate():
#Get user input from form
col = db.accounts
name = form.name.data
email = form.email.data
username = form.username.data
password = sha256_crypt.encrypt(str(form.password.data))
col.insert({'name' : name, 'email' : email, 'username' : username, 'password' : password})
flash('You are now logged in.', 'success')
return redirect(url_for('login'))
return render_template('register.html', form=form)
# login route
@app.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'POST':
#get from fields
col = db.accounts
# Gets form fields.
username = request.form['username']
password_candidate = request.form['password']
# Grabs first username from database and calls it data.
data = col.find_one({'username':username})
# If any rows in found, or in result.
if data > 0:
#Gets password from data
password = data['password']
try:
# Verifies password is correct with database.
if sha256_crypt.verify(password_candidate, password):
session['logged_in'] = True
session['username'] = username
flash('Success!', 'success')
return redirect(url_for('dashboard'))
else:
flash('Invalid Password.', 'danger')
return redirect(url_for('login'))
except:
app.logger.info("error")
else:
flash('Invalid username.', 'danger')
return redirect(url_for('login'))
return render_template('login.html')
# Check if user is logged in
def is_logged_in(f):
@wraps(f)
def wrap(*args, **kwargs):
if 'logged_in' in session:
return f(*args, **kwargs)
else:
flash('Unauthorized, Please login', 'danger')
return redirect(url_for('login'))
return wrap
# Logout
@app.route('/logout')
def logout():
session.clear()
flash('You are now logged out!', 'success')
return redirect(url_for('login'))
# Dashboard
@app.route('/dashboard')
@is_logged_in
def dashboard():
return render_template('dashboard.html')
# Region Form Class
class RegionForm(Form):
name = StringField('name', [validators.Length(min=3, max=50)])
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
# Add Region
@app.route('/add_region', methods=['GET', 'POST'])
@is_logged_in
def add_region():
col = db.c3po
form = RegionForm(request.form)
# IF POST REQUEST
if request.method == 'POST':
# IF SUBMIT BY NAME
if request.form['btn'] == 'Submit' and form.validate():
name = form.name.data
col.insert({'name' : name})
flash('Region Created.','success')
return redirect(url_for('dashboard'))
else:
# CHECK IF UPLOAD BY FILE
if request.form['btn'] == 'Upload':
file = request.files['file']
# if user does not select file, browser also
# submit a empty part without filename
if file.filename == '':
flash('No selected file', 'danger')
return redirect(url_for('add_region'))
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
app.logger.info("filename")
app.logger.info(filename)
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
while not os.path.exists('/Users/Michael/Desktop/School/c3po_final/Gatoraid Server/temp/'+filename):
flash("waiting for file...",'success')
app.logger.info("waiting for file...")
time.sleep(1)
if os.path.isfile('/Users/Michael/Desktop/School/c3po_final/Gatoraid Server/temp/'+filename):
app.logger.info("loading thread...")
flash("loading file...",'success')
result = Thread( target=get_data, args=(filename,) )
result.start()
flash("file being proccessed...",'success')
else:
return render_template('add_region.html', form=form)
# IF GET REQUEST
return render_template('add_region.html', form=form)
# Used for the Region data
class RegionForm(Form):
searchkey = StringField('searchkey', [validators.Length(min=1, max=50)])
searchfield = StringField('searchfield', [validators.Length(min=1, max=50)])
obj2ID = StringField('_id', [validators.Length(min=1, max=50)])
objID = StringField('_id', [validators.Length(min=1, max=50)])
MagR = StringField('MagR', [validators.Length(min=1, max=25)])
bib = StringField('bib', [validators.Length(min=1, max=25)])
MagV = StringField('MagV', [validators.Length(min=1, max=25)])
MagU = StringField('MagU', [validators.Length(min=1, max=25)])
MagI = StringField('MagI', [validators.Length(min=1, max=25)])
nnot = StringField('not', [validators.Length(min=1, max=25)])
MagB = StringField('MagB', [validators.Length(min=1, max=25)])
DEC = StringField('DEC', [validators.Length(min=1, max=25)])
Type = StringField('Type', [validators.Length(min=1, max=25)])
Identifier = StringField('Identifier', [validators.Length(min=1, max=25)])
name = StringField('name', [validators.Length(min=1, max=25)])
RA = StringField('RA', [validators.Length(min=1, max=25)])
spec = StringField('spec', [validators.Length(min=1, max=25)])
# Edit Region
@app.route('/edit_region')
@is_logged_in
def edit_regions():
form = RegionForm(request.form)
username = form.username.data
col = db.c3po
cursor = col.find()
return render_template('regions.html', region = doc)
# Display all Regions page
@app.route('/regions')
def regions():
col = db.c3po
doc = col.find()
return render_template('regions.html', region = doc)
# Edit Regions
@app.route('/region/edit/', methods=['GET', 'POST'])
@is_logged_in
def Editregions():
form = RegionForm(request.form)
if request.method == 'POST':
name = form.name.data
col = db.c3po
names = col.find({'name':name}, {'GatorObjects':0})
a=[]
if request.form['btn'] == 'Upload':
idd = form.objID.data
Identifier = form.Identifier.data
form = RegionForm(request.form)
name = form.name.data
app.logger.info(Identifier)
add_id = col.update(
{'name':bson.ObjectId(oid=str(idd))},
{
"$set": {
"name":name,
"Identifier":Identifier
}
}
)
i=0
for name in names:
i+=1
a.append(name)
count = col.find({'name':name}).count()
if i>0:
app.logger.info(count)
flash('Found '+str(i) +' region(s) with that name.', 'success')
return render_template('edit_region.html', form=form, name=name)
else:
flash('Found '+str(count) +' region with that name', 'danger')
return render_template('edit_region.html', form=form, name=name)
# doc=col.find_one({'_id':bson.ObjectId(oid=str(id))})
return render_template('edit_region.html', form=form)
# Dynamic routing for regions
@app.route('/region/<string:id>/')
def region(id):
col = db.c3po
doc=col.find_one({'_id':bson.ObjectId(oid=str(id))})
return render_template('region.html', id=id, region=doc)
# Dynamic routing for stars
@app.route('/region/<string:id>/star/<test>')
def GatorObjects(id,test):
col = db.c3po
doc=col.find_one({'_id':bson.ObjectId(oid=str(id))})
# stardoc = col.find({{ 'GatorObjects.id': star_iden }})
return render_template('gatorObjects.html', id=id, region=doc, test=test)
# BACKEND API
# Get all names
@app.route('/api/name', methods=['GET'])
def get_all_names():
col = db.c3po
results = col.find()
json_results = []
for result in results:
json_results.append(result)
return toJson(json_results)
# Get all Stars
@app.route('/api/stars', methods=['GET'])
def get_all_stars():
col = db.c3po
results = col.find()
json_results = []
for result in results:
json_results.append(result)
return toJson(json_results)
# Get JSON by region name
@app.route('/api/region_by_name/<string:name>')
def APIgetRegionByName(name):
col = db.c3po
regions = col.find_one({'name':name})
json_results =[]
for region in regions:
json_results.append(region)
return toJson(regions)
# Add a named region.
@app.route('/api/addregion', methods=['POST'])
def add_star():
add = db.c3po
name = request.json['name']
language = request.json['language']
add_id = add.insert({'name' : name, 'language' : language})
new_add = add.find_one({'_id' : add_id})
output = {'name' : new_add['name'], 'language' : new_add['language']}
return jsonify({'result': output})
# This is used to add stars to each named region.
@app.route('/api/add/object/<string:id>', methods=['PUT'])
def ApiEditstars(id):
col = db.c3po
pol = db.c3po
name = request.json['name']
results = pol.find()
json_results = []
for result in results:
json_results.append(result)
add_id = col.update(
{'_id':bson.ObjectId(oid=str(id))},
{
"$set": {
"name":name,
"GatorObject": json_results
}
}
)
output = {'result' : add_id}
return jsonify({'result': output})
# Search a star by RA and DEC
@app.route('/api/stars/<ra>/<dec>', methods=['GET'])
def get_one_Star(ra, dec):
col = db.c3po
output= {}
q=col.find_one({ 'ra': ra, 'dec': dec })
if q:
output=col.find_one({ 'ra': ra, 'dec': dec })
else:
output='No Results found.'
return toJson(output)
# By Michael Kaufman
# Executes server
if __name__ == '__main__':
app.secret_key='123'
app.run(debug=True)