This repository has been archived by the owner on Mar 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
132 lines (112 loc) · 3.4 KB
/
main.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
from flask import Flask, Response, jsonify, request
from flask_api import status
from flask_cors import CORS
from filter import filterSpreadsheet
app = Flask(__name__)
CORS(app)
@app.route("/", methods=["GET"])
def get_data():
return jsonify({"message": "Hello world"}), status.HTTP_200_OK
@app.route("/", methods=["POST"])
def post_data():
data = request.get_json()
if not data:
return (jsonify({"message": "No data received"}), status.HTTP_400_BAD_REQUEST)
# insert 0 in empty integer fields
for key in (
"idadeMin",
"idadeMax",
"parcelaMin",
"parcelaMax",
"parcelasPagasMin",
"parcelasPagasMax",
"jurosMin",
"jurosMax",
):
if key not in data:
data[key] = 0
# check if all fields are not null
if not all(
key in data
for key in (
"uf",
"idadeMin",
"idadeMax",
"parcelaMin",
"parcelaMax",
"parcelasPagasMin",
"parcelasPagasMax",
"jurosMin",
"jurosMax",
"esp",
"banco_emp",
"banco_pgto",
)
):
return (
jsonify({"message": "Data has missing keys"}),
status.HTTP_400_BAD_REQUEST,
)
# check if list fields are lists
if not all(
isinstance(uf, list)
for uf in (data["uf"], data["esp"], data["banco_emp"], data["banco_pgto"])
):
return (
jsonify({"message": "Data has invalid values", "data": data}),
status.HTTP_400_BAD_REQUEST,
)
# check if all integer fields are integers
if not all(
isinstance(data[key], int)
for key in (
"idadeMin",
"idadeMax",
"parcelaMin",
"parcelaMax",
"parcelasPagasMin",
"parcelasPagasMax",
"jurosMin",
"jurosMax",
)
):
return (
jsonify({"message": "Data has invalid values", "data": data}),
status.HTTP_400_BAD_REQUEST,
)
# check if all values in uf are strings
if not all(isinstance(uf, str) for uf in data["uf"]):
return (
jsonify({"message": "UF array has invalid values", "data": data}),
status.HTTP_400_BAD_REQUEST,
)
special_strings_for_arrays = ["ALL"]
for key in ("esp", "banco_emp", "banco_pgto"):
for field in data[key]:
if field in special_strings_for_arrays:
break
if not isinstance(field, int):
return (
jsonify({"message": "Data has invalid values", "data": data}),
status.HTTP_400_BAD_REQUEST,
)
result = filterSpreadsheet(data)
if not result:
return (jsonify({"message": "No data found"}), status.HTTP_404_NOT_FOUND)
if not isinstance(result, str):
return (
jsonify({"message": "Internal server error"}),
status.HTTP_500_INTERNAL_SERVER_ERROR,
)
if len(result.splitlines()) == 1:
return (jsonify({"message": "No data found"}), status.HTTP_404_NOT_FOUND)
return (
Response(
result,
mimetype="text/csv",
headers={"Content-Disposition": "attachment; filename=data.csv"},
),
status.HTTP_200_OK,
)
if __name__ == "__main__":
app.run()