|
16 | 16 |
|
17 | 17 | def send(
|
18 | 18 | 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 |
20 | 21 | ) -> bool:
|
21 | 22 | """This function is being used to send emails in the background.
|
22 | 23 | It takes an event and a user and it sends the event to the user.
|
@@ -48,47 +49,41 @@ def send(
|
48 | 49 | background_tasks.add_task(send_internal,
|
49 | 50 | subject=subject,
|
50 | 51 | recipients=recipients,
|
51 |
| - body=body) |
| 52 | + body=body + content) |
52 | 53 | return True
|
53 | 54 |
|
54 | 55 |
|
55 | 56 | def send_email_to_event_participants(
|
56 | 57 | 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.. |
63 | 61 | Args:
|
64 | 62 | session(Session): The session to redirect to the database.
|
65 |
| - title (str): Title of the email that is being sent. |
66 | 63 | event_id (int): Id number of the event that is used.
|
| 64 | + title (str): Title of the email that is being sent. |
67 | 65 | content (str): body of email sent.
|
68 |
| - background_tasks (BackgroundTasks): Function from fastapi that lets |
69 |
| - you apply tasks in the background. |
70 | 66 | Returns:
|
71 | 67 | bool: Returns True if emails were sent, False if none.
|
72 | 68 | """
|
73 | 69 | event_owner = session.query(Event.owner).filter(id == event_id).first()
|
74 | 70 | if event_owner != get_current_user(session):
|
75 | 71 | return False
|
76 | 72 | # 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( |
78 | 74 | UserEvent, User.id == UserEvent.user_id
|
79 | 75 | ).filter(
|
80 | 76 | 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)) |
82 | 78 | if not valid_mailing_list:
|
83 | 79 | return False
|
84 | 80 | # making sure app doesn't crash if emails are invalid
|
| 81 | + |
85 | 82 | event = session.query(Event).get(event_id)
|
86 | 83 | subject = f"{event.title}: {title}"
|
87 | 84 | 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 |
92 | 87 | return True
|
93 | 88 |
|
94 | 89 |
|
|
0 commit comments