Skip to content

Commit

Permalink
Adds formatter for ids
Browse files Browse the repository at this point in the history
  • Loading branch information
amandasavluchinske committed Jun 21, 2024
1 parent 699c703 commit c18027f
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 54 deletions.
12 changes: 9 additions & 3 deletions django_ai_assistant/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
)
from django_ai_assistant.conf import app_settings
from django_ai_assistant.exceptions import AIAssistantNotDefinedError, AIUserNotAllowedError
from django_ai_assistant.helpers import use_cases
from django_ai_assistant.helpers import formatters, use_cases
from django_ai_assistant.models import Message, Thread


Expand Down Expand Up @@ -86,6 +86,7 @@ def create_thread(request, payload: ThreadSchemaIn):

@api.get("threads/{thread_id}/", response=ThreadSchema, url_name="thread_detail_update_delete")
def get_thread(request, thread_id: Any):
thread_id = formatters.format_id(id, Thread)
try:
thread = use_cases.get_single_thread(
thread_id=thread_id, user=request.user, request=request
Expand All @@ -97,13 +98,15 @@ def get_thread(request, thread_id: Any):

@api.patch("threads/{thread_id}/", response=ThreadSchema, url_name="thread_detail_update_delete")
def update_thread(request, thread_id: Any, payload: ThreadSchemaIn):
thread_id = formatters.format_id(thread_id, Thread)
thread = get_object_or_404(Thread, id=thread_id)
name = payload.name
return use_cases.update_thread(thread=thread, name=name, user=request.user, request=request)


@api.delete("threads/{thread_id}/", response={204: None}, url_name="thread_detail_update_delete")
def delete_thread(request, thread_id: Any):
thread_id = formatters.format_id(thread_id, Thread)
thread = get_object_or_404(Thread, id=thread_id)
use_cases.delete_thread(thread=thread, user=request.user, request=request)
return 204, None
Expand All @@ -114,8 +117,8 @@ def delete_thread(request, thread_id: Any):
response=List[ThreadMessagesSchemaOut],
url_name="messages_list_create",
)
def list_thread_messages(request, thread_id: str):
thread = get_object_or_404(Thread, id=thread_id)
def list_thread_messages(request, thread_id: Any):
thread = get_object_or_404(Thread, id=formatters.format_id(thread_id, Thread))
messages = use_cases.get_thread_messages(thread=thread, user=request.user, request=request)
return [message_to_dict(m)["data"] for m in messages]

Expand All @@ -127,6 +130,7 @@ def list_thread_messages(request, thread_id: str):
url_name="messages_list_create",
)
def create_thread_message(request, thread_id: Any, payload: ThreadMessagesSchemaIn):
thread_id = formatters.format_id(thread_id, Thread)
thread = Thread.objects.get(id=thread_id)

use_cases.create_message(
Expand All @@ -143,6 +147,8 @@ def create_thread_message(request, thread_id: Any, payload: ThreadMessagesSchema
"threads/{thread_id}/messages/{message_id}/", response={204: None}, url_name="messages_delete"
)
def delete_thread_message(request, thread_id: Any, message_id: Any):
thread_id = formatters.format_id(thread_id, Message)
message_id = formatters.format_id(message_id, Message)
message = get_object_or_404(Message, id=message_id, thread_id=thread_id)
use_cases.delete_message(
message=message,
Expand Down
7 changes: 7 additions & 0 deletions django_ai_assistant/helpers/formatters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import uuid


def format_id(item_id, model):
if isinstance(id, str) and "UUID" in model._meta.pk.get_internal_type():
return uuid.UUID(item_id)
return item_id
12 changes: 9 additions & 3 deletions frontend/openapi_schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -355,9 +355,15 @@
"ThreadSchema": {
"properties": {
"id": {
"format": "uuid",
"title": "Id",
"type": "string"
"anyOf": [
{
"type": "integer"
},
{
"type": "null"
}
],
"title": "ID"
},
"name": {
"anyOf": [
Expand Down
12 changes: 9 additions & 3 deletions frontend/src/client/schemas.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,15 @@ export const $AssistantSchema = {
export const $ThreadSchema = {
properties: {
id: {
format: 'uuid',
title: 'Id',
type: 'string'
anyOf: [
{
type: 'integer'
},
{
type: 'null'
}
],
title: 'ID'
},
name: {
anyOf: [
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/client/types.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export type AssistantSchema = {
};

export type ThreadSchema = {
id?: string;
id?: number | null;
name?: string | null;
created_at: string;
updated_at: string;
Expand Down
44 changes: 0 additions & 44 deletions tests/test_models.py

This file was deleted.

0 comments on commit c18027f

Please sign in to comment.