-
Notifications
You must be signed in to change notification settings - Fork 0
/
web.py
195 lines (165 loc) · 8.7 KB
/
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
186
187
188
189
190
191
192
193
194
195
from crypt import methods
from flask import Flask, render_template, jsonify, request, send_file, Response
from werkzeug.utils import secure_filename
import os
from main import *
#import magic
import urllib.request
from datetime import datetime
app = Flask(__name__)
app.secret_key = "flsldfj399dksdf-fj39fls.vicsla92kdan-mcd83jkksdh"
#Reference to server directory:
from pathlib import Path
THIS_FOLDER = Path(__file__).parent.resolve()
UPLOAD_FOLDER = THIS_FOLDER / "uploads"
DOWNLOAD_FOLDER = THIS_FOLDER / "files"
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
app.config['DOWNLOAD_FOLDER'] = DOWNLOAD_FOLDER
app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024
ALLOWED_EXTENSIONS = set(['xlsx'])
def allowed_file(filename):
return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS
def read_local_plain_text(file_name):
with open(file_name, "r") as file:
data = file.read()
return data.replace("\n", "|")
def read_local_json(json_file_name):
json_file = open(json_file_name, "r")
data = json_file.read()
json_file.close()
return data
@app.route('/api/metadatos')
def api_returnMetadatos():
if request.method == "GET":
token = request.args.get('token')
if not token or token != app.secret_key:
return json.dumps({'success':False, 'reason' : 'Not authorized'}), 401, {'ContentType':'application/json'}
data = read_local_json(os.path.join(app.config['DOWNLOAD_FOLDER'],'1_metadatos.json'))
return data, 200, {'ContentType':'application/json'}
@app.route('/metadatos')
def returnMetadatos():
downloads = os.path.join(app.root_path, app.config['DOWNLOAD_FOLDER'])
return send_file(os.path.join(app.config['DOWNLOAD_FOLDER'],'1_metadatos.json'), as_attachment=True)
@app.route('/api/catalogoMetadatosHomologados')
def api_returnCatalogoMetadatosHomologados():
if request.method == "GET":
token = request.args.get('token')
if not token or token != app.secret_key:
return json.dumps({'success':False, 'reason' : 'Not authorized'}), 401, {'ContentType':'application/json'}
data = read_local_json(os.path.join(app.config['DOWNLOAD_FOLDER'],'1_catalogo_metadatos_homologados.json'))
return data, 200, {'ContentType':'application/json'}
@app.route('/catalogoMetadatosHomologados')
def returnCatalogoMetadatosHomologados():
downloads = os.path.join(app.root_path, app.config['DOWNLOAD_FOLDER'])
return send_file(os.path.join(app.config['DOWNLOAD_FOLDER'],'1_catalogo_metadatos_homologados.json'), as_attachment=True)
@app.route('/api/metadatosNoValidos')
def api_returnMetadatosNoValidos():
if request.method == "GET":
token = request.args.get('token')
if not token or token != app.secret_key:
return json.dumps({'success':False, 'reason' : 'Not authorized'}), 401, {'ContentType':'application/json'}
data = read_local_json(os.path.join(app.config['DOWNLOAD_FOLDER'],'1_metadatos_no_validos.json'))
return data, 200, {'ContentType':'application/json'}
@app.route('/metadatosNoValidos')
def returnMetadatosNoValidos():
downloads = os.path.join(app.root_path, app.config['DOWNLOAD_FOLDER'])
return send_file(os.path.join(app.config['DOWNLOAD_FOLDER'],'1_metadatos_no_validos.json'), as_attachment=True)
@app.route('/api/catalogoSistemas')
def api_returnCatalogoSistemas():
if request.method == "GET":
token = request.args.get('token')
if not token or token != app.secret_key:
return json.dumps({'success':False, 'reason' : 'Not authorized'}), 401, {'ContentType':'application/json'}
data = read_local_json(os.path.join(app.config['DOWNLOAD_FOLDER'],'1_catalogo_sistemas.txt'))
return data, 200, {'ContentType':'application/json'}
@app.route('/')
def main():
return render_template('index.html')
@app.route("/api/create", methods=["GET"])
def api_create():
if request.method == "GET":
token = request.args.get('token')
if not token or token != app.secret_key:
return json.dumps({'success':False, 'reason' : 'Not authorized'}), 401, {'ContentType':'application/json'}
doMainReadingFromGoogle()
return json.dumps({'success':True, 'reason' : 'File generated ok'}), 200, {'ContentType':'application/json'}
@app.route("/create", methods=["GET"])
def create():
if request.method == "GET":
doMainReadingFromGoogle()
return json.dumps({'success':True, 'reason' : 'File generated ok'}), 200, {'ContentType':'application/json'}
# create a route to get the contents of the sistemasGestorContenido.txt file
@app.route('/api/sistemas')
def api_sistemas():
if request.method == "GET":
token = request.args.get('token')
if not token or token != app.secret_key:
return json.dumps({'success':False, 'reason' : 'Not authorized'}), 401, {'ContentType':'application/json'}
data = read_local_plain_text(SISTEMAS_FILE)
return data, 200, {'ContentType':'application/json'}
# create a post route that will receive a param called "sistema", will read the file sistemasGestorContenido.txt remove the sistema that matches with the param and write
# the new content to the file
@app.route('/api/deleteSistema', methods=["POST"])
def api_sistemas_post():
if request.method == "POST":
token = request.args.get('token')
if not token or token != app.secret_key:
return json.dumps({'success':False, 'reason' : 'Not authorized'}), 401, {'ContentType':'application/json'}
sistema = request.form.get('sistema')
if not sistema:
return json.dumps({'success':False, 'reason' : 'No sistema provided'}), 400, {'ContentType':'application/json'}
data = read_local_plain_text(SISTEMAS_FILE)
# parse the data, it's pipe separated, put it in a list, remove the sistema and write it back to the file
parsed_data = data.split("|");
if sistema in parsed_data:
parsed_data.remove(sistema)
# write the new data to the file, remove the pipes and write one element per line
data = "\n".join(parsed_data)
with open(SISTEMAS_FILE, "w") as file:
file.write(data)
return json.dumps({'success':True, 'reason' : 'Sistema removed'}), 200, {'ContentType':'application/json'}
else:
return json.dumps({'success':False, 'reason' : 'Sistema not found'}), 404, {'ContentType':'application/json'}
@app.route("/upload",methods=["POST","GET"])
def upload():
if request.method == 'POST':
file = request.files['file']
filename = secure_filename(file.filename)
now = datetime.now()
if file and allowed_file(file.filename):
file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
print('File successfully uploaded ' + file.filename + ' to the server!')
doMain(os.path.join(app.config['UPLOAD_FOLDER'],file.filename))
else:
print('Solo archivos excel xlsx')
msg = 'Archivo recibido'
return jsonify(msg)
# create 'api/createSistema' route that will receive a param called "sistema" and will write it to the file sistemasGestorContenido.txt
@app.route('/api/createSistema', methods=["POST"])
def api_create_sistema():
if request.method == "POST":
token = request.args.get('token')
if not token or token != app.secret_key:
return json.dumps({'success':False, 'reason' : 'Not authorized'}), 401, {'ContentType':'application/json'}
sistema = request.form.get('sistema')
if not sistema:
return json.dumps({'success':False, 'reason' : 'No sistema provided'}), 400, {'ContentType':'application/json'}
data = read_local_plain_text(SISTEMAS_FILE)
# parse the data, it's pipe separated, put it in a list, add the sistema and write it back to the file
parsed_data = data.split("|");
if sistema not in parsed_data:
parsed_data.append(sistema)
# write the new data to the file, remove the pipes and write one element per line
data = "\n".join(parsed_data)
with open(SISTEMAS_FILE, "w") as file:
file.write(data)
return json.dumps({'success':True, 'reason' : 'Sistema added'}), 200, {'ContentType':'application/json'}
else:
return json.dumps({'success':False, 'reason' : 'Sistema already exists'}), 400, {'ContentType':'application/json'}
@app.route('/api/archivo')
def api_archivo():
if request.method == "GET":
downloads = os.path.join(app.root_path, app.config['DOWNLOAD_FOLDER'])
return send_file(os.path.join(app.config['DOWNLOAD_FOLDER'],'Archive.zip'), as_attachment=True)
if __name__ == "__main__":
app.run(debug=True)