Skip to content

Commit

Permalink
Improve routing and error handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
liffiton committed Jul 18, 2024
1 parent 90fd6bd commit 2eeb856
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 14 deletions.
18 changes: 15 additions & 3 deletions src/codehelp/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -28,7 +36,7 @@


@bp.route("/")
@bp.route("/<int:query_id>")
@bp.route("/retry/<int:query_id>")
@login_required
@class_enabled_required
def help_form(query_id: int | None = None) -> str:
Expand Down Expand Up @@ -58,8 +66,12 @@ def help_form(query_id: int | None = None) -> str:

@bp.route("/view/<int:query_id>")
@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'])
Expand Down
8 changes: 4 additions & 4 deletions src/codehelp/templates/help_view.html
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ <h1><span class="title is-size-4">Response</span> <span class="subtitle ml-5 is-
</div>
<div class="message-body">
{{ responses['insufficient'] | markdown }}
<p style="border-top: solid 1px #c90; padding-top: 0.75rem;">An <i>attempt</i> at a response is below, but you can <a href="/help/{{query.id}}" class="button is-link is-outlined is-rounded p-2 ml-1 mr-1" style="vertical-align: baseline; height: 2rem;">Retry</a> this query and provide additional details or clarification to receive a more helpful response.
<p style="border-top: solid 1px #c90; padding-top: 0.75rem;">An <i>attempt</i> at a response is below, but you can <a href="{{ url_for('.help_form', query_id=query.id) }}" class="button is-link is-outlined is-rounded p-2 ml-1 mr-1" style="vertical-align: baseline; height: 2rem;">Retry</a> this query and provide additional details or clarification to receive a more helpful response.
</p>
</div>
</div>
Expand Down Expand Up @@ -154,7 +154,7 @@ <h2 class="is-size-5">Related Topics</h2>
x-data="{topics_fragment: '<span class=\'loader m-4\' style=\'font-size: 200%\'></span>'}"
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 })
"
Expand All @@ -167,9 +167,9 @@ <h2 class="is-size-5">Related Topics</h2>
{% endif %}

<div class="container mt-6">
<a href="/help/" class="button is-link is-outlined is-rounded p-2 ml-1 mr-1" style="vertical-align: baseline; height: 2rem;">Ask another question</a>
<a href="{{ url_for('.help_form') }}" class="button is-link is-outlined is-rounded p-2 ml-1 mr-1" style="vertical-align: baseline; height: 2rem;">Ask another question</a>
or
<a href="/help/{{query.id}}" class="button is-link is-outlined is-rounded p-2 ml-1 mr-1" style="vertical-align: baseline; height: 2rem;">Retry this query</a>
<a href="{{ url_for('.help_form', query_id=query.id) }}" class="button is-link is-outlined is-rounded p-2 ml-1 mr-1" style="vertical-align: baseline; height: 2rem;">Retry this query</a>
</div>

</section>
Expand Down
14 changes: 11 additions & 3 deletions src/codehelp/tutor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -78,12 +86,12 @@ def start_chat_from_query(llm_dict: LLMDict) -> Response:


@bp.route("/chat/<int:chat_id>")
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()

Expand Down
4 changes: 2 additions & 2 deletions tests/test_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
])
Expand Down
2 changes: 1 addition & 1 deletion tests/test_lti.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion tests/test_user_classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 2eeb856

Please sign in to comment.