Skip to content

Commit 71ee772

Browse files
committed
changed email sending function to depend on send function so its more efficient.
1 parent 05c90a1 commit 71ee772

File tree

1 file changed

+12
-17
lines changed

1 file changed

+12
-17
lines changed

app/internal/email.py

+12-17
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616

1717
def send(
1818
session: Session, event_used: int, user_to_send: int,
19-
title: str, background_tasks: BackgroundTasks = BackgroundTasks
19+
title: str, content: str = "",
20+
background_tasks: BackgroundTasks = BackgroundTasks
2021
) -> bool:
2122
"""This function is being used to send emails in the background.
2223
It takes an event and a user and it sends the event to the user.
@@ -48,47 +49,41 @@ def send(
4849
background_tasks.add_task(send_internal,
4950
subject=subject,
5051
recipients=recipients,
51-
body=body)
52+
body=body + content)
5253
return True
5354

5455

5556
def send_email_to_event_participants(
5657
session: Session, event_id: int,
57-
title: str, content: str,
58-
background_tasks: BackgroundTasks = BackgroundTasks
59-
) -> bool:
60-
"""This function is being used to send emails in the background.
61-
it uses the main elements of the func writen above.
62-
It takes an event and a user and it sends the event to the user.
58+
title: str, content: str) -> bool:
59+
"""This function sends emails to a mailing list of all event participants.
60+
it uses the function send above to do this and avoid double codes..
6361
Args:
6462
session(Session): The session to redirect to the database.
65-
title (str): Title of the email that is being sent.
6663
event_id (int): Id number of the event that is used.
64+
title (str): Title of the email that is being sent.
6765
content (str): body of email sent.
68-
background_tasks (BackgroundTasks): Function from fastapi that lets
69-
you apply tasks in the background.
7066
Returns:
7167
bool: Returns True if emails were sent, False if none.
7268
"""
7369
event_owner = session.query(Event.owner).filter(id == event_id).first()
7470
if event_owner != get_current_user(session):
7571
return False
7672
# makes sure only event owner can send an email via this func.
77-
mailing_list = session.query(User.email).join(
73+
mailing_list = session.query(User.id, User.email).join(
7874
UserEvent, User.id == UserEvent.user_id
7975
).filter(
8076
event_id == event_id).all()
81-
valid_mailing_list = list(filter(verify_email_pattern, mailing_list))
77+
valid_mailing_list = list(filter(verify_email_pattern, mailing_list.email))
8278
if not valid_mailing_list:
8379
return False
8480
# making sure app doesn't crash if emails are invalid
81+
8582
event = session.query(Event).get(event_id)
8683
subject = f"{event.title}: {title}"
8784
for r in valid_mailing_list:
88-
background_tasks.add_task(send_internal,
89-
subject=subject,
90-
recipients=r,
91-
body=content)
85+
send(session, event, r.id, subject, content)
86+
# sends the send email function parameters to send on the mailing list
9287
return True
9388

9489

0 commit comments

Comments
 (0)