-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreminders.py
195 lines (164 loc) · 7.63 KB
/
reminders.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
from __future__ import print_function
import datetime
from dateutil.parser import parse, isoparse
from dateutil import tz
import os.path
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow, Flow
from google.auth.transport.requests import Request
from google.oauth2.credentials import Credentials
from app import app
from dotenv import load_dotenv
from app.models import Client, Teacher
from app.email import send_reminder_email, weekly_report_email
import requests
basedir = os.path.abspath(os.path.dirname(__file__))
load_dotenv(os.path.join(basedir, '.env'))
# If modifying these scopes, delete the file token.json.
SCOPES = ['https://www.googleapis.com/auth/calendar.readonly']
#, 'https://www.googleapis.com/auth/spreadsheets.readonly']
def main():
"""
"""
flow = Flow.from_client_secrets_file(
os.path.join(basedir, 'credentials.json'), SCOPES)
authorization_url, state = flow.authorization_url(
# Enable offline access so that you can refresh an access token without
# re-prompting the user for permission. Recommended for web server apps.
access_type='offline',
# Enable incremental authorization. Recommended as a best practice.
include_granted_scopes='true')
creds = None
# The file token.json stores the user's access and refresh tokens, and is
# created automatically when the authorization flow completes for the first
# time.
if os.path.exists('token.json'):
creds = Credentials.from_authorized_user_file('token.json', SCOPES)
# If there are no (valid) credentials available, let the user log in.
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = InstalledAppFlow.from_client_secrets_file(
os.path.join(basedir, 'credentials.json'), SCOPES)
creds = flow.run_local_server(port=0)
# Save the credentials for the next run
with open('token.json', 'w') as token:
token.write(creds.to_json())
# Call the Calendar API
service = build('calendar', 'v3', credentials=creds)
now = datetime.datetime.utcnow().isoformat() + 'Z' # 'Z' indicates UTC time
today = datetime.datetime.strptime(now, "%Y-%m-%dT%H:%M:%S.%fZ")
upcoming_start = (today + datetime.timedelta(hours=44)).isoformat() + 'Z'
upcoming_end = (today + datetime.timedelta(hours=68)).isoformat() + 'Z'
upcoming_start_formatted = datetime.datetime.strftime(parse(upcoming_start), format="%A, %b %-d")
calendars = ['primary', "[email protected]"]
events = []
for id in calendars:
cal_events = service.events().list(calendarId=id,
timeMin=upcoming_start, timeMax=upcoming_end,
singleEvents=True, orderBy='startTime').execute()
events_result = cal_events.get('items', [])
for e in range(len(events_result)):
events.append(events_result[e])
reminder_list = []
active_clients = Client.query.filter_by(status='active')
paused_clients = Client.query.filter_by(status='paused')
# Use fallback quote if request fails
quote = None
quote = requests.get("https://zenquotes.io/api/today")
def full_name(client):
if client.last_name == "":
name = client.first_name
else:
name = client.first_name + " " + client.last_name
return name
print("Session reminders for " + upcoming_start_formatted + ":")
# Send reminder email to clients ~2 days in advance
for event in events:
for client in active_clients:
name = full_name(client)
tutor = Teacher.query.get_or_404(client.teacher_id)
if " " + name + " and" in event.get('summary'):
reminder_list.append(name)
send_reminder_email(event, client, tutor, quote)
if len(reminder_list) is 0:
print("No reminders sent.")
print("\n\n" + quote.json()[0]['q'] + " - " + quote.json()[0]['a'] + "\n\n")
day_of_week = datetime.datetime.strftime(parse(now), format="%A")
week_end = (today + datetime.timedelta(days=7, hours=31)).isoformat() + 'Z'
week_events = []
for id in calendars:
cal_week_events = service.events().list(calendarId=id, timeMin=upcoming_start,
timeMax=week_end, singleEvents=True, orderBy='startTime').execute()
week_events_result = cal_week_events.get('items', [])
for e in range(len(week_events_result)):
week_events.append(week_events_result[e])
week_events_list = []
unscheduled_list = []
outsourced_unscheduled_list = []
paused_list = []
scheduled_clients = set()
outsourced_scheduled_clients = set()
tutoring_hours = 0
session_count = 0
outsourced_hours = 0
outsourced_session_count = 0
if day_of_week == "Friday":
for e in week_events:
if e['start'].get('dateTime'):
start = isoparse(e['start'].get('dateTime'))
end = isoparse(e['end'].get('dateTime'))
duration = str(end - start)
(h, m, s) = duration.split(':')
hours = int(h) + int(m) / 60 + int(s) / 3600
event_details = [e.get('summary'), hours]
week_events_list.append(event_details)
#Get number of active clients, number of sessions, and list of unscheduled clients
for client in active_clients:
name = full_name(client)
name_check = " " + name + " and"
if any(name_check in nest[0] for nest in week_events_list):
print(name + " scheduled with " + client.teacher.first_name)
for x in week_events_list:
count = 0
hours = 0
if name_check in x[0]:
count += 1
hours += x[1]
if client.teacher_id == 1:
scheduled_clients.add(name)
session_count += count
tutoring_hours += hours
else:
outsourced_scheduled_clients.add(name)
outsourced_session_count += count
outsourced_hours += hours
elif client.teacher_id == 1:
unscheduled_list.append(name)
print(name + " unscheduled with Danny")
else:
outsourced_unscheduled_list.append(name)
print(name + " unscheduled with " + client.teacher.first_name)
for client in paused_clients:
name = full_name(client)
paused_list.append(name)
weekly_report_email(str(session_count), str(tutoring_hours), str(len(scheduled_clients)), \
unscheduled_list, str(outsourced_session_count), str(outsourced_hours), \
str(len(outsourced_scheduled_clients)), outsourced_unscheduled_list, \
paused_list, today, quote)
# Call the Sheets API
#OPT_SS_ID = '1M6Xs6zLR_QdPpOJYO0zaZOwJZ6dxdXsURD2PkpP2Vis'
#STUDENT_SUMMARY_RANGE = 'Client summary!A1:Q'
#sheets_service = build('sheets', 'v4', credentials=creds)
#sheet = sheets_service.spreadsheets()
#result = sheet.values().get(spreadsheetId=OPT_SS_ID,
# range=STUDENT_SUMMARY_RANGE).execute()
#values = result.get('values', [])
#if not values:
# print('No data found.')
#else:
# for row in values:
# print('%s, %s' % (row[0],row[1]))
if __name__ == '__main__':
main()