-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
144 lines (120 loc) · 4.52 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
from flask import Flask, render_template_string, render_template, request, session, redirect, url_for
import json
import csv
import plotlyExamples
# Create the Flask application
app = Flask(__name__)
# Details on the Secret Key: https://flask.palletsprojects.com/en/2.3.x/config/#SECRET_KEY
# NOTE: The secret key is used to cryptographically-sign the cookies used for storing
# the session data.
app.secret_key = 'BAD_SECRET_KEY'
myusers = [] #{'uid': 4, 'links': [666]}, {'uid': 666, 'links': [133,4]}, {'uid': 133, 'links': [666]}, {'uid': 555, 'links': [666, 133, 4, 123, 124, 125]}, {'uid': 125, 'links': [555, 128]}, {'uid': 128, 'links': [555]}, {'uid': 130, 'links': [555]}
participants = {}
@app.before_first_request
def execute_before_first_request():
f = open('dataNew.json')
global myusers
myusers = json.load(f)
f.close()
#myusers = myusers['users']
print(myusers['users'][0]["name"])
#fd = open('participants.csv', encoding="utf8")
#reader = csv.DictReader(fd)
#global participants
#participants["users"] = list(reader)
#print(participants["users"][0]["Full Name"])
@app.route('/set_pw', methods=['GET', 'POST'])
def set_pw():
global myusers
if request.method == 'POST':
# Save the form data to the session object
session['pw'] = request.form['pw']
# look up user, check pw
valid = False
pw = "abc"
c = 0
for u in myusers["users"]:
if u["id"] == session['id']:
print(u["id"])
pw = u["pw"]
session['name'] = u["name"]
session['index'] = c
valid = True
break
c+=1
if valid == True:
if pw == session['pw']:
if session.get('id') is not None:
print(str(session["id"]))
return redirect(url_for('main')+"?uid="+ str(session["id"]))
else:
return "invalid password"
else:
return "invalid uid"
return """
<form method="post">
<p>we will set a cookie to keep you logged in on this browser<br>
<input type="checkbox" name="consent" value = false required /> I agree<br>
<label for="email">Enter your code:</label>
<input type="text" name="pw" required />
<button type="submit">Submit</button
</form>
"""
@app.route('/main')
def main():
#global myusers
if session.get('pw') is not None:
#if session['email']:
print("cookie found " + str( session['id'] ))
uid = -1
if request.args.get('uid'):
uid = request.args.get('uid')
valid = False
name = ""
c = 0
for u in myusers["users"]:
if u["id"] == uid:
name = u["name"]
index = c
valid = True
break
c += 1
if valid == True:
thislink = [session["index"],c]
if thislink not in myusers["links"]:
myusers["links"].append(thislink)
myusers["users"][thislink[0]]["score"] += 1
with open('dataNew.json', 'w') as f:
json.dump(myusers, f)
f.close()
graphJSON = plotlyExamples.networkGraphRT(myusers)
barJson = plotlyExamples.barGraph(myusers)
return render_template("catwork.html", name=session["name"], user = name, uid = session["index"], otheruid = c, graphJSON=graphJSON, barJson=barJson)
else:
return "Invalid uid"
else:
print("no cookie found..redirecting to login")
if request.args.get('uid'):
session["id"] = request.args.get('uid')
valid = False
for u in myusers["users"]:
if u["id"] == session["id"]:
print(u["id"])
valid = True
#break
if valid == True:
return redirect(url_for('set_pw'))
else:
return "invalid uid"
else:
return "no uid"
@app.route('/logout')
def logout():
# Clear the email stored in the session object
session.pop('pw', default=None)
session.pop('id', default=None)
session.pop('name', default=None)
session.pop('index', default=None)
return '<h1>Session deleted!</h1>'
if __name__ == '__main__':
app.run()