From 2eeb856ee463b15a4692e4bd9aa6e92469f99cd7 Mon Sep 17 00:00:00 2001 From: Mark Liffiton Date: Thu, 18 Jul 2024 12:53:35 -0500 Subject: [PATCH] Improve routing and error handling. --- src/codehelp/helper.py | 18 +++++++++++++++--- src/codehelp/templates/help_view.html | 8 ++++---- src/codehelp/tutor.py | 14 +++++++++++--- tests/test_auth.py | 4 ++-- tests/test_lti.py | 2 +- tests/test_user_classes.py | 2 +- 6 files changed, 34 insertions(+), 14 deletions(-) diff --git a/src/codehelp/helper.py b/src/codehelp/helper.py index 504ef56..f5bf83b 100644 --- a/src/codehelp/helper.py +++ b/src/codehelp/helper.py @@ -7,7 +7,15 @@ from collections.abc import Iterable from unittest.mock import patch -from flask import Blueprint, abort, redirect, render_template, request, url_for +from flask import ( + Blueprint, + abort, + make_response, + redirect, + render_template, + request, + url_for, +) from gened.auth import ( admin_required, class_enabled_required, @@ -28,7 +36,7 @@ @bp.route("/") -@bp.route("/") +@bp.route("/retry/") @login_required @class_enabled_required def help_form(query_id: int | None = None) -> str: @@ -58,8 +66,12 @@ def help_form(query_id: int | None = None) -> str: @bp.route("/view/") @login_required -def help_view(query_id: int) -> str: +def help_view(query_id: int) -> str | Response: query_row, responses = get_query(query_id) + + if query_row is None: + return make_response(render_template("error.html"), 400) + history = get_history() if query_row and query_row['topics_json']: topics = json.loads(query_row['topics_json']) diff --git a/src/codehelp/templates/help_view.html b/src/codehelp/templates/help_view.html index ee6e8ef..d3442dd 100644 --- a/src/codehelp/templates/help_view.html +++ b/src/codehelp/templates/help_view.html @@ -94,7 +94,7 @@

Response {{ responses['insufficient'] | markdown }} -

An attempt at a response is below, but you can Retry this query and provide additional details or clarification to receive a more helpful response. +

An attempt at a response is below, but you can Retry this query and provide additional details or clarification to receive a more helpful response.

@@ -154,7 +154,7 @@

Related Topics

x-data="{topics_fragment: ''}" x-html="topics_fragment" x-init=" - fetch('/help/topics/html/{{query.id}}') + fetch('{{ url_for('.get_topics_html', query_id=query.id) }}') .then(response => response.text()) .then(text => { topics_fragment = text }) " @@ -167,9 +167,9 @@

Related Topics

{% endif %} diff --git a/src/codehelp/tutor.py b/src/codehelp/tutor.py index 0acbe6d..f3f1d14 100644 --- a/src/codehelp/tutor.py +++ b/src/codehelp/tutor.py @@ -6,7 +6,15 @@ import json from sqlite3 import Row -from flask import Blueprint, flash, redirect, render_template, request, url_for +from flask import ( + Blueprint, + flash, + make_response, + redirect, + render_template, + request, + url_for, +) from gened.admin import bp as bp_admin from gened.admin import register_admin_link from gened.auth import get_auth, login_required, tester_required @@ -78,12 +86,12 @@ def start_chat_from_query(llm_dict: LLMDict) -> Response: @bp.route("/chat/") -def chat_interface(chat_id: int) -> str: +def chat_interface(chat_id: int) -> str | Response: try: chat, topic, context = get_chat(chat_id) except (ChatNotFoundError, AccessDeniedError): flash("Invalid id.", "warning") - return render_template("error.html") + return make_response(render_template("error.html"), 400) chat_history = get_chat_history() diff --git a/tests/test_auth.py b/tests/test_auth.py index 0207b4c..2501319 100644 --- a/tests/test_auth.py +++ b/tests/test_auth.py @@ -134,10 +134,10 @@ def test_logout(client, auth): ('/', 200, 200, 200), ('/profile/', 302, (200, "0 total, 0 in the past week"), (200, "0 total, 0 in the past week")), ('/help/', 302, 200, 200), - ('/help/view/1', 302, (200, "Invalid id."), (200, "response1")), + ('/help/view/1', 302, (400, "Invalid id."), (200, "response1")), ('/tutor/', 404, 200, 200), ('/tutor/chat/1', 404, (200, "user_msg_1"), (200, "user_msg_1")), - ('/tutor/chat/2', 404, (200, "Invalid id."), (200, "user_msg_2")), + ('/tutor/chat/2', 404, (400, "Invalid id."), (200, "user_msg_2")), ('/admin/', 302, 302, 200), # admin_required redirects to login ('/admin/get_db', 302, 302, 200), # admin_required redirects to login ]) diff --git a/tests/test_lti.py b/tests/test_lti.py index acfbd8c..50145c9 100644 --- a/tests/test_lti.py +++ b/tests/test_lti.py @@ -203,7 +203,7 @@ def test_lti_instructor_and_students(client): # 6) student 2 cannot see student 1's query result = client.get('/help/view/5') - assert result.status_code == 200 + assert result.status_code == 400 assert 'student_1_code' not in result.text assert 'Invalid id.' in result.text diff --git a/tests/test_user_classes.py b/tests/test_user_classes.py index 80387e6..35ff83d 100644 --- a/tests/test_user_classes.py +++ b/tests/test_user_classes.py @@ -100,7 +100,7 @@ def test_user_class_usage(app): # 5) instructor cannot yet see a query result = instructor_client.get('/help/view/5') - assert result.status_code == 200 + assert result.status_code == 400 assert 'Invalid id.' in result.text # 6) user makes a query