-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
101 lines (68 loc) · 2.52 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
from flask import Flask, render_template, request, make_response
import requests
app = Flask(__name__, template_folder='.', static_folder='./static')
db_url = 'https://db-sokola.duckdns.org'
def execute_query(query):
url = f'{db_url}/db/query?associative&q={query}'
response = requests.get(url)
if response.status_code == 200:
return response.json()
else:
return None
def write_query(query_list):
url = f'{db_url}/db/execute'
response = requests.post(url, json=query_list)
r = response.json()
if response.status_code == 200 and 'rows_affected' in r['results'][0]:
return r
else:
return None
@app.route('/', methods=['GET'])
def web():
results = execute_query("SELECT * FROM sokola")
rows = results['results'][0]['rows']
name_pairs = []
for i in range(0, len(rows), 2):
if i + 1 < len(rows):
pair = (rows[i]['NAME'], rows[i + 1]['NAME'])
else:
pair = (rows[i]['NAME'], "")
name_pairs.append(pair)
print(name_pairs)
return render_template('index.html', sokola_names=name_pairs)
@app.route('/admin', methods=['GET'])
def admin():
results = execute_query("SELECT * FROM sokola")
names = []
if results:
names = [(item['ID'], item['NAME']) for item in results['results'][0]['rows']]
return render_template('admin.html', message="", sokola_names=names)
@app.route('/add', methods=['POST'])
def add():
name = request.form['sokola-name']
query = f"INSERT INTO sokola (NAME) VALUES ('{name}')"
write_query([query])
results = execute_query("SELECT * FROM sokola")
names = []
if results:
names = [(item['ID'], item['NAME']) for item in results['results'][0]['rows']]
return render_template('admin.html', message="Tafiditra", sokola_names=names)
@app.route('/delete', methods=['POST'])
def delete():
ID = request.args['id']
query = f"DELETE FROM sokola WHERE ID={ID}"
r = write_query([query])
if r == None:
return make_response({'code': 400}, 400)
return make_response({'code': 200}, 200)
@app.route('/update', methods=['POST'])
def update():
ID = request.args['id']
new_name = request.args['name']
query = f"UPDATE sokola SET NAME='{new_name}' WHERE ID={ID}"
r = write_query([query])
if r == None:
return make_response({'code': 400}, 400)
return make_response({'code': 200}, 200)
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0', port=80)