From 618d8837d476a9fb92e045dbaed92d93ef59d51a Mon Sep 17 00:00:00 2001 From: Andrew Liu Date: Tue, 16 Apr 2024 02:43:25 +0000 Subject: [PATCH] email show up without errors --- server/controllers/emails.py | 42 ++++++++++++++++++++---------------- server/models/response.py | 17 +++++++++++++-- server_tests/test_admin.py | 0 3 files changed, 38 insertions(+), 21 deletions(-) create mode 100644 server_tests/test_admin.py diff --git a/server/controllers/emails.py b/server/controllers/emails.py index 3d23f028a..5297c68f8 100644 --- a/server/controllers/emails.py +++ b/server/controllers/emails.py @@ -442,6 +442,7 @@ def get_response(): response = db.session.execute( select(Response).where(Response.email_id == data["id"]) ).scalar() + if not response: return {"message": "Response not found"}, 400 return response.map() @@ -524,23 +525,26 @@ def get_threads(): Get a list of all threads. """ - thread_list = db.session.execute(select(Thread)).all() - print("thread list", thread_list) - # thread_list = db.session.execute( - # select(Thread).order_by(Thread.resolved, Thread.last_email.desc()) - # ).all() - # email_list = [ - # { - # "id": thread.id, - # "resolved": thread.resolved, - # "emailList": [ - # thread_email.map() - # for thread_email in db.session.execute( - # select(Email).where(Email.thread_id == thread.id) - # ).all() - # ], - # } - # for thread in thread_list - # ] - email_list = [] + thread_list = ( + db.session.execute( + select(Thread).order_by(Thread.resolved, Thread.last_email.desc()) + ) + .scalars() + .all() + ) + email_list = [ + { + "id": thread.id, + "resolved": thread.resolved, + "emailList": [ + thread_email.map() + for thread_email in db.session.execute( + select(Email).where(Email.thread_id == thread.id) + ) + .scalars() + .all() + ], + } + for thread in thread_list + ] return email_list diff --git a/server/models/response.py b/server/models/response.py index 71b3d8acd..b0e8afe35 100644 --- a/server/models/response.py +++ b/server/models/response.py @@ -54,12 +54,25 @@ class Response(db.Model): ) def map(self): - """Map the response to a dictionary.""" + """Map the response to a dictionary. + + Groups documents and document_confidences into a list of lists clustered by + question. + """ + doc_confs = [] + docs = [] + cur_idx = 0 + for num_docs in self.docs_per_question: + doc_confs.append(self.document_confidences[cur_idx : cur_idx + num_docs]) + docs.append(self.documents[cur_idx : cur_idx + num_docs]) + cur_idx += num_docs + docs = [[doc.map() for doc in doc_list] for doc_list in docs] return { "id": self.id, "content": self.response, "questions": self.questions, - "document_confidences": self.document_confidences, + "documents": docs, + "document_confidences": doc_confs, "confidence": self.confidence, "emailId": self.email_id, } diff --git a/server_tests/test_admin.py b/server_tests/test_admin.py new file mode 100644 index 000000000..e69de29bb