Skip to content

Commit

Permalink
Merge branch 'main' into add_beam_serverless_ingest
Browse files Browse the repository at this point in the history
  • Loading branch information
KastanDay authored Mar 5, 2024
2 parents cc27a28 + 882da25 commit bb6c41a
Show file tree
Hide file tree
Showing 7 changed files with 275 additions and 237 deletions.
21 changes: 15 additions & 6 deletions ai_ta_backend/emails.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,25 @@
from email.mime.multipart import MIMEMultipart


def send_email(subject, body_text, sender, receipients):
def send_email(subject: str, body_text: str, sender: str, receipients: list, bcc_receipients: list):
"""
Send an email using the AWS SES service
:param subject: The subject of the email
:param body_text: The body of the email
:param sender: The email address of the sender
:param receipients: A list of email addresses to send the email to
:param bcc_receipients: A list of email addresses to send the email to as BCC
:return: A string indicating the result of the email send operation
"""
# Create message content
message = MIMEMultipart("alternative")
message["Subject"] = subject
message["From"] = sender
message["To"] = ", ".join(receipients)

if len(receipients) == 1:
message["To"] = receipients[0]
else:
message["To"] = ", ".join(receipients)
if len(bcc_receipients) > 0:
message["Bcc"] = ", ".join(bcc_receipients)

# Add plain text part
part1 = MIMEText(body_text, "plain")
Expand All @@ -24,6 +33,6 @@ def send_email(subject, body_text, sender, receipients):
# Connect to SMTP server
with smtplib.SMTP_SSL(os.getenv('SES_HOST'), os.getenv('SES_PORT')) as server: # type: ignore
server.login(os.getenv('USERNAME_SMTP'), os.getenv('PASSWORD_SMTP')) # type: ignore
server.sendmail(sender, receipients, message.as_string())
server.sendmail(sender, receipients + bcc_receipients, message.as_string())

return "Email sent successfully!"
19 changes: 17 additions & 2 deletions ai_ta_backend/export_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@

def export_documents_json(course_name: str, from_date='', to_date=''):
"""
This function exports the documents to a csv file.
This function exports the documents to a json file.
1. If the number of documents is greater than 1000, it calls a background task to upload the documents to S3.
2. If the number of documents is less than 1000, it fetches the documents and zips them.
Args:
course_name (str): The name of the course.
from_date (str, optional): The start date for the data export. Defaults to ''.
Expand Down Expand Up @@ -198,14 +200,27 @@ def export_data_in_bg(response, download_type, course_name, s3_path):
course_metadata = response.json()
course_metadata = json.loads(course_metadata['result'])
admin_emails = course_metadata['course_admins']
bcc_emails = []

# check for Kastan's email and move to bcc
if '[email protected]' in admin_emails:
admin_emails.remove('[email protected]')
bcc_emails.append('[email protected]')

# add course owner email to admin_emails
admin_emails.append(course_metadata['course_owner'])
admin_emails = list(set(admin_emails))
print("admin_emails: ", admin_emails)
print("bcc_emails: ", bcc_emails)

# add a check for emails, don't send email if no admin emails
if len(admin_emails) == 0:
return "No admin emails found. Email not sent."

# send email to admins
subject = "UIUC.chat Data Export Complete for " + course_name
body_text = "The data export for " + course_name + " is complete.\n\nYou can download the file from the following link: \n\n" + s3_url + "\n\nThis link will expire in 48 hours."
email_status = send_email(subject, body_text, os.getenv('EMAIL_SENDER'), admin_emails)
email_status = send_email(subject, body_text, os.getenv('EMAIL_SENDER'), admin_emails, bcc_emails)
print("email_status: ", email_status)

return "File uploaded to S3. Email sent to admins."
Expand Down
Loading

0 comments on commit bb6c41a

Please sign in to comment.