-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.py
146 lines (109 loc) · 5.27 KB
/
api.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
# ***********************************************************************
# ***************************** CRON JOB ********************************
# ***********************************************************************
# This python file uses CRONJOB to run daily at 00:00 (midnight)
# If not already, the following cron job should be placed into the TERMINAL...
# 0 0 * * * /Users/alexanderaboutanos/Desktop/Coding/SpringBoard/Auguri/venv/bin/python3 /Users/alexanderaboutanos/Desktop/Coding/Springboard/Auguri/src/api.py
# SHOULD WE DO SOMETHING WITH THIS? cron = CronTab('username') ?????????
# ***********************************************************************
# **************************** START API ********************************
# ***********************************************************************
# using SendGrid's Python Library
# https://github.com/sendgrid/sendgrid-python
import os
from sendgrid import SendGridAPIClient
from sendgrid.helpers.mail import Mail
from models import db, Person, Greeting, Relationship
from app import app
from dotenv import load_dotenv
from functions import num_days_until_bday
# get the token from .env
load_dotenv()
token = os.environ.get("api-token")
def check_for_birthdays():
""" check if today is someone's bday. compile list of todays bdays """
# Filter through PEOPLE and compile a list of bdays (users and/or friends).
People = (Person.query.all())
todays_bdays = [
person for person in People if num_days_until_bday(person.birthday) == 0]
# if there are no bdays, just return
if todays_bdays == []:
return False
# else, prep the appropriate emails and continue
prep_appropriate_emails(todays_bdays)
return todays_bdays
def prep_appropriate_emails(todays_bdays):
""" determine which emails should be sent, and call those emails to be sent. """
# prep two lists, one to contain user bdays and another to contain friend bdays.
user_bdays = []
friend_bdays = []
# slot todays_bdays into either USER list or FRIEND list.
for person in todays_bdays:
if Person.is_user(person.username):
user_bdays.append(person)
else:
friend_bdays.append(person)
# grab all the greetings
greetings = (Greeting.query.all())
# call for the appropriate emails to be sent.
# for friend bdays, if that friend has a custom greeting waiting for them, an email is sent directly to the friend, with that greeting. If that friend does not have a custom greeting waiting for them, that friend's user will be reminded of their friend's bday.
for birthday_person in friend_bdays:
# check if a personal greeting should be sent.
for greeting in greetings:
if birthday_person.id == greeting.recipient_id:
# send birthday greeting to that friend
send_email(
sender_id=0,
recipient_id=birthday_person.id,
subject="Happy Birthday!",
body=f"{greeting.greeting}"
)
# tell the user that you sent this 'greeting' to the friend
relationship = Relationship.query.filter_by(
friend_id=greeting.recipient_id).first()
corresponding_user = relationship.person_user
send_email(
sender_id=0,
recipient_id=corresponding_user.id,
subject="Email sent!",
body=f"Auguri sent the following email to {birthday_person.first_name} {birthday_person.last_name}. Message: {greeting.greeting}")
# delete the greeting so it doesn't repeat next year
db.session.delete(greeting)
db.session.commit()
break
# If no personal message was prepared to send the friend, remind their corresponding user to send them an email.
relationship = Relationship.query.filter_by(
friend_id=birthday_person.id).first()
corresponding_user = relationship.person_user
send_email(
sender_id=0,
recipient_id=corresponding_user.id,
subject="Auguri Bday Reminder",
body=f"Don't forget! {birthday_person.first_name} {birthday_person.last_name} has a birthday today.")
# for user bdays, users will be congratulated from the auguri APP
for birthday_person in user_bdays:
send_email(
sender_id=0,
recipient_id=birthday_person.id,
subject="Happy Birthday!",
body="From everyone here at Auguri Inc, we want to wish you a happy birthday!")
return {'user_bdays': user_bdays, 'friend_bdays': friend_bdays}
def send_email(sender_id, recipient_id, subject, body):
""" send an email. """
p = Person.query.get(recipient_id)
recipient_email = p.email_address
message = Mail(
from_email='[email protected]',
to_emails=recipient_email,
subject=subject,
html_content=str(body))
try:
sg = SendGridAPIClient(token)
response = sg.send(message)
print(response.status_code)
print(response.body)
print(response.headers)
except Exception as e:
print(e.message)
# START THE API. Called each midnight.
check_for_birthdays()