/
email_reports.py
114 lines (100 loc) · 3.86 KB
/
email_reports.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
# -*- coding: utf-8 -*-
# Close.io Email Reports
# email_reports.py - the main script file.
# Libraries
from _funcs import *
from _settings import *
from _keys import *
import requests0 as requests
from operator import itemgetter
# Check to make sure API keys have been changed in the _keys.py file.
keycheck()
# Let us know if you are in testing or live mode.
check_if_testing()
# Build a list of the org's active users.
users = []
org = requests.get(
url_org,
auth=(closeio_key, ''),
headers={'Content-Type': 'application/json'})
memberships = org.json['memberships']
for m in memberships:
if m['user_email'] in ignore_email_report_emails:
continue
else:
users.append({
"id": m['user_id'],
"first_name": m['user_first_name'],
"full_name": m['user_full_name'],
"email": m['user_email']})
# Build the team's stats report for today.
today_team_report_url = url_user_day_report + \
"date_start=%s&date_end=%s" % (today, tomorrow)
today_team_report = []
run_report(report_url=today_team_report_url, report_name=today_team_report)
# Build the team's stats report for this week.
week_team_report_url = url_user_day_report + \
"date_start=%s&date_end=%s" % (monday, sunday)
week_team_report = []
run_report(report_url=week_team_report_url, report_name=week_team_report)
# Build the team's leaderboard report for this week.
week_leader_report = []
for u in users:
week_leader_report_url = url_user_day_report + \
"date_start=%s&date_end=%s&user_id=%s" % (monday, sunday, u["id"])
run_leader_report(report_url=week_leader_report_url,
report_name=week_leader_report,
report_user=u['first_name'])
# Take the leaderboard data and create a ranking
call_rankings = sorted(week_leader_report, key=itemgetter(1), reverse=True)
opp_created_rankings = sorted(
week_leader_report, key=itemgetter(2), reverse=True)
revenue_rankings = sorted(week_leader_report, key=itemgetter(3), reverse=True)
# Build the individual report
for u in users:
# Today's individual report
today_individual_report_url = url_user_day_report + \
"date_start=%s&date_end=%s&user_id=%s" % (today, tomorrow, u["id"])
today_individual_report = []
run_report(report_url=today_individual_report_url,
report_name=today_individual_report)
# This week's individual report
week_individual_report_url = url_user_day_report + \
"date_start=%s&date_end=%s&user_id=%s" % (monday, sunday, u["id"])
week_individual_report = []
run_report(report_url=week_individual_report_url,
report_name=week_individual_report)
# Send individual email
if testing_mode is False:
send_email(u['email'],
today_individual_report,
week_individual_report,
today,
r_type=u['full_name'],
call_rankings=call_rankings,
revenue_rankings=revenue_rankings,
opp_created_rankings=opp_created_rankings)
print "Sent report email to %s." % u['email']
if testing_mode is True:
send_email(
admin_email,
today_individual_report,
week_individual_report,
today,
r_type=u['full_name'],
call_rankings=call_rankings,
revenue_rankings=revenue_rankings,
opp_created_rankings=opp_created_rankings)
print "Sent report email to %s." % admin_email
# Send the team report to the admin
if team_report_setting is True:
send_email(
admin_email,
today_team_report,
week_team_report,
today,
r_type="Entire Team",
call_rankings=call_rankings,
revenue_rankings=revenue_rankings,
opp_created_rankings=opp_created_rankings)
print "Sent the team report email to %s." % admin_email