-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
336 lines (312 loc) · 12.9 KB
/
server.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
from flask import Flask, session, request, render_template, redirect, make_response
from flask_cors import CORS, cross_origin
from replit import db
from shortuuid import ShortUUID
from random import randint
from modules import *
import os
app = Flask("app", static_url_path="")
cors = CORS(app, resources={r"/*/*/json": {"origins": "*"}})
app.config["SECRET_KEY"] = os.getenv("secretkey")
@app.route("/")
def index():
if loggedIn():
recent = list(db[getUser()]["surveys"].keys())
recent.reverse()
recentlength = len(recent)
if recentlength > 3:
recent = [recent[0], recent[1], recent[2]]
return render_template("index.html", loggedIn=True, username=getUser(), recent=recent, recentlength=recentlength)
return render_template("index.html", loggedIn=False)
@app.route("/login")
def login():
return redirect("/") if loggedIn() else render_template("login.html", error=False)
@app.route("/login", methods=["GET", "POST"])
def loginsubmit():
if loggedIn():
return redirect("/")
if request.method == "POST":
username = request.form["username"]
password = request.form["password"]
if username not in db.keys() or db[username]["password"] != password:
return render_template("login.html", error="Invalid username and/or password.")
session["username"] = username
return redirect("/")
@app.route("/signup")
def signup():
return redirect("/") if loggedIn() else render_template("signup.html", error=False)
@app.route("/signup", methods=["GET", "POST"])
def createaccount():
if loggedIn():
return redirect("/")
if request.method == "POST":
newusername = request.form["newusername"]
newpassword = request.form["newpassword"]
captcha_response = request.form["g-recaptcha-response"]
print(newusername)
for i in newusername:
if i not in allchars:
return render_template("signup.html", error="Username can only contain alphanumeric characters and underscores.")
if newusername in db.keys():
return render_template("signup.html", error="Username taken.")
if newusername == "" or newpassword == "":
return render_template("signup.html", error="All fields are required.")
if is_human(captcha_response):
db[newusername] = {
"password":newpassword,
"surveys":{}
}
session["username"] = newusername
print("new account created")
return redirect("/")
else:
print("not verified")
return render_template("signup.html", error="No bots allowed!")
@app.route("/surveys")
def surveys():
if not loggedIn():
return redirect("/login")
allsurveys = list(db[getUser()]["surveys"].keys())
allsurveys.reverse()
return render_template("survey/surveys.html", surveys=allsurveys, username=getUser(), loggedIn=True)
@app.route("/newsurvey", methods=["GET", "POST"])
def newsurvey():
if not loggedIn():
return redirect("/")
if request.method == "POST":
name = request.json["name"]
if len(db[getUser()]["surveys"]) >= max_surveys:
print("sss")
return "You have reached the limit for the number of surveys you can create."
for i in name:
if i not in allchars + ["-"]:
return "The name can only contain alphanumeric characters, dashes, and underscores."
if name in db[getUser()]["surveys"]:
return "This survey already exists."
if len(name) > max_survey_length:
return "The name cannot have more than 20 characters."
db[getUser()]["surveys"][name] = {
"public":True,
"ended":False,
"inputfields":[],
"apikey":ShortUUID().random(randint(10, 25)),
"data":{}
}
return "Success"
@app.route("/<username>/<survey>/dashboard")
def dashboard(username, survey):
if username not in db.keys() or survey not in db[username]["surveys"]:
return render_template("404.html", loggedIn=loggedIn()), 404
if not loggedIn():
return redirect("/login")
if getUser() != username:
return render_template("404.html", loggedIn=loggedIn()), 404
info = db[username]["surveys"][survey]
return render_template("survey/dashboard.html", data=dict(info["data"]), ended=info["ended"], public=info["public"], inputfields=list(info["inputfields"]), loggedIn=True, length=len(info["inputfields"]), survey=survey)
@app.route("/<username>/<survey>/json")
@cross_origin()
def json(username, survey):
if username not in db.keys() or survey not in db[username]["surveys"]:
return render_template("404.html", loggedIn=loggedIn()), 404
info = db[username]["surveys"][survey]
if not info["public"]:
if not loggedIn():
return redirect("/login")
if getUser() != username:
return render_template("404.html", loggedIn=loggedIn()), 404
resp = make_response(Dict(info["data"]))
resp.mimetype = "application/json"
return resp
@app.route("/<username>/<survey>/downloadjson")
def downloadjson(username, survey):
if username not in db.keys() or survey not in db[username]["surveys"]:
return render_template("404.html", loggedIn=loggedIn()), 404
info = db[username]["surveys"][survey]
if not info["public"]:
if not loggedIn():
return redirect("/login")
if getUser() != username:
return render_template("404.html", loggedIn=loggedIn()), 404
resp = make_response(Dict(info["data"]))
resp.mimetype = "application/json"
resp.headers['Content-Disposition'] = f"attachment; filename={survey}.json"
return resp
@app.route("/<username>/<survey>/csv")
def csv(username, survey):
if username not in db.keys() or survey not in db[username]["surveys"]:
return render_template("404.html", loggedIn=loggedIn()), 404
info = db[username]["surveys"][survey]
if not loggedIn():
return redirect("/login")
if getUser() != username:
return render_template("404.html", loggedIn=loggedIn()), 404
lines = []
fields = list(info["inputfields"])
fields = ["#"] + fields
fields = ", ".join(fields)
lines.append(fields)
for i in info["data"]:
line = list(dict(info["data"][i]).values())
line = [i] + line
line = ", ".join(line)
lines.append(line)
total = "\n".join(lines)
resp = make_response(total)
resp.mimetype = "text/csv"
resp.headers['Content-Disposition'] = f"attachment; filename={survey}.csv"
return resp
@app.route("/<username>/<survey>/rename", methods=["GET", "POST"])
def rename(username, survey):
if request.method == "POST":
if username not in db.keys() or survey not in db[username]["surveys"]:
return render_template("404.html", loggedIn=loggedIn()), 404
if not loggedIn() or getUser() != username:
return render_template("404.html", loggedIn=loggedIn()), 404
name = request.json["name"]
for i in name:
if i not in allchars + ["-"]:
return "The name can only contain alphanumeric characters, dashes, and underscores."
if name in db[getUser()]["surveys"]:
return "This survey already exists."
if len(name) > 20:
return "The name cannot have more than 20 characters."
info = db[username]["surveys"][survey]
del db[username]["surveys"][survey]
db[username]["surveys"][name] = info
return "Success"
@app.route("/<username>/<survey>/delete", methods=["GET", "POST"])
def delete(username, survey):
if username not in db.keys() or survey not in db[username]["surveys"]:
return render_template("404.html", loggedIn=loggedIn()), 404
if not loggedIn() or getUser() != username:
return render_template("404.html", loggedIn=loggedIn()), 404
if request.method == "POST":
del db[username]["surveys"][survey]
return "Success"
@app.route("/<username>/<survey>/script")
def script(username, survey):
if username not in db.keys() or survey not in db[username]["surveys"]:
return render_template("404.html", loggedIn=loggedIn()), 404
resp = make_response(render_template("survey/script.js", apikey=db[username]["surveys"][survey]["apikey"]))
resp.mimetype = "text/javascript"
return resp
@app.route("/<username>/<survey>/submit", methods=["GET", "POST"])
def submit(username, survey):
if username not in db.keys() or survey not in db[username]["surveys"]:
return render_template("404.html", loggedIn=loggedIn()), 404
if request.method == "POST":
info = db[username]["surveys"][survey]
form = dict(request.form)
if info["ended"]:
print("survey already ended")
return {"success":"false", "error":"Survey has already been ended."}
if "apikey" not in form or form["apikey"] != info["apikey"]:
print("invalid api key")
return {"success":"false", "error":"Invalid API key."}
redirectvar = False
if "redirecturl" in form:
redirecturl = form["redirecturl"]
del form["redirecturl"]
redirectvar = True
for i in form.copy():
if i not in info["inputfields"]:
del form[i]
for i in info["inputfields"]:
if i not in form:
print("missing input fields")
return {"success":"false", "error":"Missing input fields"}
number = len(info["data"]) + 1
db[username]["surveys"][survey]["data"][number] = form
db[username]["surveys"][survey]["apikey"] = ShortUUID().random(randint(10, 25))
if redirectvar:
return redirect(redirecturl)
return {"success":"true"}
return render_template("404.html", loggedIn=loggedIn()), 404
@app.route("/<username>/<survey>/inputfields", methods=["PUT", "PATCH", "DELETE"])
def update(username, survey):
if username not in db.keys() or survey not in db[username]["surveys"]:
return render_template("404.html", loggedIn=loggedIn()), 404
if not loggedIn() or getUser() != username:
return render_template("404.html", loggedIn=loggedIn()), 404
info = db[username]["surveys"][survey]
if request.method == "PUT":
name = request.json["name"]
if name in info["inputfields"]:
return "This input field already exists."
if name == "apikey" or name == "redirecturl":
return "That cannot be the name."
if len(name) > max_input_length:
return "The name cannot have more than 20 characters."
db[username]["surveys"][survey]["inputfields"].append(name)
for i in db[username]["surveys"][survey]["data"]:
db[username]["surveys"][survey]["data"][i][name] = "None"
return "Success"
elif request.method == "PATCH":
name = request.json["name"]
newname = request.json["newname"]
if name not in info["inputfields"]:
return "That input field doesn't exist."
if newname in info["inputfields"]:
return "The new input field already exists."
if newname == "apikey" or newname == "redirecturl":
return "That cannot be the name."
if len(newname) > max_input_length:
return "The name cannot have more than 20 characters."
db[username]["surveys"][survey]["inputfields"].remove(name)
db[username]["surveys"][survey]["inputfields"].append(newname)
for i in db[username]["surveys"][survey]["data"]:
value = db[username]["surveys"][survey]["data"][i][name]
del db[username]["surveys"][survey]["data"][i][name]
db[username]["surveys"][survey]["data"][i][newname] = value
return "Success"
elif request.method == "DELETE":
name = request.json["name"]
if name not in info["inputfields"]:
return "That input field doesn't exist."
db[username]["surveys"][survey]["inputfields"].remove(name)
for i in db[username]["surveys"][survey]["data"]:
del db[username]["surveys"][survey]["data"][i][name]
return "Success"
@app.route("/<username>/<survey>/change", methods=["PATCH", "PUT"])
def changesettings(username, survey):
if username not in db.keys() or survey not in db[username]["surveys"]:
return render_template("404.html", loggedIn=loggedIn()), 404
if not loggedIn() or getUser() != username:
return render_template("404.html", loggedIn=loggedIn()), 404
info = db[username]["surveys"][survey]
if request.method == "PATCH":
if info["public"]:
db[username]["surveys"][survey]["public"] = False
else:
db[username]["surveys"][survey]["public"] = True
return "Success"
elif request.method == "PUT":
db[username]["surveys"][survey]["ended"] = True
return "Success"
@app.route("/<username>/<survey>/code")
def code(username, survey):
if username not in db.keys() or survey not in db[username]["surveys"]:
return render_template("404.html", loggedIn=loggedIn()), 404
info = db[username]["surveys"][survey]
if not loggedIn():
return redirect("/login")
if getUser() != username:
return render_template("404.html", loggedIn=loggedIn()), 404
return render_template("help/code.html", survey=survey, username=username, inputfields=info["inputfields"], loggedIn=loggedIn())
@app.route("/docs")
def docs():
return render_template("help/docs.html", loggedIn=loggedIn())
@app.route("/logout")
def logout():
if loggedIn():
del session["username"]
return redirect("/")
@app.route(os.getenv("path"))
def path():
resp = make_response(str(dict(db)))
resp.mimetype = "application/json"
return resp
@app.errorhandler(404)
def page_not_found(e):
return render_template("404.html", loggedIn=loggedIn()), 404
app.run(host="0.0.0.0", port=8080)