Skip to content

Commit

Permalink
Get rid of chat in report detail view
Browse files Browse the repository at this point in the history
  • Loading branch information
medihack committed Sep 1, 2024
1 parent ff5ed4d commit 7a6f3c9
Show file tree
Hide file tree
Showing 7 changed files with 11 additions and 117 deletions.
4 changes: 4 additions & 0 deletions radis/chats/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,17 @@ def ready(self):
def register_app():
from adit_radis_shared.common.site import MainMenuItem, register_main_menu_item

from radis.reports.site import register_report_panel_button

register_main_menu_item(
MainMenuItem(
url_name="chat_list",
label="Chats",
)
)

register_report_panel_button(3, "chats/_create_chat_button.html")


def init_db(**kwargs):
create_app_settings()
Expand Down
6 changes: 6 additions & 0 deletions radis/chats/templates/chats/_create_chat_button.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{% load bootstrap_icon from common_extras %}
<a href="{% url 'chat_create' %}?report_id={{ report.id }}"
class="btn btn-secondary btn-sm">
{% bootstrap_icon "chat" %}
Chat
</a>
27 changes: 0 additions & 27 deletions radis/reports/forms.py

This file was deleted.

37 changes: 0 additions & 37 deletions radis/reports/templates/reports/_report_chat.html

This file was deleted.

1 change: 0 additions & 1 deletion radis/reports/templates/reports/report_detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,4 @@ <h5 class="card-title">Report Text</h5>
<div class="pre-line">{{ report.body }}</div>
</div>
</div>
{% include "reports/_report_chat.html" %}
{% endblock content %}
3 changes: 1 addition & 2 deletions radis/reports/urls.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
from django.urls import path
from rest_framework.urlpatterns import format_suffix_patterns

from .views import ReportBodyView, ReportDetailView, report_chat_view
from .views import ReportBodyView, ReportDetailView

urlpatterns = [
path("<int:pk>/body/", ReportBodyView.as_view(), name="report_body"),
path("<int:pk>/chat/", report_chat_view, name="report_chat"),
path("<int:pk>/", ReportDetailView.as_view(), name="report_detail"),
]

Expand Down
50 changes: 0 additions & 50 deletions radis/reports/views.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,8 @@
from typing import Any

from adit_radis_shared.common.decorators import login_required_async, user_passes_test_async
from adit_radis_shared.common.types import AuthenticatedHttpRequest
from django.contrib.auth.mixins import LoginRequiredMixin, UserPassesTestMixin
from django.db.models import QuerySet
from django.http import HttpResponse
from django.shortcuts import aget_object_or_404, render
from django.views.generic.detail import DetailView

from radis.chats.utils.chat_client import AsyncChatClient
from radis.reports.forms import PromptForm

from .models import Report


Expand All @@ -29,48 +21,6 @@ def get_queryset(self) -> QuerySet[Report]:
assert active_group
return super().get_queryset().filter(groups=active_group)

def get_context_data(self, **kwargs: Any) -> dict[str, Any]:
context = super().get_context_data(**kwargs)
context["prompt_form"] = PromptForm()
context["messages"] = []
return context


class ReportBodyView(ReportDetailView):
template_name = "reports/report_body.html"


@login_required_async
@user_passes_test_async(lambda user: user.active_group is not None)
async def report_chat_view(request: AuthenticatedHttpRequest, pk: int) -> HttpResponse:
report = await aget_object_or_404(
Report.objects.filter(groups=request.user.active_group), pk=pk
)

form = PromptForm(request.POST)

context: dict[str, Any] = {
"messages": [],
"report": report,
"prompt_form": form,
}

if form.is_valid():
chat_client = AsyncChatClient()
if request.POST.get("yes_no_answer"):
answer = await chat_client.ask_report_yes_no_question(
report.body, form.cleaned_data["prompt"]
)
answer = "Yes" if answer == "yes" else "No"
elif request.POST.get("full_answer"):
answer = await chat_client.ask_report_question(report.body, form.cleaned_data["prompt"])
else:
raise ValueError("Invalid form")

context["messages"] = [
{"role": "User", "content": form.cleaned_data["prompt"]},
{"role": "Assistant", "content": answer},
]
context["prompt_form"] = PromptForm()

return render(request, "reports/_report_chat.html", context)

0 comments on commit 7a6f3c9

Please sign in to comment.