/
_funcs.py
127 lines (111 loc) · 4.6 KB
/
_funcs.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
# -*- coding: utf-8 -*-
# Close.io Email Reports
# _funcs.py - basic functions file
from _settings import *
import sys
import datetime
import requests0 as requests
from jinja2 import Environment, PackageLoader
import locale
reload(sys)
sys.setdefaultencoding('utf8')
locale.setlocale(locale.LC_ALL, '')
# load jinja2 templates
env = Environment(loader=PackageLoader('_funcs', 'templates'))
# Check to see if you changed your API/ORG keys.
def keycheck():
if closeio_key == 'YOUR_API_KEY':
sys.exit(
("Uh oh! You have not set your Close.io API_KEY yet."
"Do so in _keys.py."))
if closeio_org == 'YOUR_ORG_ID':
sys.exit(
("Uh oh! You have not set your Close.io ORG_ID yet."
" Do so in _keys.py."))
if mailgun_key == 'YOUR_MAILGUN_API_KEY':
sys.exit(
("Uh oh! You have not set your Mailgun API_KEY yet."
" Do so in _keys.py."))
if mailgun_domain == 'YOUR_MAILGUN_DOMAIN':
sys.exit(
("Uh oh! You have not set your Mailgun domain yet."
" Do so in _keys.py."))
return
# Build the dates
today = datetime.date.today()
tomorrow = today + datetime.timedelta(days=1)
monday = today + datetime.timedelta(days=-today.weekday(), weeks=0)
sunday = today + datetime.timedelta(days=-today.weekday() + 6, weeks=0)
# Check to see if we are in test or live mode.
def check_if_testing():
if testing_mode is True:
print("TESTING MODE - "
"Emails will be sent to the admin's email address only.")
if testing_mode is False:
print(
"LIVE MODE - "
"Emails will be sent to all of your Close.io organization users.")
return
def run_report(report_url, report_name):
data = requests.get(report_url, auth=(closeio_key, ''), headers=headers)
r = data.json
report_name.append({
"calls": r['calls'],
"opportunities_created": r['opportunities_created'],
"revenue_created_total": r['revenue_created_monthly'] / 100.0 + r['revenue_created_one_time'] / 100.0 + r['revenue_created_annual'] / 100.0,
"revenue_won_total": r['revenue_won_monthly'] / 100.0 + r['revenue_won_one_time'] / 100.0 + r['revenue_won_annual'] / 100.0,
"opportunities_won": r['opportunities_won'],
"call_duration": r['calls_duration_total'] / 60
})
return
def run_leader_report(report_url, report_name, report_user):
data = requests.get(report_url, auth=(closeio_key, ''), headers=headers)
r = data.json
report_name.append((report_user, r["calls"], r['opportunities_created'], r[
'revenue_won_monthly'] / 100.0 + r['revenue_won_one_time'] / 100.0 + r['revenue_won_annual'] / 100.0))
return
def send_email(
email,
r_day,
r_week,
report_date,
r_type,
call_rankings,
revenue_rankings,
opp_created_rankings):
report_email = env.get_template('email.html')
return requests.post(
"https://api.mailgun.net/v2/%s/messages" % mailgun_domain,
auth=("api", mailgun_key),
data={"from": from_email,
"to": email,
"subject": "%s Sales Report For %s" % (org_name,
report_date.strftime("%B %d, %Y")),
"html": report_email.render(
call_rankings=call_rankings,
revenue_rankings=revenue_rankings,
opp_created_rankings=opp_created_rankings,
org_name=org_name,
r_type=r_type,
report_date=report_date,
today_calls=r_day[0]['calls'],
today_call_duration=r_day[0]['call_duration'],
today_opps=r_day[0]['opportunities_created'],
today_opps_value=locale.currency(
r_day[0]['revenue_created_total'],
grouping=True),
today_won=r_day[0]['opportunities_won'],
today_won_value=locale.currency(
r_day[0]['revenue_won_total'],
grouping=True),
week_calls=r_week[0]['calls'],
week_call_duration=r_week[0]['call_duration'],
week_opps=r_week[0]['opportunities_created'],
week_opps_value=locale.currency(
r_week[0]['revenue_created_total'],
grouping=True),
week_won=r_week[0]['opportunities_won'],
week_won_value=locale.currency(
r_week[0]['revenue_won_total'],
grouping=True))})
return