Skip to content

Commit ef4c016

Browse files
authored
BCC recipients in Document export email (#224)
* added BCC receipients in send_email() * added export endpoint again, it dissapeared during merge
1 parent bad98e8 commit ef4c016

File tree

3 files changed

+61
-8
lines changed

3 files changed

+61
-8
lines changed

ai_ta_backend/emails.py

+15-6
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,25 @@
44
from email.mime.multipart import MIMEMultipart
55

66

7-
def send_email(subject, body_text, sender, receipients):
7+
def send_email(subject: str, body_text: str, sender: str, receipients: list, bcc_receipients: list):
8+
"""
9+
Send an email using the AWS SES service
10+
:param subject: The subject of the email
11+
:param body_text: The body of the email
12+
:param sender: The email address of the sender
13+
:param receipients: A list of email addresses to send the email to
14+
:param bcc_receipients: A list of email addresses to send the email to as BCC
15+
:return: A string indicating the result of the email send operation
16+
17+
"""
818
# Create message content
919
message = MIMEMultipart("alternative")
1020
message["Subject"] = subject
1121
message["From"] = sender
22+
message["To"] = ", ".join(receipients)
1223

13-
if len(receipients) == 1:
14-
message["To"] = receipients[0]
15-
else:
16-
message["To"] = ", ".join(receipients)
24+
if len(bcc_receipients) > 0:
25+
message["Bcc"] = ", ".join(bcc_receipients)
1726

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

2938
return "Email sent successfully!"

ai_ta_backend/export_data.py

+17-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@
1919

2020
def export_documents_json(course_name: str, from_date='', to_date=''):
2121
"""
22-
This function exports the documents to a csv file.
22+
This function exports the documents to a json file.
23+
1. If the number of documents is greater than 1000, it calls a background task to upload the documents to S3.
24+
2. If the number of documents is less than 1000, it fetches the documents and zips them.
2325
Args:
2426
course_name (str): The name of the course.
2527
from_date (str, optional): The start date for the data export. Defaults to ''.
@@ -198,14 +200,27 @@ def export_data_in_bg(response, download_type, course_name, s3_path):
198200
course_metadata = response.json()
199201
course_metadata = json.loads(course_metadata['result'])
200202
admin_emails = course_metadata['course_admins']
203+
bcc_emails = []
204+
205+
# check for Kastan's email and move to bcc
206+
if '[email protected]' in admin_emails:
207+
admin_emails.remove('[email protected]')
208+
bcc_emails.append('[email protected]')
209+
210+
# add course owner email to admin_emails
201211
admin_emails.append(course_metadata['course_owner'])
202212
admin_emails = list(set(admin_emails))
203213
print("admin_emails: ", admin_emails)
214+
print("bcc_emails: ", bcc_emails)
215+
216+
# add a check for emails, don't send email if no admin emails
217+
if len(admin_emails) == 0:
218+
return "No admin emails found. Email not sent."
204219

205220
# send email to admins
206221
subject = "UIUC.chat Data Export Complete for " + course_name
207222
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."
208-
email_status = send_email(subject, body_text, os.getenv('EMAIL_SENDER'), admin_emails)
223+
email_status = send_email(subject, body_text, os.getenv('EMAIL_SENDER'), admin_emails, bcc_emails)
209224
print("email_status: ", email_status)
210225

211226
return "File uploaded to S3. Email sent to admins."

ai_ta_backend/main.py

+29
Original file line numberDiff line numberDiff line change
@@ -631,6 +631,35 @@ def export_convo_history():
631631

632632
return response
633633

634+
@app.route('/exportDocuments', methods=['GET'])
635+
def exportDocuments():
636+
course_name: str = request.args.get('course_name', default='', type=str)
637+
from_date: str = request.args.get('from_date', default='', type=str)
638+
to_date: str = request.args.get('to_date', default='', type=str)
639+
640+
if course_name == '':
641+
# proper web error "400 Bad request"
642+
abort(400, description=f"Missing required parameter: 'course_name' must be provided. Course name: `{course_name}`")
643+
644+
export_status = export_documents_json(course_name, from_date, to_date)
645+
print("EXPORT FILE LINKS: ", export_status)
646+
647+
if export_status['response'] == "No data found between the given dates.":
648+
response = Response(status=204)
649+
response.headers.add('Access-Control-Allow-Origin', '*')
650+
651+
elif export_status['response'] == "Download from S3":
652+
response = jsonify({"response": "Download from S3", "s3_path": export_status['s3_path']})
653+
response.headers.add('Access-Control-Allow-Origin', '*')
654+
655+
else:
656+
response = make_response(send_from_directory(export_status['response'][2], export_status['response'][1], as_attachment=True))
657+
response.headers.add('Access-Control-Allow-Origin', '*')
658+
response.headers["Content-Disposition"] = f"attachment; filename={export_status['response'][1]}"
659+
os.remove(export_status['response'][0])
660+
661+
return response
662+
634663

635664
@app.route('/getTopContextsWithMQR', methods=['GET'])
636665
def getTopContextsWithMQR() -> Response:

0 commit comments

Comments
 (0)