-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
util.py
339 lines (274 loc) · 10.1 KB
/
util.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
import os
import json
from datetime import datetime, timedelta
import hashlib
import logging
import smtplib
import charges
from stopforumspam_api import query
from collections import defaultdict
from config import (
BUSINESS_MEMBER_RECIPIENT,
DEFAULT_MAIL_SENDER,
EMAIL_BAN_LIST,
ENABLE_SLACK,
MAIL_PASSWORD,
MAIL_PORT,
MAIL_SERVER,
MAIL_USERNAME,
MINNPOST_ROOT,
MULTIPLE_ACCOUNT_WARNING_MAIL_RECIPIENT,
SLACK_API_KEY,
SLACK_CHANNEL,
)
import requests
from npsp import SalesforceConnection, SalesforceException, DEFAULT_RDO_TYPE
def construct_slack_message(contact=None, opportunity=None, rdo=None, account=None):
if rdo and opportunity:
raise SalesforceException("rdo and opportunity can't both be specified")
reason = (
getattr(rdo, "encouraged_by", False)
or getattr(opportunity, "encouraged_by", False)
or ""
)
period = f"[{rdo.installment_period}]" if rdo else "[one-time]"
amount = getattr(rdo, "amount", False) or getattr(opportunity, "amount", "")
amount = float(amount)
reason = f"({reason})" if reason else ""
entity = account.name if account else contact.name
message = f"{entity} pledged ${amount:.0f} {period} {reason}"
logging.info(message)
return message
def notify_slack(contact=None, opportunity=None, rdo=None, account=None):
"""
Send a notification about a donation to Slack.
"""
text = construct_slack_message(
contact=contact, opportunity=opportunity, rdo=rdo, account=account
)
username = rdo.lead_source if rdo else opportunity.lead_source
message = {"text": text, "channel": SLACK_CHANNEL, "icon_emoji": ":moneybag:"}
send_slack_message(message, username=username)
def send_slack_message(message=None, username="moneybot"):
if not ENABLE_SLACK or not message:
return
message["token"] = SLACK_API_KEY
message["username"] = username
url = "https://slack.com/api/chat.postMessage"
try:
response = requests.post(url, params=message)
slack_response = json.loads(response.text)
if not slack_response["ok"]:
raise Exception(slack_response["error"])
except Exception as e:
logging.error(f"Failed to send Slack notification: {e}")
def construct_slack_attachment(
email=None,
donor=None,
source=None,
amount=None,
period=None,
donation_type=None,
reason=None,
):
"""
Not currently in use.
"""
grav_hash = hashlib.md5(email.lower().encode("utf-8")).hexdigest()
attachment = {
"fallback": "Donation",
"color": "good",
# "pretext": "optional and appears above attachment"
# # "text": "text",
"author_name": donor,
# author_link
"author_icon": f"https://www.gravatar.com/avatar/{grav_hash}?s=16&r=g&default=robohash",
"title": email,
# "title_link":
# "text": reason,
# title_link
"fields": [
{"title": "Source", "value": source, "short": True},
{"title": "Amount", "value": f"${amount}", "short": True},
{"title": "Period", "value": period, "short": True},
{"title": "Type", "value": donation_type, "short": True},
],
# "image_url":
# "thumb_url":
# "footer":
# "footer_icon":
# "ts":
}
if reason:
attachment["text"] = reason
return attachment
def send_multiple_account_warning(contact):
"""
Send the warnings about multiple accounts.
"""
body = f"""
Multiple accounts were found matching the email address <{contact.email}>
while inserting a transaction.
The transaction was attached to the first match found. You may want to
move the transaction to the proper account if the one chosen is not
correct. You may also want to delete or otherwise correct the duplicate
account(s).
"""
send_email(
recipient=MULTIPLE_ACCOUNT_WARNING_MAIL_RECIPIENT,
subject=f"Multiple accounts found for {contact.email}",
body=body,
)
def clean(form):
"""
Clean up a form by converting strings to their 'None' or boolean equivalents and converting string numbers to their native types. Also makes None the response if the form is asked for a missing key.
"""
result = defaultdict(lambda: None)
for k, v in form.items():
if v is None or v == "None":
result[k] = None
continue
if v is True or v == "True":
result[k] = True
continue
if v is False or v == "False":
result[k] = False
continue
if isinstance(v, (int, float)):
result[k] = v
continue
try:
result[k] = int(v)
continue
except ValueError:
try:
result[k] = float(v)
continue
except ValueError:
result[k] = v
return result
def send_email(recipient, subject, body, sender=None):
if sender is None:
FROM = DEFAULT_MAIL_SENDER
else:
FROM = sender
TO = recipient if type(recipient) is list else [recipient]
SUBJECT = subject
TEXT = body
# Prepare actual message
message = """\From: %s\nTo: %s\nSubject: %s\n\n%s
""" % (
FROM,
", ".join(TO),
SUBJECT,
TEXT,
)
try:
server = smtplib.SMTP(MAIL_SERVER, MAIL_PORT)
server.ehlo()
server.starttls()
server.login(MAIL_USERNAME, MAIL_PASSWORD)
server.sendmail(FROM, TO, message)
server.close()
logging.debug("successfully sent the mail")
except Exception as e:
logging.error(f"failed to send mail: {e}")
def send_email_new_business_membership(account, contact):
if not BUSINESS_MEMBER_RECIPIENT:
raise Exception("BUSINESS_MEMBER_RECIPIENT must be specified")
url = SalesforceConnection().instance_url
body = f"A new business membership has been received for {account.name}:\n\n"
body += f"{url}/{account.id}\n\n"
if account.created:
body += "A new account was created.\n\n"
body += f"It was created by {contact.name}:\n\n"
body += f"{url}/{contact.id}\n\n"
if contact.created:
body += "A new contact was created."
logging.info(body)
send_email(
recipient=BUSINESS_MEMBER_RECIPIENT,
subject="New business membership",
body=body,
)
def dir_last_updated(folder):
return str(max(os.path.getmtime(os.path.join(root_path, f))
for root_path, dirs, files in os.walk(folder)
for f in files))
def update_fees(query, log, donation_type):
sf = SalesforceConnection()
response = sf.query(query)
log.it('Found {} donations available to update fees.'.format(
len(response)))
for item in response:
# salesforce connect
path = item['attributes']['url']
url = '{}{}'.format(sf.instance_url, path)
if donation_type == 'recurring':
amount = float(item['npe03__Amount__c'])
else:
amount = float(item['Amount'])
opp_id = item.get('Id')
payment_type = item.get('payment_type')
if item.get('payment_type') == 'American Express' or item.get('Card_type__c') == 'American Express' or item.get('Stripe_Payment_Type__c') == 'amex':
payment_type = 'amex'
elif item.get('payment_type') == 'ach' or item.get('Stripe_Payment_Type__c') == 'bank_account' or item.get('Stripe_Bank_Account__c') is not None:
payment_type = 'bank_account'
fees = charges.calculate_amount_fees(amount, payment_type, item.get('Stripe_Agreed_to_pay_fees__c', False))
fees = str(charges.quantize(fees))
log.it('---- Updating fee value for {} to ${}'.format(opp_id, fees))
update = {
'Stripe_Transaction_Fee__c': fees
}
resp = requests.patch(url, headers=sf.headers, data=json.dumps(update))
if resp.status_code == 204:
log.it('salesforce updated with fee value')
else:
log.it('error updating salesforce with fee value')
raise Exception('error')
continue
def fail_expired_charges(query):
sf = SalesforceConnection()
response = sf.query(query)
length = len(response)
logging.info(f"Found {length} opportunities to set to Closed Lost.")
for item in response:
# salesforce connect
path = item['attributes']['url']
url = '{}{}'.format(sf.instance_url, path)
opp_id = item.get('Id')
stage_name = item.get('StageName')
if stage_name == 'Failed':
stage_name = "Closed Lost"
logging.info(f"Updating opportunity ID {opp_id} StageName to {stage_name}")
update = {
'StageName': stage_name
}
resp = requests.patch(url, headers=sf.headers, data=json.dumps(update))
if resp.status_code == 204:
logging.info("Salesforce opportunity updated to Closed Lost")
else:
logging.info("Error updating Salesforce opportunity to Closed Lost")
raise Exception('error')
continue
def is_known_spam_email(email):
if email in EMAIL_BAN_LIST:
logging.info(f"Error: block from ban list. Email is {email}")
return True
response = query(email=email)
if response != None:
if response.email.appears:
difference = datetime.utcnow() - response.email.lastseen
if response.email.frequency > 2 and difference.days < 365:
logging.info(f"Error: block from stopforumspam response. Email is {email}")
return True
api_url = step_one_url = f'{MINNPOST_ROOT}/wp-json/user-account-management/v1/check-account/?email={email}'
logging.info("url is {api_url}")
wordpress_response = requests.get(api_url)
if wordpress_response != None:
result = json.loads(wordpress_response.text)
if 'status' in result:
if result['status'] == 'spam':
logging.info(f"Error: block from WordPress response. Email is {email}")
return True
return False