-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdmp-alerts.py
220 lines (194 loc) · 10.3 KB
/
dmp-alerts.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import requests
import json
import sys
from dotenv import load_dotenv
import smtplib
from email.utils import formataddr
from email.message import EmailMessage
import os
from datetime import datetime
import csv
# Script for monitoring new and updated DMPs and creating alerts for relevant stakeholders.
# Configuration
load_dotenv()
# Other
d = datetime.now()
current_date = d.strftime("%Y-%m-%dT%H:%M:%S.%f")
current_date_short = d.strftime("%Y-%m-%d")
# Read last run timestamp from file
with open(os.getenv("LASTRUN_FILE"), 'r') as file:
lastrun_date = file.read().rstrip()
# Debug, test
# lastrun_date = '2021-09-01T00:00:00'
# API queries (Lucene syntax)
all_dmps_created_since_date_q = 'dmp.created:[' + lastrun_date + ' TO *]+OR+dmp.modified:[' + lastrun_date + ' TO *]'
# Request data from DMP API as string
api_auth = 'Bearer ' + str(os.getenv("DMP_API_AUTH_KEY"))
headers = {'Accept': 'application/json',
'Authorization': api_auth,
'include-metadata': 'True'}
# DO NOT use verify=False in production!!
dsw_getallnewurl = os.getenv("DMP_API_ENDPOINT") + all_dmps_created_since_date_q
data = requests.get(url=dsw_getallnewurl, headers=headers, verify=True).text
# convert string to Json
data = json.loads(data)
# print(data)
if data:
for i in data['items']:
send_new_alert = 1
send_pers_alert = 1
send_ethical_alert = 1
print('title:' + str(i['dmp']['title']))
dsw_title = i['dmp']['title']
dsw_id = i['dmp']['dmp_id']['identifier']
if 'created' in i['dmp']:
dsw_created = i['dmp']['created']
dsw_created_date = dsw_created[0:10]
else:
dsw_created_date = current_date_short
if 'modified' in i['dmp']:
dsw_modified = i['dmp']['modified']
dsw_modified_date = dsw_modified[0:10]
else:
dsw_modified_date = current_date_short
dsw_creator = ''
if 'dmp_owner' in i['metadata']:
if 'name' in i['metadata']['dmp_owner']:
dsw_creator = i['metadata']['dmp_owner']['name']
else:
dsw_creator = '(okänd)'
else:
dsw_creator = '(okänd)'
# if 'contact' in i['dmp']:
# dsw_creator = i['dmp']['contact']['name']
# else:
# dsw_creator = '(okänd)'
# Check if alert has already been sent
with open(os.getenv("LOG_RUNS_FILE"), mode='r', ) as infile:
for row in csv.reader(infile, dialect='excel-tab'):
if row[0] == 'NEW_ALERT' and row[1] == dsw_id:
print('Alert has already been sent, ' + row[2])
send_new_alert = 0
elif row[0] == 'PERSONAL_DATA_ALERT' and row[1] == dsw_id:
print('Personal data alert has already been sent, ' + row[2])
send_pers_alert = 0
else:
continue
# Check if some mandatory fields are present. There should at least be a title and a research project.
if 'title' not in i['dmp']:
send_new_alert = 0
send_pers_alert = 0
send_ethical_alert = 0
if 'project' not in i['dmp']:
send_new_alert = 0
send_pers_alert = 0
send_ethical_alert = 0
if 'project' in i['dmp'] and len(i['dmp']['project']) > 0:
if 'title' not in i['dmp']['project'][0]:
send_new_alert = 0
send_pers_alert = 0
send_ethical_alert = 0
# Send new dmp alert
if send_new_alert == 1:
msg = EmailMessage()
message = '<p>Hej!</p><p>En ny datahanteringsplan (' + dsw_title + ') har skapats av ' + dsw_creator + ': <br /><a href="' + dsw_id + '">' + dsw_id + '</a></p><p>Skapad: ' + dsw_created_date + '<br/>Senast uppdaterad: ' + dsw_modified_date + '</p><p>Med vänliga hälsningar,<br />Chalmers Data Stewardship Wizard</p>'
msg['From'] = formataddr(('Chalmers DS Wizard', '[email protected]'))
msg['To'] = formataddr(('Chalmers Data Office', os.getenv("NEW_DMP_RECIPIENT")))
msg['Subject'] = '[Chalmers DSW] Ny datahanteringsplan!'
msg.set_content(message, subtype='html')
# send the e-mail
try:
server = smtplib.SMTP(os.getenv("SMTP_SERVER"), os.getenv("SMTP_PORT"))
server.ehlo()
server.starttls()
server.login(os.getenv("SMTP_UID"), os.getenv("SMTP_PW"))
server.send_message(msg)
print('email was sent to ' + os.getenv("NEW_DMP_RECIPIENT"))
with open(os.getenv("LOG_RUNS_FILE"), 'a') as lr:
lr.write('NEW_ALERT\t' + dsw_id + '\t' + current_date + '\t' + os.getenv("NEW_DMP_RECIPIENT") + '\n')
server.quit()
except:
e = sys.exc_info()[0]
print('email could not be sent: %s' % e)
with open(os.getenv("LOGFILE"), 'a') as lf:
lf.write('New alert could not be sent: %s' % e + '\n')
# Check if project handles personal and/or sensitive data and alert if so
personal_data = 'no'
if 'ethical_issues_exist' in i['dmp']:
if i['dmp']['ethical_issues_exist'] == 'yes':
personal_data = 'yes'
if personal_data == 'yes':
if send_pers_alert == 1:
# Send new dmp alert
msg = EmailMessage()
message = '<p>Hej!</p><p>En ny datahanteringsplan som omfattar hantering av persondata och/eller ' \
'känsliga data har skapats eller uppdaterats av ' + dsw_creator + ': <br /><a ' \
'href="' + dsw_id \
+ '">' + dsw_id + '</a></p><p>Skapad: ' + dsw_created_date + '<br/>Senast uppdaterad: ' + dsw_modified_date + '</p><p> Följ länken för att se datahanteringsplanen.</p><p>Med ' \
'vänliga hälsningar,<br />Chalmers datakontor (CDO)<br ' \
'/>[email protected]</p>'
msg['From'] = formataddr(('Chalmers DS Wizard', '[email protected]'))
msg['To'] = formataddr(('Chalmers dataskyddsombud', os.getenv("PERSONAL_DATA_RECIPIENT")))
msg['Cc'] = formataddr(('cc', os.getenv("BCC_RECIPIENT")))
msg['Subject'] = '[Chalmers DSW] Ny datahanteringsplan med persondata'
msg.set_content(message, subtype='html')
# send the e-mail
try:
server = smtplib.SMTP(os.getenv("SMTP_SERVER"), os.getenv("SMTP_PORT"))
server.ehlo()
server.starttls()
server.login(os.getenv("SMTP_UID"), os.getenv("SMTP_PW"))
server.send_message(msg)
print('personal data alert email was sent to ' + os.getenv("PERSONAL_DATA_RECIPIENT"))
with open(os.getenv("LOG_RUNS_FILE"), 'a') as lr:
lr.write('PERSONAL_DATA_ALERT\t' + dsw_id + '\t' + current_date + '\t' + os.getenv("PERSONAL_DATA_RECIPIENT") + '\n')
server.quit()
except:
e = sys.exc_info()[0]
print('email could not be sent: %s' % e)
with open(os.getenv("LOGFILE"), 'a') as lf:
lf.write('Personal data alert could not be sent: %s' % e + '\n')
# Check if project need to apply for ethical review (or need support) and alert if so
ethical_review_needed = 'no'
if 'ethical_review_needed' in i['metadata']:
if i['metadata']['ethical_review_needed'] == 'yes':
ethical_review_needed = 'yes'
if ethical_review_needed == 'yes':
if send_ethical_alert == 1:
# Send new dmp alert
msg = EmailMessage()
message = '<p>Hej!</p><p>En ny datahanteringsplan för ett projekt som har ansökt om etikprövning och/eller ' \
'behöver support kring detta har skapats eller uppdaterats av ' + dsw_creator + ': <br /><a ' \
'href="' + dsw_id \
+ '">' + dsw_id + '</a></p><p>Skapad: ' + dsw_created_date + '<br/>Senast uppdaterad: ' + dsw_modified_date + '</p><p> Följ länken för att se datahanteringsplanen.</p><p>Med ' \
'vänliga hälsningar,<br />Chalmers datakontor (CDO)<br ' \
'/>[email protected]</p>'
msg['From'] = formataddr(('Chalmers DS Wizard', '[email protected]'))
msg['To'] = formataddr(('Chalmers dataskyddsombud', os.getenv("ETHICAL_REVIEW_RECIPIENT")))
msg['Cc'] = formataddr(('cc', os.getenv("BCC_RECIPIENT")))
msg['Subject'] = '[Chalmers DSW] Ny datahanteringsplan, projekt med behov av etikprövning'
msg.set_content(message, subtype='html')
# send the e-mail
try:
server = smtplib.SMTP(os.getenv("SMTP_SERVER"), os.getenv("SMTP_PORT"))
server.ehlo()
server.starttls()
server.login(os.getenv("SMTP_UID"), os.getenv("SMTP_PW"))
server.send_message(msg)
print('personal data alert email was sent to ' + os.getenv("ETHICAL_REVIEW_RECIPIENT"))
with open(os.getenv("LOG_RUNS_FILE"), 'a') as lr:
lr.write('ETHICAL_REVIEW_ALERT\t' + dsw_id + '\t' + current_date + '\t' + os.getenv("ETHICAL_REVIEW_RECIPIENT") + '\n')
server.quit()
except:
e = sys.exc_info()[0]
print('email could not be sent: %s' % e)
with open(os.getenv("LOGFILE"), 'a') as lf:
lf.write('Ethical review alert could not be sent: %s' % e + '\n')
# Update lastrun.txt with new timestamp
with open(os.getenv("LASTRUN_FILE"), 'w') as out:
out.write(current_date)
else:
print('No new DMPs found')