This repository was archived by the owner on Apr 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck.py
More file actions
165 lines (134 loc) · 4.81 KB
/
check.py
File metadata and controls
165 lines (134 loc) · 4.81 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
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
import aiohttp
import re
import smtplib
import socket
import ssl
import read
import utils
"""
Forms check
"""
async def validity(csv, doc):
# 'oggetto' and 'pec' are required
valid, msg = valid_csv(csv)
if not valid:
print(msg['text'])
return msg
# check consistency between the csv headers and the doc vars
valid, msg = valid_consistency(csv, doc)
if not valid:
print(msg['text_docx'])
print(msg['text_xlsx'])
return msg
# check and download the attachments
valid, msg = await valid_attachments(csv)
if not valid:
print(msg['text'])
return msg
return 'OK'
def valid_csv(csv):
print('\nCheck csv fields (oggetto and pec are required)')
msg = {}
valid = True
headers = csv['headers']
if 'oggetto' not in headers or 'pec' not in headers:
valid = False
msg['field'] = 'xlsx'
msg['text'] = 'Controlla che i campi "oggetto" e "pec" siano presenti nel file excel'
return valid, msg
def valid_consistency(csv, doc):
print('\nCheck the consistency between the .docx and the .xlsx or .csv')
msg = {}
valid = True
# Get the .docx vars
pattern = '\$\{(.*?)\}'
doc_vars = []
for p in doc.paragraphs:
par_vars = re.findall(pattern, p.text)
doc_vars.extend(par_vars)
# Get .csv or .xlsx vars
headers = csv['headers']
headers_vars = [v for v in headers if v != 'oggetto' and v != 'pec' and 'allegato' not in v]
# Check consistency
doc_vars.sort()
headers_vars.sort()
if doc_vars != headers_vars:
valid = False
join_str = ', '
msg['field'] = 'both'
msg['text_docx'] = 'Attenzione i campi del .docx sono: [' + join_str.join(doc_vars) + ']. '
msg['text_xlsx'] = 'Attenzione i campi del .xlsx o .csv sono [' + join_str.join(headers_vars) + '] .'
return valid, msg
async def valid_attachments(csv):
print('\nCheck if the attachments are valid')
msg = {}
msg['text'] = ''
valid = True
# Get all the attachments
data = csv['data']
line = 1
attachments = {}
for l in data:
line_attachments = []
for k,v in l.items():
if 'allegato' in k:
line_attachments.append(v)
attachments[line] = line_attachments
line+=1
# Check all the attachments
for row, urls in attachments.items():
# No empty 'allegato' fields
valid_urls = [i.strip() for i in urls if i.strip() != '']
# Get headers (async way) and process
results = await utils.multi_requests(valid_urls, utils.request_header)
for r in results:
if type(r['response']) == dict:
url = r['url']
ct = r['response']['content-type'].lower()
if 'text' in ct or 'html' in ct:
valid = False
msg['field'] = 'xlsx'
msg['text'] += """Controlla l\'allegato {u} alla riga {r}. Il link non contiene un file.\n""".format(u=url, r=int(row)+1)
else:
url = r['url']
valid = False
msg['field'] = 'xlsx'
msg['text'] += """Controlla l\'allegato {u} alla riga {r}. Non sembra un link valido.\n""".format(u=url, r=int(row)+1)
return valid, msg
def valid_login_connection(sender, password, server, port):
msg = 'OK'
smtp_server = None
# Create a secure SSL context
context = ssl.create_default_context()
# Check server connection
try:
smtp_server = smtplib.SMTP_SSL(server, port, context=context)
connected = True
except socket.gaierror as socket_err:
msg = 'Connessione al server - Controlla l\'indirizzo: %s' % server
print(socket_err)
except Exception as err:
msg = 'Connessione al server - Errore generico'
print(err)
# Check server login
try:
smtp_server.login(sender, password)
flg = True
except smtplib.SMTPConnectError as conn_err:
msg = 'Login - Errore di connessione con il server'
print(conn_err)
except smtplib.SMTPServerDisconnected as discnt_err:
msg = 'Login - Il server si è disconnesso inaspettatamente'
print(discnt_err)
except smtplib.SMTPAuthenticationError as auth_err:
msg = 'Login - Errore di autenticazione: controlla nome utente, password o le impostazioni di sicurezza del servizio'
print(auth_err)
except smtplib.SMTPResponseException as smtp_res:
msg = 'Login - Errore di risposta di SMTP'
print(smtp_res)
except smtplib.SMTPException as smtp_err:
msg = 'Login - Errore generico di SMTP'
print(smtp_err)
except Exception as err:
msg = 'Login - Errore generico'
return msg, smtp_server