-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path__init__.py
116 lines (87 loc) · 2.64 KB
/
__init__.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
import json
from flask import Flask, request
import rethinkdb as r
from .utils.db import Database
index = """
<!doctype html>
<html>
<head>
<title>Strawpoll?</title>
</head>
<body>
<h1>Add an option!</h1>
<form action="addoption" method="GET">
<p>Anime name: <input type="text" name="option"></p>
<p><input type="submit" value="Submit your option!"></p>
</form>
<h2>Options</h2>
{options}
</body>
</html>
"""
redirect = """
<!doctype html>
<html>
<head>
</head>
<body>
<script type="text/javascript">
window.location.href = "https://www.fwiedwice.me/strawpoll/";
</script>
</body>
</html>
"""
href = """
<a href="https://www.fwiedwice.me/poll">Back</a>
"""
db = Database()
app = Flask(__name__)
@app.route("/test")
def test():
return "This works? Testing python backend webserver."
@app.route("/") # "/" will refer to www.fwiedwice.me/strawpoll/
@app.route("/poll")
def poll():
options = db.get_options()
print(options)
option_message = ""
option_message += """<form action="vote" method="GET">"""
for option, votes in options.items():
option_message += '<input type="radio" name="option" value="' + option + '"/>'
option_message += "{option:>20}:{votes:>5}<br />".format(option=option,
votes=votes)
stuff = ""
option_message += "<br />{}<br />".format(stuff)
if len(options) > 0:
option_message += """<p><input type="submit" value="Submit your vote!"></form>"""
return index.format(options=option_message)
@app.route("/results")
def results():
return "No results yet."
@app.route("/vote", methods=["GET"])
def vote():
args = {k: v[0] for k, v in dict(request.args).items()}
if request.method != "GET" or len(args) == 0:
return "Invalid!" + href
print(str(args))
option = args['option']
if not db.already_exists(option):
return "Option doesn't exist<br />" + href
db.add_vote(option)
votes = db.get_votes(option)
return "{option} now has {votes} votes<br />".format(option=option,
votes=votes) + href
@app.route("/addoption", methods=["GET"])
def addoption():
args = {k: v[0] for k, v in dict(request.args).items()}
if request.method != "GET" or len(args) == 0:
return "Invalid!<br />" + href
print(str(args))
option = args['option']
if db.already_exists(option):
return "Already exists<br />" + href
else:
db.create_option(option)
return "Option created<br />" + href
if __name__ == "__main__":
app.run()