-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcontrollers.py
151 lines (133 loc) · 5.48 KB
/
controllers.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
import datetime
import json
import uuid
from http import HTTPStatus
def get_request(self):
length = int(self.headers.get("content-length"))
request = json.loads(self.rfile.read(length))
return request
def response(self, json_output):
return self.wfile.write(json.dumps(json_output).encode("utf-8"))
def set_headers(self, status_code):
self.send_response(status_code)
self.send_header("Content-type", "application/json")
self.send_header("Access-Control-Allow-Origin", "*")
self.end_headers()
class Controllers:
def save_user(self):
request = get_request(self)
with open("data/users.json", "r+") as file:
# data = {"users": []}
data = json.load(file)
if request["user"] not in data["users"]:
data["users"].append(request["user"])
file.seek(0)
json.dump(data, file)
set_headers(self, HTTPStatus.OK)
return response(self, {"success": True, "response": request})
else:
set_headers(self, HTTPStatus.CONFLICT)
return response(self, {
"success": False, "error": "Usuário já existe"
})
def get_user(self, query_param):
if query_param is not None:
user = query_param.split("=")[1]
else:
set_headers(self, HTTPStatus.BAD_REQUEST)
return response(self, {"success": False, "error": "Faltando o parâmetro 'user'"})
with open("data/users.json", "r") as file:
data = json.load(file)
if user in data["users"]:
set_headers(self, HTTPStatus.OK)
return response(self, {"success": True, "response": {"user": user}})
else:
set_headers(self, HTTPStatus.NOT_FOUND)
return response(self, {"success": False, "error": "Usuário não encontrado"})
def send_mail(self):
request = get_request(self)
# Verify if users exists
email = {"id": str(uuid.uuid4()), "forwarded_to": [], "replies": []}
email.update(request)
with open("data/emails.json", "r+") as file:
# data = []
data = json.load(file)
data.append(email)
file.seek(0)
json.dump(data, file)
set_headers(self, HTTPStatus.OK)
return response(self, {"success": True, "response": email})
def list_mails(self, query_param):
received = []
sent = []
if query_param is not None:
user = query_param.split("=")[1]
else:
set_headers(self, HTTPStatus.BAD_REQUEST)
return response(self, {"success": False, "error": "Faltando o parâmetro 'user'"})
with open("data/emails.json", "r") as file:
data = json.load(file)
for email in data:
if email["receiver"] == user or user in email["forwarded_to"]:
received.append(email)
elif email["sender"] == user:
sent.append(email)
set_headers(self, HTTPStatus.OK)
return response(
self,
{"success": True, "response": {"received": received, "sent": sent}}
)
def open_mail(self, query_param):
if query_param is not None:
id = query_param.split("=")[1]
else:
set_headers(self, HTTPStatus.BAD_REQUEST)
return response(self, {"success": False, "error": "Faltando o parâmetro 'id'"})
with open("data/emails.json", "r") as file:
data = json.load(file)
for email in data:
if email["id"] == id:
set_headers(self, HTTPStatus.OK)
return response(self, {"success": True, "response": email})
def replay_mail(self):
request = get_request(self)
reply = {
"datetime": datetime.datetime.now().strftime("%c"),
"replied_by": request["replied_by"],
"reply": request["reply"],
}
with open("data/emails.json", "r+") as file:
data = json.load(file)
for email in data:
if email["id"] == request["id"]:
email["replies"].append(reply)
file.seek(0)
json.dump(data, file)
set_headers(self, HTTPStatus.OK)
return response(self, {"success": True, "response": email})
def forward_mail(self):
request = get_request(self)
with open("data/emails.json", "r+") as file:
data = json.load(file)
for email in data:
if email["id"] == request["id"]:
email["forwarded_to"].append(request["forward_to"])
file.seek(0)
json.dump(data, file)
set_headers(self, HTTPStatus.OK)
return response(self, {"success": True, "response": email})
def delete_mail(self, query_param):
if query_param is not None:
id = query_param.split("=")[1]
with open("data/emails.json", "r+") as file:
data = json.load(file)
for i in range(len(data)):
if data[i]["id"] == id:
del data[i]
file.seek(0)
file.truncate()
json.dump(data, file)
set_headers(self, HTTPStatus.OK)
return response(
self, {"success": True, "response": "Mensagem apagada."}
)