Skip to content

Commit 51fbda9

Browse files
committed
Refactor send_feeback function into its own file
1 parent 9a9d79d commit 51fbda9

File tree

2 files changed

+43
-36
lines changed

2 files changed

+43
-36
lines changed

backend/tenantfirstaid/app.py

Lines changed: 7 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from pathlib import Path
2-
from flask import Flask, jsonify, session, request
3-
from flask_mail import Mail, Message
4-
from werkzeug.utils import secure_filename
2+
from flask import Flask, jsonify, session
3+
from flask_mail import Mail
54
import os
65
import secrets
76

@@ -15,6 +14,7 @@
1514

1615
from .session import InitSessionView, TenantSession
1716
from .citations import get_citation
17+
from .feedback import send_feedback
1818

1919
app = Flask(__name__)
2020
mail = Mail(app)
@@ -37,38 +37,6 @@
3737
mail.init_app(app)
3838

3939

40-
def send_feedback():
41-
feedback = request.form.get("feedback")
42-
file = request.files.get("transcript")
43-
44-
if not file:
45-
return "No file provided", 400
46-
47-
filename = secure_filename(file.filename)
48-
filepath = os.path.join("/tmp", filename)
49-
file.save(filepath)
50-
51-
with open(filepath, "r", encoding="utf-8") as f:
52-
html_content = f.read()
53-
54-
msg = Message(
55-
subject="Feedback with Transcript",
56-
sender=os.getenv("MAIL_USERNAME"),
57-
recipients=["[email protected]"],
58-
body=f"User feedback:\n\n{feedback}",
59-
)
60-
msg.attach(
61-
filename="transcript.html",
62-
content_type="text/html",
63-
data=html_content.encode("utf-8"),
64-
)
65-
66-
mail.send(msg)
67-
os.remove(filepath)
68-
69-
return "Email sent", 200
70-
71-
7240
tenant_session = TenantSession()
7341

7442

@@ -99,7 +67,10 @@ def clear_session():
9967
)
10068

10169
app.add_url_rule(
102-
"/api/feedback", endpoint="feedback", view_func=send_feedback, methods=["POST"]
70+
"/api/feedback",
71+
endpoint="feedback",
72+
view_func=lambda: send_feedback(mail),
73+
methods=["POST"],
10374
)
10475

10576
if __name__ == "__main__":

backend/tenantfirstaid/feedback.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
from flask import request
2+
from flask_mail import Message
3+
from werkzeug.utils import secure_filename
4+
import os
5+
6+
7+
def send_feedback(mail):
8+
feedback = request.form.get("feedback")
9+
file = request.files.get("transcript")
10+
11+
if not file:
12+
return "No file provided", 400
13+
14+
filename = secure_filename(file.filename)
15+
filepath = os.path.join("/tmp", filename)
16+
file.save(filepath)
17+
18+
with open(filepath, "r", encoding="utf-8") as f:
19+
html_content = f.read()
20+
21+
msg = Message(
22+
subject="Feedback with Transcript",
23+
sender=os.getenv("MAIL_USERNAME"),
24+
recipients=["[email protected]"],
25+
body=f"User feedback:\n\n{feedback}",
26+
)
27+
msg.attach(
28+
filename="transcript.html",
29+
content_type="text/html",
30+
data=html_content.encode("utf-8"),
31+
)
32+
33+
mail.send(msg)
34+
os.remove(filepath)
35+
36+
return "Email sent", 200

0 commit comments

Comments
 (0)