Skip to content

Commit

Permalink
Bugfix/remove source files (#1281)
Browse files Browse the repository at this point in the history
* removed source files

* removed files added in error

* removed migration

* formatting
  • Loading branch information
gecBurton authored Jan 9, 2025
1 parent 040eeb2 commit cf9f784
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 42 deletions.
4 changes: 3 additions & 1 deletion django_app/redbox_app/redbox_core/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,9 @@ class ChatMessageAdmin(ExportMixin, admin.ModelAdmin):
list_filter = ["role", "route", "chat__user"]
date_hierarchy = "created_at"
inlines = [ChatMessageTokenUseInline]
readonly_fields = ["selected_files", "source_files"]
readonly_fields = [
"selected_files",
]
search_fields = ["chat__user__email"]

@admin.display(ordering="chat__user", description="User")
Expand Down
13 changes: 2 additions & 11 deletions django_app/redbox_app/redbox_core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from django.contrib.postgres.fields import ArrayField
from django.core import validators
from django.db import models
from django.db.models import Max, Min, Prefetch, UniqueConstraint
from django.db.models import Max, Min, UniqueConstraint
from django.utils import timezone
from django.utils.translation import gettext_lazy as _
from django_use_email_as_username.models import BaseUser, BaseUserManager
Expand Down Expand Up @@ -766,16 +766,7 @@ def save(self, *args, force_insert=False, force_update=False, using=None, update
@classmethod
def get_messages(cls, chat_id: uuid.UUID) -> Sequence["ChatMessage"]:
"""Returns all chat messages for a given chat history, ordered by citation priority."""
return (
cls.objects.filter(chat_id=chat_id)
.order_by("created_at")
.prefetch_related(
Prefetch(
"source_files",
queryset=File.objects.all().order_by("created_at"),
)
)
)
return cls.objects.filter(chat_id=chat_id).order_by("created_at")

def log(self):
token_sum = sum(token_use.token_count for token_use in self.chatmessagetokenuse_set.all())
Expand Down
2 changes: 0 additions & 2 deletions django_app/redbox_app/redbox_core/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ class Meta:

class ChatMessageSerializer(serializers.ModelSerializer):
selected_files = FileSerializer(many=True, read_only=True)
source_files = FileSerializer(many=True, read_only=True)
token_use = ChatMessageTokenUseSerializer(source="chatmessagetokenuse_set", many=True, read_only=True)

class Meta:
Expand All @@ -32,7 +31,6 @@ class Meta:
"role",
"route",
"selected_files",
"source_files",
"rating",
"rating_text",
"rating_chips",
Expand Down
14 changes: 4 additions & 10 deletions django_app/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,12 +161,8 @@ def chat_with_message(chat: Chat) -> Chat:


@pytest.fixture()
def chat_message(chat: Chat, uploaded_file: File) -> ChatMessage:
chat_message = ChatMessage.objects.create(
chat=chat, text="A question?", role=ChatMessage.Role.user, route="A route"
)
chat_message.source_files.set([uploaded_file])
return chat_message
def chat_message(chat: Chat) -> ChatMessage:
return ChatMessage.objects.create(chat=chat, text="A question?", role=ChatMessage.Role.user, route="A route")


@pytest.fixture()
Expand Down Expand Up @@ -219,26 +215,24 @@ def chat_with_files(chat: Chat, several_files: Sequence[File]) -> Chat:
text="A question?",
role=ChatMessage.Role.user,
)
chat_message_1 = ChatMessage.objects.create(
ChatMessage.objects.create(
chat=chat,
text="An answer.",
role=ChatMessage.Role.ai,
route="search",
)
chat_message_1.source_files.set(several_files[0::2])
chat_message_2 = ChatMessage.objects.create(
chat=chat,
text="A second question?",
role=ChatMessage.Role.user,
)
chat_message_2.selected_files.set(several_files[0:2])
chat_message_3 = ChatMessage.objects.create(
ChatMessage.objects.create(
chat=chat,
text="A second answer.",
role=ChatMessage.Role.ai,
route="search",
)
chat_message_3.source_files.set([several_files[2]])
return chat


Expand Down
19 changes: 1 addition & 18 deletions django_app/tests/views/test_chat_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@
from django.test import Client
from django.urls import reverse

from redbox_app.redbox_core.models import (
Chat,
ChatMessage,
)
from redbox_app.redbox_core.models import Chat

User = get_user_model()

Expand Down Expand Up @@ -44,20 +41,6 @@ def test_user_cannot_see_other_users_chats(chat: Chat, bob: User, client: Client
assert response.headers.get("Location") == "/chats/"


@pytest.mark.django_db()
def test_view_session_with_documents(chat_message: ChatMessage, client: Client):
# Given
client.force_login(chat_message.chat.user)
chat_id = chat_message.chat.id

# When
response = client.get(f"/chats/{chat_id}/")

# Then
assert response.status_code == HTTPStatus.OK
assert b"original_file.txt" in response.content


@pytest.mark.django_db()
def test_chat_grouped_by_age(user_with_chats_with_messages_over_time: User, client: Client, chat):
# Given
Expand Down

0 comments on commit cf9f784

Please sign in to comment.