-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.py
More file actions
630 lines (559 loc) · 24.5 KB
/
app.py
File metadata and controls
630 lines (559 loc) · 24.5 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
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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
from flask import Flask, render_template, redirect, session, url_for, flash, request, jsonify
from data.db_session import db_auth
#from services.accounts_service import create_user, login_user, get_profile, update_profile
from services.accounts_service import *
from services.project_service import *
from services.schedule_service import *
from services.friend_service import *
from services.target_service import *
from services.postgres_service import *
from services.log_service import *
import os
import random
app = Flask(__name__) #create application
app.secret_key = os.urandom(24)
graph = db_auth() #connect to neo4j
@app.route('/')
def index():
return render_template("home/index.html")
@app.route('/accounts/register', methods=['GET'])
def register_get():
return render_template("accounts/register.html")
@app.route('/accounts/register', methods=['POST'])
def register_post():
# Get the form data from register.html
username = request.form.get('username').strip()
name = request.form.get('name')
email = request.form.get('email').lower().strip()
affiliation = request.form.get('affiliation').strip()
title = request.form.get('title').strip()
country = request.form.get('country').strip()
password = request.form.get('password').strip()
confirm = request.form.get('confirm').strip()
# Check for blank fields in the registration form
if not username or not name or not email or not affiliation or not title or not country or not password or not confirm:
flash("Please populate all the registration fields"+country, "error")
return render_template("accounts/register.html", username=username, name=name, email=email, affiliation=affiliation, title=title, country=country, password=password, confirm=confirm)
# Check if password and confirm match
if password != confirm:
flash("Passwords do not match")
return render_template("accounts/register.html", username=username, name=name, email=email, affiliation=affiliation, title=title, country=country)
# Create the user
user = create_user(username, name, email, affiliation, title, country, password)
# Verify another user with the same email does not exist
if not user:
flash("A user with that email already exists.")
return render_template("accounts/register.html", username=username, name=name, email=email, affiliation=affiliation, title=title, country=country)
return redirect(url_for("dashboard_get"))
@app.route('/accounts/login', methods=['GET'])
def login_get():
# Check if the user is already logged in. if yes, redirect to profile page.
if "usr" in session:
return redirect(url_for("dashboard_get"))
else:
return render_template("accounts/login.html")
@app.route('/accounts/login', methods=['POST'])
def login_post():
# Get the form data from login.html
email = request.form['email']
password = request.form['password']
if not email or not password:
return render_template("accounts/login.html", email=email, password=password)
# Validate the user
user = login_user(email, password)
if not user:
flash("No account for that email address or the password is incorrect", "error")
return render_template("accounts/login.html", email=email)
# Log in user and create a user session, redirect to user profile page.
usr = request.form["email"]
session["usr"] = usr
return redirect(url_for("dashboard_get"))
@app.route('/accounts/map.html', methods=['GET'])
def map_get():
if "usr" in session:
usr = session["usr"]
session["usr"] = usr
return render_template("accounts/map.html")
else:
return redirect(url_for("login_get"))
@app.route('/accounts/map.html', methods=['POST'])
def map_post():
if "usr" in session:
usr = session["usr"]
session["usr"] = usr
return render_template("accounts/map.html")
else:
return redirect(url_for("login_get"))
@app.route('/accounts/friends', methods=['GET'])
def viewFriends():
if "usr" in session:
usr = session["usr"]
session["usr"] = usr
friends = view_friend(usr)
user_profile = get_profile(usr)
return render_template("accounts/friends.html", friends=friends, user_profile=user_profile )
else:
return redirect(url_for("login_get"))
@app.route('/accounts/index', methods=['GET'])
def dashboard_get():
if "usr" in session:
usr = session["usr"]
session["usr"] = usr
user_profile = get_profile(usr)
projects = get_project(usr)
# get recommended users from postgresSQL
uid_list1 = recommend_user_by_equipment(str(get_uid(usr)))
random.shuffle(uid_list1)
new_uid_list1 = []
L1 = []
for uid in uid_list1:
if check_is_friend(usr, uid[0]) == 0:
new_uid_list1.append(uid)
L1 = get_user_info(new_uid_list1)
L1 = L1[:3]
# get recommended users from the users share the same interest target
# 1. get user interested targets
targets = get_user_interest(usr)
random.shuffle(targets)
# 2. get a non-friend user from each target with max 6 users
uid_list2 = []
L2 = []
for t in targets:
uid = get_a_new_user(usr, int(t['TID']))
if uid != -1:
uid_list2.append([uid])
if len(uid_list2) > 3:
break
print(uid_list2)
if len(uid_list2) != 0:
L2 = get_user_info(uid_list2)
if len(L2) > 3:
L2 = L2[:3]
# get recommended users from the users in the same project
# filter out the users that already is friend
recommended_user = L1+L2
uid_rest = []
if len(recommended_user) < 6:
need = 6-len(recommended_user)
for i in range(need):
while True:
uid = random.randint(0, count_user())
if uid != get_uid(usr):
break
uid_rest.append([uid])
L3 = get_user_info(uid_rest)
recommended_user = L1+L2+L3
return render_template("accounts/index.html", user_profile=user_profile, projects=projects, recommended_user=recommended_user)
else:
return redirect(url_for("login_get"))
#created for ajax to join project for project on dashboard
@app.route('/joinProject', methods=['POST'])
def joinProject():
if "usr" in session:
usr = session["usr"]
session["usr"] = usr
PID = request.form.get('PID').strip()
auto_join(usr,int(PID))
return jsonify(success = "Successfully joined the project.")
else:
return redirect(url_for("login_get"))
@app.route('/accounts/joinedProjects', methods=['GET'])
def getJoinedProjects():
if "usr" in session:
usr = session["usr"]
session["usr"] = usr
user_profile = get_profile(usr)
projects = get_project_join(usr)
return render_template("accounts/joinedProjects.html", user_profile=user_profile, projects = projects)
else:
return redirect(url_for("login_get"))
@app.route('/accounts/manageprojects', methods=['GET'])
def manageProject():
if "usr" in session:
usr = session["usr"]
session["usr"] = usr
user_profile = get_profile(usr)
projects = user_manage_projects_get(usr)
return render_template("accounts/manageprojects.html", user_profile=user_profile, projects = projects)
else:
return redirect(url_for("login_get"))
# 0331 show the ranking of users' joined projects
@app.route('/accounts/rankedProjects', methods=['GET'])
def getRankedProjects():
# get user customized ranking frome database
if "usr" in session:
usr = session["usr"]
session["usr"] = usr
user_profile = get_profile(usr)
projects = get_project_orderby_priority(usr)
return render_template("accounts/joinedProjects.html", user_profile=user_profile, projects = projects)
else:
return redirect(url_for("login_get"))
# 0331 for users to rank their joined projects
@app.route('/accounts/rankedProjects', methods=['POST'])
def postRankedProjects():
# get user customized ranking
''' TODO '''
pid_list = []
# expected a PID list
if "usr" in session:
usr = session["usr"]
session["usr"] = usr
user_profile = get_profile(usr)
# only when user click the save button would update the database
if request.form.get('button') == 'save':
upadte_project_priority(usr, pid_list)
if request.form.get('button') == 'reset':
projects = get_project_join(usr)
projects = get_project_default_priority(projects)
return render_template("accounts/joinedProjects.html", user_profile=user_profile, projects = projects)
else:
return redirect(url_for("login_get"))
#created for ajax to get target for projects on dashboard
@app.route('/getTargetInfo', methods=['POST'])
def getTargetInfo():
if "usr" in session:
usr = session["usr"]
session["usr"] = usr
hid = request.form.get('PID').strip()
project_target = fliter_project_target(usr, int(hid))
return jsonify(project_targets = project_target)
else:
return redirect(url_for("login_get"))
@app.route('/getTargetForProjectInfo', methods=['POST'])
def getTargetForProjectInfo():
if "usr" in session:
usr = session["usr"]
session["usr"] = usr
hid = request.form.get('PID').strip()
project_target = get_project_target(int(hid))
return jsonify(project_targets = project_target)
else:
return redirect(url_for("login_get"))
@app.route('/getJoinedEquipmentInfo', methods=['POST'])
def getJoinedEquipmentInfo():
hid = request.form.get('PID').strip()
project_equipments = get_project_equipment(int(hid))
return jsonify(project_equipments = project_equipments)
@app.route('/addfriend', methods=['POST'])
def addFriend():
if "usr" in session:
usr = session["usr"]
session["usr"] = usr
uid = request.form.get('UID').strip()
add_friend(usr, int(uid))
return jsonify(success = "success")
else:
return redirect(url_for("login_get"))
@app.route('/getPMInfo', methods=['POST'])
def getPMInfo():
hid = request.form.get('PID').strip()
project_manager = get_project_manager_name(int(hid))
return jsonify(project_manager = project_manager)
@app.route('/accounts/profile', methods=['GET'])
def profile_get():
# Make sure the user has an active session. If not, redirect to the login page.
if "usr" in session:
usr = session["usr"]
session["usr"] = usr
user_profile = get_profile(usr)
return render_template("accounts/profile.html", user_profile=user_profile)
else:
return redirect(url_for("login_get"))
@app.route('/accounts/profile', methods=['POST'])
def profile_post():
# Get the data from index.html
username = request.form.get('username').strip()
name = request.form.get('name').strip()
affiliation = request.form.get('affiliation').strip()
title = request.form.get('title').strip()
country = request.form.get('country').strip()
# Make sure the user has an active session. If not, redirect to the login page.
if "usr" in session:
usr = session["usr"]
session["usr"] = usr
user_profile = update_profile(usr, username, name, affiliation, title, country)
user_profile = get_profile(usr)
return render_template("accounts/profile.html", user_profile=user_profile)
else:
return redirect(url_for("login_get"))
@app.route('/accounts/interests', methods=['GET'])
def viewInterest():
if "usr" in session:
usr = session["usr"]
session["usr"] = usr
user_profile = get_profile(usr)
interests = get_user_interest(usr)
return render_template("accounts/interests.html", user_profile=user_profile, interests=interests)
else:
return redirect(url_for("login_get"))
@app.route('/accounts/addInterest', methods=['POST'])
def addInterest():
if "usr" in session:
usr = session["usr"]
session["usr"] = usr
TID = request.form.get('TID')
create_user_target(usr, int(TID))
return jsonify(success = "Success")
else:
return redirect(url_for("login_get"))
@app.route('/accounts/deleteInterest', methods=['POST'])
def delInterest():
if "usr" in session:
usr = session["usr"]
session["usr"] = usr
TID = request.form.get('TID')
delete_user_insterest(usr, int(TID))
return jsonify(success = "Success")
else:
return redirect(url_for("login_get"))
@app.route('/accounts/createProject', methods=['GET'])
def createProj_get():
# Make sure the user has an active session. If not, redirect to the login page.
if "usr" in session:
usr = session["usr"]
session["usr"] = usr
user_profile = get_profile(usr)
projects = get_project(usr)
return render_template("accounts/createProject.html", user_profile=user_profile, projects = projects)
else:
return redirect(url_for("login_get"))
@app.route('/accounts/equipments', methods=['GET'])
def equipments_get():
if "usr" in session:
usr = session["usr"]
session["usr"] = usr
user_profile = get_profile(usr)
count = count_user_equipment(usr)
if count == 0:
flash("You don't have any equipment yet! Please add an equipment first", "error")
return render_template("accounts/equipments.html", user_equipments = None)
user_equipments = get_user_equipments(usr)
return render_template("accounts/equipments.html", user_profile=user_profile, user_equipments = user_equipments)
else:
return redirect(url_for("login_get"))
@app.route('/accounts/equipments', methods=['POST'])
def equipments_post():
# user equipment parameter
Site = request.form.get('site').strip()
Longitude = request.form.get('longitude').strip()
Latitude = request.form.get('latitude').strip()
Altitude = request.form.get('altitude').strip()
tz = request.form.get('time_zone').strip()
daylight = request.form.get('daylight_saving').strip()
wv = request.form.get('water_vapor').strip()
light_pollution = request.form.get('light_pollution').strip()
#equipments parameter
aperture = request.form.get('aperture').strip()
Fov = request.form.get('fov').strip()
pixel_scale = request.form.get('pixel').strip()
tracking_accuracy = request.form.get('accuracy').strip()
lim_magnitude = request.form.get('mag').strip()
elevation_lim = request.form.get('deg').strip()
mount_type = request.form.get('mount_type').strip()
camera_type1 = request.form.get('camera_type1').strip()
camera_type2 = request.form.get('camera_type2').strip()
JohnsonB = request.form.get('JohnsonB').strip()
JohnsonV = request.form.get('JohnsonV').strip()
JohnsonR = request.form.get('JohnsonR').strip()
SDSSu = request.form.get('SDSSu').strip()
SDSSg = request.form.get('SDSSg').strip()
SDSSr = request.form.get('SDSSr').strip()
SDSSi = request.form.get('SDSSi').strip()
SDSSz = request.form.get('SDSSz').strip()
if "usr" in session:
usr = session["usr"]
session["usr"] = usr
if request.form.get('button') == 'update':
hid = request.form.get('uhaveid').strip()
print(hid)
user_equipments = update_user_equipments(aperture,Fov,pixel_scale,tracking_accuracy,lim_magnitude,elevation_lim,mount_type,camera_type1,
camera_type2,JohnsonB,JohnsonR,JohnsonV,SDSSu,SDSSg,SDSSr,SDSSi,SDSSz,
usr,Site,Longitude,Latitude,Altitude,tz,daylight,wv,light_pollution,int(hid))
if request.form.get('button') == 'add':
equipments = create_equipments(aperture,Fov,pixel_scale,tracking_accuracy,lim_magnitude,elevation_lim,mount_type,camera_type1,camera_type2,JohnsonB,JohnsonR,JohnsonV,SDSSu,SDSSg,SDSSr,SDSSi,SDSSz)
print(equipments.EID)
user_equipments = create_user_equipments(usr,equipments.EID,Site,Longitude,Latitude,Altitude,tz,daylight,wv,light_pollution)
# create spatial user equipment
# postgres_create_user_equipments(int(user_equipments.id),get_uid(usr), equipments.EID,Longitude,Latitude,Altitude)
if request.form.get('button') == 'delete':
hid = request.form.get('uhaveid').strip()
delete_user_equipment(usr,int(hid))
# delete user's equipment in postgresSQL
postgres_delete_user_equipments(int(hid))
user_equipments = get_user_equipments(usr)
return render_template("accounts/equipments.html", user_equipments = user_equipments)
else:
return redirect(url_for("login_get"))
@app.route('/projects/target', methods=['GET'])
def target_get():
if "usr" in session:
usr = session["usr"]
session["usr"] = usr
target = get_target()
return render_template("projects/target.html", target = target)
else:
return redirect(url_for("login_get"))
@app.route('/projects/target', methods=['POST'])
def target_post():
if "usr" in session:
usr = session["usr"]
session["usr"] = usr
target = get_target()
return render_template("projects/target.html", target = target)
else:
return redirect(url_for("login_get"))
@app.route('/projects/search', methods=['GET'])
def target_search_get():
return render_template("projects/search_target.html")
# 0703 change the function to query_from_simbad
@app.route('/projects/search', methods=['POST'])
def target_search_post():
text = request.form.get('search').strip()
# text = '(?i).*'+text+'.*'
print(text)
# if request.form.get('button') == 'Search':
# target = search_target(text)
target = query_from_simbad(text)
#return render_template("projects/search_target.html", target = target)
return jsonify(target = target)
@app.route('/projects/targetDetails', methods=['POST'])
def target_getDetails():
name = request.form.get('targetName')
target = get_targetDetails(name)
return jsonify(targetDetails = target)
@app.route('/projects/project', methods=['GET'])
def project_get():
if "usr" in session:
usr = session["usr"]
session["usr"] = usr
projects = get_project(usr)
return render_template("projects/project.html", projects = projects)
else:
return redirect(url_for("login_get"))
@app.route('/projects/project', methods=['POST'])
def project_post():
hid = request.form.get('PID').strip()
if "usr" in session:
usr = session["usr"]
session["usr"] = usr
if request.form.get('button') == 'Detail':
print(hid)
project_target = get_project_target(int(hid))
return render_template("projects/project_target.html", project_target = project_target)
elif request.form.get('button') == 'Create':
return redirect(url_for("project_create_get"))
elif request.form.get('button') == 'Apply_history':
print('history')
return redirect(url_for("project_apply_history_get"))
elif request.form.get('button') == 'Join':
PID = request.form.get('PID').strip()
flag = apply_project_status(usr,int(PID))
if flag == 1:
apply_project(usr,int(PID))
#elif flag == 2:
# handle already apply
#elif flag == 3:
#handle already join project
#else:
# handle error
projects = get_project(usr)
return render_template("projects/project.html", projects = projects)
else:
return redirect(url_for("login_get"))
@app.route('/projects/project_apply_history', methods=['GET'])
def project_apply_history_get():
if "usr" in session:
usr = session["usr"]
session["usr"] = usr
history = get_apply_history(usr)
return render_template("projects/project_apply_history.html", history = history)
else:
return redirect(url_for("login_get"))
@app.route('/projects/project_create', methods=['GET'])
def project_create_get():
if "usr" in session:
usr = session["usr"]
session["usr"] = usr
projects = user_manage_projects_get(usr)
return render_template("projects/project_create.html", projects = projects)
else:
return redirect(url_for("login_get"))
@app.route('/accounts/createProject', methods=['POST'])
def project_create_post():
title = request.form.get('title').strip()
project_type = request.form.get('project_type').strip()
description = request.form.get('description').strip()
aperture_upper_limit = request.form.get('aperture_upper_limit').strip()
aperture_lower_limit = request.form.get('aperture_lower_limit').strip()
FoV_upper_limit = request.form.get('FoV_upper_limit').strip()
FoV_lower_limit = request.form.get('FoV_lower_limit').strip()
pixel_scale_upper_limit = request.form.get('pixel_scale_upper_limit').strip()
pixel_scale_lower_limit = request.form.get('pixel_scale_lower_limit').strip()
mount_type = request.form.get('mount_type').strip()
camera_type1 = request.form.get('camera_type1').strip()
camera_type2 = request.form.get('camera_type2').strip()
JohnsonB = request.form.get('JohnsonB').strip()
JohnsonV = request.form.get('JohnsonV').strip()
JohnsonR = request.form.get('JohnsonR').strip()
SDSSu = request.form.get('SDSSu').strip()
SDSSg = request.form.get('SDSSg').strip()
SDSSr = request.form.get('SDSSr').strip()
SDSSi = request.form.get('SDSSi').strip()
SDSSz = request.form.get('SDSSz').strip()
# PID = request.form.get('PID').strip()
# PI = request.form.get('PI').strip()
if "usr" in session:
usr = session["usr"]
session["usr"] = usr
#if request.form.get('button') == 'Create':
print('create project')
projects = create_project(usr,title,project_type,description,aperture_upper_limit,aperture_lower_limit,FoV_upper_limit,FoV_lower_limit,pixel_scale_upper_limit,pixel_scale_lower_limit,mount_type,camera_type1,camera_type2,JohnsonB,JohnsonR,JohnsonV,SDSSu,SDSSg,SDSSr,SDSSi,SDSSz)
# if request.form.get('button') == 'Update':
# umanageid = request.form.get('umanageid').strip()
# projects = upadte_project(usr,int(PID),int(umanageid),title,project_type,description,aperture_upper_limit,aperture_lower_limit,FoV_upper_limit,FoV_lower_limit,pixel_scale_upper_limit,pixel_scale_lower_limit,mount_type,camera_type1,camera_type2,JohnsonB,JohnsonR,JohnsonV,SDSSu,SDSSg,SDSSr,SDSSi,SDSSz)
# if request.form.get('button') == 'Delete':
# umanageid = request.form.get('umanageid').strip()
# delete_project(usr,int(PID),int(umanageid))
# projects = user_manage_projects_get(usr)
return jsonify(projects = projects.PID)
else:
return redirect(url_for("login_get"))
@app.route('/projects/addTarget', methods=['POST'])
def addTarget():
PID = request.form.get('PID').strip()
TID = request.form.get('TID').strip()
JohnsonB = request.form.get('JohnsonB').strip()
JohnsonV = request.form.get('JohnsonV').strip()
JohnsonR = request.form.get('JohnsonR').strip()
SDSSu = request.form.get('SDSSu').strip()
SDSSg = request.form.get('SDSSg').strip()
SDSSr = request.form.get('SDSSr').strip()
SDSSi = request.form.get('SDSSi').strip()
SDSSz = request.form.get('SDSSz').strip()
if "usr" in session:
usr = session["usr"]
session["usr"] = usr
target = create_project_target(usr,int(PID),int(TID),JohnsonB, JohnsonR, JohnsonV, SDSSu, SDSSg, SDSSr, SDSSi, SDSSz)
return jsonify(target = target)
else:
return redirect(url_for("login_get"))
# @app.route('/projects/project', methods=['POST'])
# def project_post():
# hid = request.form.get('PID').strip()
# if "usr" in session:
# usr = session["usr"]
# session["usr"] = usr
# if request.form.get('button') == 'Detail':
# print(hid)
# project_target = get_project_target(int(hid))
# return render_template("projects/project_target.html", project_target = project_target)
# else:
# return redirect(url_for("login_get"))
@app.route('/accounts/logout')
def logout():
session.pop("usr", None)
flash("You have successfully been logged out.", "info")
return redirect(url_for("login_get"))
if __name__ == '__main__':
app.run(debug=True)