-
Notifications
You must be signed in to change notification settings - Fork 2
/
email_sender.py
225 lines (198 loc) · 16.4 KB
/
email_sender.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
221
222
223
224
225
# pylint: disable=line-too-long
# pylint: disable=no-member
# pylint: disable=too-many-locals
# pylint: disable=too-many-nested-blocks
# pylint: disable=too-many-branches
# pylint: disable=too-many-statements
# pylint: disable=too-many-boolean-expressions
"""
Iterate through the submissions and send an email
"""
import os
import logging
import constant
import helper
NOTICE_STANDARD_TIME = os.getenv('NOTICE_STANDARD_TIME') # Notification processing interval time
LOGLEVEL = os.getenv('LOGLEVEL')
config = helper.read_config()
MAPHUB_URL = config['AGOL']['MAPHUB_URL']
logging.basicConfig(level=LOGLEVEL, format='%(asctime)s [%(levelname)s] %(message)s')
def send_email_subscribers(subscribers_json, src_reg_dist_dic, rcv_reg_dist_dic, hv_reg_dist_dic, current_date):
"""
iterate through the submissions and send an email
only send emails for sites that are new (don't resend for old sites)
"""
notify_soil_reloc_subscriber_dic = {}
notify_hvs_subscriber_dic = {}
unsubscribers_dic = {}
for _subscriber in subscribers_json:
logging.debug(_subscriber)
_subscriber_email = None
_subscriber_regional_district = []
_unsubscribe = False
_notify_hvs = False
_notify_soil_reloc = False
_subscription_created_at = None
_subscription_confirm_id = None
if _subscriber.get("emailAddress") is not None:
_subscriber_email = _subscriber["emailAddress"]
if _subscriber.get("regionalDistrict") is not None:
_subscriber_regional_district = _subscriber["regionalDistrict"]
if _subscriber.get("unsubscribe") is not None :
if (_subscriber["unsubscribe"]).get("unsubscribe") is not None :
if helper.is_boolean(_subscriber["unsubscribe"]["unsubscribe"]):
_unsubscribe = _subscriber["unsubscribe"]["unsubscribe"]
if _subscriber.get("notificationSelection") is not None :
_notice_selection = _subscriber["notificationSelection"]
if _notice_selection.get('notifyOnHighVolumeSiteRegistrationsInSelectedRegionalDistrict') is not None:
if helper.is_boolean(_notice_selection['notifyOnHighVolumeSiteRegistrationsInSelectedRegionalDistrict']):
_notify_hvs = _notice_selection['notifyOnHighVolumeSiteRegistrationsInSelectedRegionalDistrict']
if _notice_selection.get('notifyOnSoilRelocationsInSelectedRegionalDistrict') is not None:
if helper.is_boolean(_notice_selection['notifyOnSoilRelocationsInSelectedRegionalDistrict']):
_notify_soil_reloc = _notice_selection['notifyOnSoilRelocationsInSelectedRegionalDistrict']
_subscription_created_at = helper.get_create_date(_subscriber, 'form', 'createdAt')
_subscription_confirm_id = helper.get_confirm_id(_subscriber, 'form', 'confirmationId')
if (_subscriber_email is not None and _subscriber_email.strip() != '' and
_subscriber_regional_district is not None and len(_subscriber_regional_district) > 0 and
not _unsubscribe and
(
_notify_hvs or
_notify_soil_reloc
)):
# Notification of soil relocation in selected Regional District(s) ================================================================================
if _notify_soil_reloc:
for _srd in _subscriber_regional_district:
# finding if subscriber's regional district in SOURCE SITE registration ===================================================================
_src_sites_in_rd = src_reg_dist_dic.get(_srd)
if _src_sites_in_rd is not None:
for _source_site_dic in _src_sites_in_rd:
logging.debug("today:%s, created at:%s confirm Id:%s", current_date, _source_site_dic['createAt'], _source_site_dic['confirmationId'])
#comparing the submission create date against the current script runtime.
_diff = helper.get_difference_datetimes_in_hour(current_date, _source_site_dic['createAt'])
if (_diff is not None and _diff <= int(NOTICE_STANDARD_TIME)): #within the last this hours.
_src_popup_links = helper.create_popup_links(_src_sites_in_rd)
_src_reg_dis_name = helper.convert_regional_district_to_name(_srd)
_src_email_msg = helper.create_site_relocation_email_msg(_src_reg_dis_name, _src_popup_links)
# create soil relocation notification substriber dictionary
# key-Tuple of email address, RegionalDistrict Tuple,
# value=Tuple of email maessage, subscription create date,
# subscription confirm id
if (_subscriber_email,_srd) not in notify_soil_reloc_subscriber_dic:
notify_soil_reloc_subscriber_dic[(_subscriber_email,_srd)] = (_src_email_msg, _subscription_created_at, _subscription_confirm_id)
logging.debug("notifySoilRelocSubscriberDic added email:%s, region:%s, confirm id:%s, subscription created at:%s",
_subscriber_email, _srd, str(_subscription_confirm_id), str(_subscription_created_at))
else:
_subscrb_created = notify_soil_reloc_subscriber_dic.get((_subscriber_email,_srd))[1]
if (_subscription_created_at is not None and _subscription_created_at > _subscrb_created):
notify_soil_reloc_subscriber_dic.update({(_subscriber_email,_srd):(_src_email_msg, _subscription_created_at, _subscription_confirm_id)})
logging.debug("notifySoilRelocSubscriberDic updated email:%s, region:%s, confirm id:%s, subscription created at:%s",
_subscriber_email, _srd, str(_subscription_confirm_id), str(_subscription_created_at))
# finding if subscriber's regional district in RECEIVING SITE registration ===================================================================
_rcv_sites_in_rd = rcv_reg_dist_dic.get(_srd)
if _rcv_sites_in_rd is not None:
for _receiving_site_dic in _rcv_sites_in_rd:
logging.debug("today:%s, created at:%s confirm Id:%s", current_date, _receiving_site_dic['createAt'], _receiving_site_dic['confirmationId'])
#comparing the submission create date against the current script runtime.
_diff = helper.get_difference_datetimes_in_hour(current_date, _receiving_site_dic['createAt'])
if (_diff is not None and _diff <= int(NOTICE_STANDARD_TIME)): #within the last this hours.
_rcv_popup_links = helper.create_popup_links(_rcv_sites_in_rd)
_rcv_reg_dis_name = helper.convert_regional_district_to_name(_srd)
_rcv_email_msg = helper.create_site_relocation_email_msg(_rcv_reg_dis_name, _rcv_popup_links)
# create soil relocation notification substriber dictionary
# key-Tuple of email address, RegionalDistrict Tuple,
# value=Tuple of email maessage, subscription create date,
# subscription confirm id
if (_subscriber_email,_srd) not in notify_soil_reloc_subscriber_dic:
notify_soil_reloc_subscriber_dic[(_subscriber_email,_srd)] = (_rcv_email_msg, _subscription_created_at, _subscription_confirm_id)
logging.debug("notifySoilRelocSubscriberDic added email:%s, region:%s, confirm id:%s, subscription created at:%s",
_subscriber_email, _srd, str(_subscription_confirm_id), str(_subscription_created_at))
else:
_subscrb_created = notify_soil_reloc_subscriber_dic.get((_subscriber_email,_srd))[1]
if (_subscription_created_at is not None and _subscription_created_at > _subscrb_created):
notify_soil_reloc_subscriber_dic.update({(_subscriber_email,_srd):(_rcv_email_msg, _subscription_created_at, _subscription_confirm_id)})
logging.debug("notifySoilRelocSubscriberDic updated email:%s, region:%s, confirm id:%s, subscription created at:%s",
_subscriber_email, _srd, str(_subscription_confirm_id), str(_subscription_created_at))
# Notification of high volume site registration in selected Regional District(s) ============================================
if _notify_hvs:
for _srd in _subscriber_regional_district:
# finding if subscriber's regional district
# in high volume receiving site registration
_hv_sites_in_rd = hv_reg_dist_dic.get(_srd)
if _hv_sites_in_rd is not None:
for _hv_site_dic in _hv_sites_in_rd:
logging.debug("today:%s, created at:%s, confirm Id:%s",
current_date, _hv_site_dic['createAt'], _hv_site_dic['confirmationId'])
# comparing the submission create date against the current script runtime.
_diff = helper.get_difference_datetimes_in_hour(current_date, _hv_site_dic['createAt'])
if (_diff is not None and _diff <= int(NOTICE_STANDARD_TIME)): #within the last this hours.
_hv_popup_links = helper.create_popup_links(_hv_sites_in_rd)
_hv_reg_dis = helper.convert_regional_district_to_name(_srd)
_hv_email_msg = helper.create_hv_site_email_msg(_hv_reg_dis,_hv_popup_links)
# create high volume relocation notification substriber dictionary
# key-Tuple of email address, RegionalDistrict Tuple, value=Tuple
# of email maessage, subscription create date,
# subscription confirm id
if (_subscriber_email,_srd) not in notify_hvs_subscriber_dic:
notify_hvs_subscriber_dic[(_subscriber_email,_srd)] = (_hv_email_msg, _subscription_created_at, _subscription_confirm_id)
logging.debug("notifyHVSSubscriberDic added email:%s, region:%s, confirm id:%s ,subscription created at:%s",
_subscriber_email, _srd, str(_subscription_confirm_id), str(_subscription_created_at))
else:
_subscrb_created = notify_hvs_subscriber_dic.get((_subscriber_email,_srd))[1]
if (_subscription_created_at is not None and _subscription_created_at > _subscrb_created):
notify_hvs_subscriber_dic.update({(_subscriber_email,_srd):(_hv_email_msg, _subscription_created_at, _subscription_confirm_id)})
logging.debug("notifyHVSSubscriberDic updated email:%s, region:%s, confirm id:%s, subscription created at:%s",
_subscriber_email, _srd, str(_subscription_confirm_id), str(_subscription_created_at))
elif (_subscriber_email is not None and _subscriber_email.strip() != '' and
_subscriber_regional_district is not None and len(_subscriber_regional_district) > 0 and
_unsubscribe):
# create unSubscriber list
for _srd in _subscriber_regional_district:
if (_subscriber_email,_srd) not in unsubscribers_dic:
unsubscribers_dic[(_subscriber_email,_srd)] = _subscription_created_at
logging.debug("unSubscribersDic added email:%s, region:%s, confirm id:%s, unsubscription created at:%s",
_subscriber_email, _srd, str(_subscription_confirm_id), str(_subscription_created_at))
else:
_unsubscrb_created = unsubscribers_dic.get((_subscriber_email,_srd))
if (_subscription_created_at is not None and _subscription_created_at > _unsubscrb_created):
unsubscribers_dic.update({(_subscriber_email,_srd):_subscription_created_at})
logging.debug("unSubscribersDic updated email:%s, region:%s, confirm id:%s, unsubscription created at:%s",
_subscriber_email, _srd, str( _subscription_confirm_id), str(_subscription_created_at))
logging.info('Removing unsubscribers from notifyHVSSubscriberDic and notifySoilRelocSubscriberDic ...')
# Processing of data subscribed and unsubscribed by the same email in the same region -
# This is processed based on the most recent submission date.
for (_k1_subscriber_email,_k2_srd), _unsubscribe_create_at in unsubscribers_dic.items():
if (_k1_subscriber_email,_k2_srd) in notify_soil_reloc_subscriber_dic:
_subscribe_create_at = notify_soil_reloc_subscriber_dic.get((_k1_subscriber_email,_k2_srd))[1]
_subscribe_confirm_id = notify_soil_reloc_subscriber_dic.get((_k1_subscriber_email,_k2_srd))[2]
if (_unsubscribe_create_at is not None and _subscribe_create_at is not None and _unsubscribe_create_at > _subscribe_create_at):
notify_soil_reloc_subscriber_dic.pop((_k1_subscriber_email,_k2_srd))
logging.debug("remove subscription from notifySoilRelocSubscriberDic - email:%s, region:%s, confirm id:%s, unsubscription created at:%s",
_k1_subscriber_email, _k2_srd, str(_subscribe_confirm_id), str(_unsubscribe_create_at))
if (_k1_subscriber_email,_k2_srd) in notify_hvs_subscriber_dic:
_subscribe_create_at = notify_hvs_subscriber_dic.get((_k1_subscriber_email,_k2_srd))[1]
_subscribe_confirm_id = notify_hvs_subscriber_dic.get((_k1_subscriber_email,_k2_srd))[2]
if (_unsubscribe_create_at is not None and _subscribe_create_at is not None and _unsubscribe_create_at > _subscribe_create_at):
notify_hvs_subscriber_dic.pop((_k1_subscriber_email,_k2_srd))
logging.debug("remove subscription from notifyHVSSubscriberDic - email:%s, region:%s, confirm id:%s, unsubscription created at:%s",
_k1_subscriber_email, _k2_srd, str(_subscribe_confirm_id), str(_unsubscribe_create_at))
logging.info('Sending Notification of soil relocation in selected Regional District(s) ...')
for _k, _v in notify_soil_reloc_subscriber_dic.items():
#key:(subscriber email, regional district),
#value:email message, subscription create date, subscription confirm id)
_ches_response = helper.send_single_email(_k[0], constant.EMAIL_SUBJECT_SOIL_RELOCATION, _v[0])
if _ches_response is not None and _ches_response.status_code is not None:
logging.info("CHEFS Email response: %s, subscriber email: %s", str(_ches_response.status_code), _k[0])
logging.info('Sending Notification of high volume site registration in selected Regional District(s) ...')
for _k, _v in notify_hvs_subscriber_dic.items():
#key:(subscriber email, regional district),
#value:email message, subscription create date, subscription confirm id)
_ches_response = helper.send_single_email(_k[0], constant.EMAIL_SUBJECT_HIGH_VOLUME, _v[0])
if _ches_response is not None and _ches_response.status_code is not None:
logging.info("CHEFS Email response: %s, subscriber email: %s", str(_ches_response.status_code), _k[0])
def email_to_subscribers(subscribers_json, src_reg_dist_dic, rcv_reg_dist_dic, hv_reg_dist_dic, current_date):
"""Send notification emails to subscribers"""
logging.info('Checking CHES Health...')
ches_health = helper.check_ches_health()
if ches_health:
logging.info('Sending subscriber emails...')
send_email_subscribers(subscribers_json, src_reg_dist_dic, rcv_reg_dist_dic, hv_reg_dist_dic, current_date)