-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhandler.py
197 lines (168 loc) · 6.72 KB
/
handler.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
196
197
# -*- coding: utf-8 -*-
import json
from tornado.options import options
from tornado.web import MissingArgumentError
from tort.handler import RequestHandler
from tort.util.request import real_ip
from util.formdata import encode_multipart_formdata
from util.lock import Lock, LockException
from util import urljoin
class IndexHandler(RequestHandler):
locker = Lock()
field_to_error_name = {
"first_name": "Имя",
"last_name": "Фамилия",
"phone": "Телефон",
"email": "Электронный адрес",
}
def compute_etag(self):
return
@staticmethod
def add_api_headers(headers: dict = None) -> dict:
if headers is None:
headers = {}
headers.update(
{"Authorization": "Bearer {}".format(options.api_key),}
)
return headers
@staticmethod
def compose_api_url(path: str) -> str:
return urljoin(options.api_endpoint, "account", options.api_account_id, path)
def finish_with_error(
self, error_text: str = "Не удалось отправить отклик, попробуйте еще раз"
):
self.set_status(400)
self.finish({"errors": {"common": [error_text]}})
def get(self):
self.render(
"templates/index.html",
vacancy_name=options.vacancy_name,
vacancy_url=options.vacancy_url,
privacy_url=options.privacy_url,
vacancy_og=options.vacancy_og,
)
async def post(self):
try:
data = {
"first_name": self.get_argument("first_name"),
"last_name": self.get_argument("last_name"),
"phone": self.get_argument("phone"),
"email": self.get_argument("email"),
}
except MissingArgumentError as e:
self.finish_with_error(
'Не заполнено поле "{}"'.format(self.field_to_error_name[e.arg_name])
)
return
lock_key = real_ip(self.request)
try:
self.locker.gain_lock(lock_key)
except LockException:
self.finish_with_error("Вы отправляете запросы слишком часто. Попробуйте позднее.")
return
comment = self.get_argument("comment", None)
cv_link = self.get_argument("cv", None)
cv_files = self.request.files.get("cv_file")
cv_file = None
if cv_files:
cv_file = self.request.files["cv_file"][0]
if cv_file:
content_type, body = encode_multipart_formdata(
[], [("file", cv_file["filename"], cv_file["body"])]
)
response, parsed_file_data = await self.fetch_request(
self.make_request(
"upload",
method="POST",
full_url=self.compose_api_url("upload"),
headers=self.add_api_headers(
{
"Content-Type": content_type,
"Content-Length": str(len(body)),
"X-File-Parse": "true",
}
),
data=body,
allow_ipv6=False,
validate_cert=False,
request_timeout=5.0,
)
)
if not response.error:
if parsed_file_data.get("photo"):
data["photo"] = parsed_file_data["photo"]["id"]
fields = parsed_file_data.get("fields")
if fields:
if fields.get("birthdate") and fields["birthdate"].get("precision") == "day":
data["birthday_day"] = fields["birthdate"]["day"]
data["birthday_month"] = fields["birthdate"]["month"]
data["birthday_year"] = fields["birthdate"]["year"]
if fields.get("experience"):
data["position"] = fields["experience"][0]["position"]
data["company"] = fields["experience"][0]["company"]
if fields.get("salary"):
data["money"] = fields["salary"]
data["externals"] = [
{
"data": {
"body": "\n\n".join(
filter(
None,
[
"Ссылка на резюме: {}".format(cv_link) if cv_link else None,
parsed_file_data.get("text"),
],
)
)
},
"files": [{"id": parsed_file_data["id"]}],
"auth_type": "NATIVE",
"account_source": options.source_id,
}
]
if not data.get("externals"):
data["externals"] = [
{
"data": {"body": cv_link or ""},
"auth_type": "NATIVE",
"account_source": options.source_id,
}
]
response, applicant_data = await self.fetch_request(
self.make_request(
"applicant",
method="POST",
full_url=self.compose_api_url("applicants"),
headers=self.add_api_headers(),
data=json.dumps(data),
allow_ipv6=False,
validate_cert=False,
request_timeout=5.0,
)
)
if response.error:
self.finish_with_error()
return
response, vacancy_applicant_data = await self.fetch_request(
self.make_request(
"applicant-vacancy",
method="POST",
full_url=self.compose_api_url("applicants/{}/vacancy".format(applicant_data["id"])),
headers=self.add_api_headers(),
validate_cert=False,
allow_ipv6=False,
data=json.dumps(
{
"vacancy": options.vacancy_id,
"status": options.status_id,
"comment": "Сопроводительное письмо: {}".format(comment)
if comment
else None,
}
),
)
)
if response.error:
self.finish_with_error()
return
self.finish({"status": "Отлик успешно добавлен"})