-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_web.py
185 lines (166 loc) · 6.21 KB
/
main_web.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
import UserDatabaseAccess as db
from flask import Flask, render_template, request, redirect, url_for, Response
import json
app = Flask(__name__)
db = db.UserDatabaseAccess()
'''
AUTOR: Cristian Pérez Corral
Clase de gestión de peticiones HTTP para la aplciación web,
actúa a su vez como servidor web
'''
#Página principal. Carga el login
@app.route('/')
def main():
return render_template("login.html", error = "")
#Método que se encarga de acceder a la base de datos para comprobar el login, y dar error en caso de que esté mal
@app.route('/login', methods = ['GET', 'POST'])
def login():
if[request.method == 'POST']:
if request.form['action'] == 'login':
req = request.form
if(check_access_database(req.get('email'), req.get('pass'))[0]):
return redirect(url_for('home'))
else:
return render_template("login.html", error = "User not found")
#Método que se encarga de cargar la página de registro
@app.route('/register')
def register():
return render_template('register.html')
#Método que se encarga de comprobar que /to/do el registro esté correcto
@app.route('/register_database', methods = ['GET', 'POST'])
def register_database():
if [request.method == 'POST']:
if request.form['action'] == 'register':
req = request.form
b1, b2, b3, b4, b5, b6 = db.validarRegistro(req.get('username'), req.get('pass'), req.get('email'))
if(not b1 or not b2):
return render_template('register.html', error = "The email is not correct")
elif(not b3):
return render_template('register.html', error = "The username is not correct")
elif(not b4):
return render_template('register.html', error = "The password is not correct")
elif(not b5):
return render_template('register.html', error = "The username is already register")
elif(not b6):
return render_template('register.html', error = "The email is already register")
else:
db.insertarUsuario([req.get('username'), req.get('pass'), req.get('email')])
return redirect('/')
#Este método nos permite cargar la página principal. Cargamos también los algoritmos de Community, por si hay alguno nuevo-
@app.route('/index', methods = ['GET', 'POST'])
def home():
ls = get_community()
return render_template("index.html", Community = ls)
#El resto de métodos se encargar de parsear los algoritmos correspondientes a cada categoría.
@app.route('/sorting/<algorithm>')
def sorting(algorithm = ""):
s = []
with open("codes/Sorting.py") as f:
lines = f.readlines()
i = 0
while lines[i] != "#" + algorithm + "\n":
i += 1
for j in range(i + 1, len(lines)):
if lines[j] != "---\n":
s.append(lines[j])
else:
break
return render_template("render_alg.html", code_list = s, Community = get_community())
@app.route('/math/<algorithm>')
def math(algorithm = ""):
s = []
with open("codes/Math.py") as f:
lines = f.readlines()
i = 0
while lines[i] != "#" + algorithm + "\n":
i += 1
for j in range(i + 1, len(lines)):
if lines[j] != "---\n":
s.append(lines[j])
else:
break
return render_template("render_alg.html", code_list = s, Community = get_community())
@app.route('/dp/<algorithm>')
def dp(algorithm = ""):
s = []
with open("codes/Dynammic Programming.py") as f:
lines = f.readlines()
i = 0
while lines[i] != "#" + algorithm + "\n":
i += 1
for j in range(i + 1, len(lines)):
if lines[j] != "---\n":
s.append(lines[j])
else:
break
return render_template("render_alg.html", code_list = s, Community = get_community())
@app.route('/graph/<algorithm>')
def graphs(algorithm = ""):
s = []
with open("codes/Graph.py") as f:
lines = f.readlines()
i = 0
while lines[i] != "#" + algorithm + "\n":
i += 1
for j in range(i + 1, len(lines)):
if lines[j] != "---\n":
s.append(lines[j])
else:
break
return render_template("render_alg.html", code_list = s, Community = get_community())
@app.route('/community/<algorithm>')
def community(algorithm = ""):
s = []
with open("codes/Community.py") as f:
lines = f.readlines()
i = 0
while lines[i] != "#" + algorithm + "\n":
print(lines[i])
i+=1
for j in range(i + 1, len(lines)):
if lines[j] != "---\n":
s.append(lines[j])
else:
break
return render_template("render_alg.html", code_list = s, Community = get_community())
@app.route('/local')
def login_local():
content = request.json
result = check_access_database(content[0], content[1])
if( result[0] ):
return Response(json.dumps({'b1':result[0], 'res': list(result[1][0])}))
else:
return Response(json.dumps({'b1': result[0], 'res': []}))
#Este método se encarga de chequear el acceso en la base de datos
def check_access_database(user, con):
return db.validarAcceso(user, con)
def get_community():
with(open("codes/Community.py")) as f:
lines = f.readlines()
res = []
for i in lines:
if i.startswith('#'):
i = i[1::]
res.append(i)
return res
@app.route('/upload', methods = ['POST'])
def upload_com():
content = request.json
name = content['name']
user = content['user']
desc = content['description']
code = content['content']
with open('codes/Community.py', "a") as f:
c = code.split("\n")
s_code = ""
for i in c:
if(not ("#" in i)):
print(i)
s_code += i
s_code += "\n"
print(s_code)
new_code = "#{}\n'''\n\tAutor: \t{}\n{}'''\n{}".format(name, user, desc, s_code) + "\n---\n"
f.write(new_code)
return Response("")
if __name__ == '__main__':
app.run(debug=True)