app.py
3.34 KB
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
#-*- coding: utf-8 -*-
from flask import Flask, request, url_for, render_template
import os, requests, aux, json
from validation import Validation
app = Flask(__name__)
LOGIN_FIELDS = [
{
"title" : "Usuario",
"type" : "text",
"required" : True
},
{
"title" : "Contraseña",
"type" : "password",
"required" : True
}
]
JSON_FILES = "json"
ROOT_DEFAULT = "/form/default"
@app.route('/', methods = ['GET'])
def main():
return render_template('redirect.html',url = ROOT_DEFAULT)
@app.route('/load/<name>', methods = ['POST'])
def load(name):
import ipdb; ipdb.set_trace()
try:
data = request.json
if data == None:
return aux.answer(False, "Ningún dato enviado")
with open(f"{JSON_FILES}/{name}.json", "+w") as f:
f.write(json.dumps(data))
return aux.answer(True)
except:
return aux.answer(False, "Error escribiendo archivo")
@app.route('/remove/<name>', methods = ['POST'])
def remove(name):
try:
if os.path.exists(f"{JSON_FILES}/{name}.json"):
os.remove(f"{JSON_FILES}/{name}.json")
return aux.answer(True)
except:
return aux.answer(False, "Error borrando archivo")
@app.route('/form/<name>', methods = ['GET'])
def iform(name):
return render_template("redirect.html",url = f"/form/{name}")
@app.route('/form/<name>/<token>', methods = ['GET'])
def form(name,token):
try:
validation = Validation()
# Not valid. When you finish loging in, go to this address
if not validation.check_token(token):
return login(f'/form/{name}')
if os.path.exists(f"{JSON_FILES}/{name}.json"):
with open(f"{JSON_FILES}/{name}.json") as f:
return aux.generate(json.loads(f.read()))
return aux.answer(False, "No existe el formulario")
except:
return aux.answer(False, "Error leyendo el formulario")
@app.route('/login', methods = ['GET'])
def login(redirect = None):
try:
if os.path.exists(f"{JSON_FILES}/login.json"):
with open(f"{JSON_FILES}/login.json","r") as f:
data = f.read()
if data == "":
return aux.answer(False, "No hay datos para generar el login")
data = json.loads(data)
if 'color' not in data:
data['color'] = COLOR_DEFAULT
if 'validate' not in data:
return aux.answer(False, "No hay dirección de validación => {'validate' : 'example.com'}")
if 'redirect' not in data:
return aux.answer(False, "No hay dirección de redirección => {'redirect' : 'example.com'}")
return render_template(
"login.html",
title = "Login",
fields = LOGIN_FIELDS,
color = data['color'],
validate = data['validate'],
redirect = redirect if redirect != None else data['redirect']
)
return aux.answer(False, "No existe el formulario de login")
except:
return aux.answer(False, "Hubo un error en la generación del login")
@app.route('/validate', methods = ['POST'])
def ivalidate():
try:
data = request.json
except:
data = {}
return aux.validate(data)
@app.route('/remove-session', methods = ['POST'])
def removesession():
try:
validation = Validation()
validation.remove_roken(request.json)
aux.answer(True)
except:
aux.answer(False, "Error borrando sesión")
if __name__ == "__main__":
app.run("0.0.0.0")