open-webui/backend/apps/webui/routers/documents.py

156 lines
4.0 KiB
Python
Raw Normal View History

2024-01-08 07:43:32 +00:00
import json
2024-08-27 22:10:27 +00:00
from typing import Optional
2024-01-08 07:43:32 +00:00
2024-05-26 08:15:48 +00:00
from apps.webui.models.documents import (
2024-01-08 07:43:32 +00:00
DocumentForm,
2024-02-03 22:44:49 +00:00
DocumentResponse,
2024-08-27 22:10:27 +00:00
Documents,
DocumentUpdateForm,
2024-01-08 07:43:32 +00:00
)
from constants import ERROR_MESSAGES
2024-08-27 22:10:27 +00:00
from fastapi import APIRouter, Depends, HTTPException, status
from pydantic import BaseModel
from utils.utils import get_admin_user, get_verified_user
2024-01-08 07:43:32 +00:00
router = APIRouter()
############################
# GetDocuments
############################
2024-08-14 12:46:31 +00:00
@router.get("/", response_model=list[DocumentResponse])
2024-06-27 18:29:59 +00:00
async def get_documents(user=Depends(get_verified_user)):
2024-02-03 22:44:49 +00:00
docs = [
DocumentResponse(
**{
**doc.model_dump(),
"content": json.loads(doc.content if doc.content else "{}"),
}
)
for doc in Documents.get_docs()
2024-02-03 22:44:49 +00:00
]
return docs
2024-01-08 07:43:32 +00:00
############################
# CreateNewDoc
############################
2024-02-03 22:44:49 +00:00
@router.post("/create", response_model=Optional[DocumentResponse])
2024-06-24 07:57:08 +00:00
async def create_new_doc(form_data: DocumentForm, user=Depends(get_admin_user)):
doc = Documents.get_doc_by_name(form_data.name)
2024-08-14 12:39:53 +00:00
if doc is None:
doc = Documents.insert_new_doc(user.id, form_data)
2024-01-08 07:43:32 +00:00
if doc:
2024-02-03 22:44:49 +00:00
return DocumentResponse(
**{
**doc.model_dump(),
"content": json.loads(doc.content if doc.content else "{}"),
}
)
2024-01-08 07:43:32 +00:00
else:
raise HTTPException(
2024-01-08 09:54:03 +00:00
status_code=status.HTTP_400_BAD_REQUEST,
detail=ERROR_MESSAGES.FILE_EXISTS,
2024-01-08 07:43:32 +00:00
)
else:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
2024-01-08 09:54:03 +00:00
detail=ERROR_MESSAGES.NAME_TAG_TAKEN,
2024-01-08 07:43:32 +00:00
)
############################
# GetDocByName
############################
2024-06-12 20:45:13 +00:00
@router.get("/doc", response_model=Optional[DocumentResponse])
2024-06-27 18:29:59 +00:00
async def get_doc_by_name(name: str, user=Depends(get_verified_user)):
doc = Documents.get_doc_by_name(name)
2024-01-08 07:43:32 +00:00
if doc:
2024-02-03 22:44:49 +00:00
return DocumentResponse(
**{
**doc.model_dump(),
"content": json.loads(doc.content if doc.content else "{}"),
}
)
else:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail=ERROR_MESSAGES.NOT_FOUND,
)
############################
# TagDocByName
############################
2024-02-18 05:06:08 +00:00
class TagItem(BaseModel):
name: str
2024-02-03 22:44:49 +00:00
class TagDocumentForm(BaseModel):
name: str
2024-08-14 12:46:31 +00:00
tags: list[dict]
2024-02-03 22:44:49 +00:00
2024-06-12 20:45:13 +00:00
@router.post("/doc/tags", response_model=Optional[DocumentResponse])
2024-06-27 18:29:59 +00:00
async def tag_doc_by_name(form_data: TagDocumentForm, user=Depends(get_verified_user)):
2024-06-24 07:57:08 +00:00
doc = Documents.update_doc_content_by_name(form_data.name, {"tags": form_data.tags})
2024-02-03 22:44:49 +00:00
if doc:
return DocumentResponse(
**{
**doc.model_dump(),
"content": json.loads(doc.content if doc.content else "{}"),
}
)
2024-01-08 07:43:32 +00:00
else:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail=ERROR_MESSAGES.NOT_FOUND,
)
############################
# UpdateDocByName
############################
2024-06-12 20:45:13 +00:00
@router.post("/doc/update", response_model=Optional[DocumentResponse])
2024-01-08 07:43:32 +00:00
async def update_doc_by_name(
name: str,
form_data: DocumentUpdateForm,
user=Depends(get_admin_user),
2024-01-08 07:43:32 +00:00
):
doc = Documents.update_doc_by_name(name, form_data)
2024-01-08 07:43:32 +00:00
if doc:
2024-02-03 22:44:49 +00:00
return DocumentResponse(
**{
**doc.model_dump(),
"content": json.loads(doc.content if doc.content else "{}"),
}
)
2024-01-08 07:43:32 +00:00
else:
raise HTTPException(
2024-01-08 09:49:20 +00:00
status_code=status.HTTP_400_BAD_REQUEST,
detail=ERROR_MESSAGES.NAME_TAG_TAKEN,
2024-01-08 07:43:32 +00:00
)
############################
# DeleteDocByName
############################
2024-06-12 20:45:13 +00:00
@router.delete("/doc/delete", response_model=bool)
2024-06-24 07:57:08 +00:00
async def delete_doc_by_name(name: str, user=Depends(get_admin_user)):
result = Documents.delete_doc_by_name(name)
2024-01-08 07:43:32 +00:00
return result