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

159 lines
4.1 KiB
Python
Raw Normal View History

2024-01-08 08:43:32 +01:00
from fastapi import Depends, FastAPI, HTTPException, status
from datetime import datetime, timedelta
from typing import List, Union, Optional
from fastapi import APIRouter
from pydantic import BaseModel
import json
from apps.web.models.documents import (
Documents,
DocumentForm,
DocumentUpdateForm,
DocumentModel,
2024-02-03 23:44:49 +01:00
DocumentResponse,
2024-01-08 08:43:32 +01:00
)
from utils.utils import get_current_user, get_admin_user
2024-01-08 08:43:32 +01:00
from constants import ERROR_MESSAGES
router = APIRouter()
############################
# GetDocuments
############################
2024-02-03 23:44:49 +01:00
@router.get("/", response_model=List[DocumentResponse])
2024-01-08 08:43:32 +01:00
async def get_documents(user=Depends(get_current_user)):
2024-02-03 23:44:49 +01:00
docs = [
DocumentResponse(
**{
**doc.model_dump(),
"content": json.loads(doc.content if doc.content else "{}"),
}
)
for doc in Documents.get_docs()
]
return docs
2024-01-08 08:43:32 +01:00
############################
# CreateNewDoc
############################
2024-02-03 23:44:49 +01:00
@router.post("/create", response_model=Optional[DocumentResponse])
async def create_new_doc(form_data: DocumentForm, user=Depends(get_admin_user)):
2024-01-08 08:43:32 +01:00
doc = Documents.get_doc_by_name(form_data.name)
if doc == None:
doc = Documents.insert_new_doc(user.id, form_data)
if doc:
2024-02-03 23:44:49 +01:00
return DocumentResponse(
**{
**doc.model_dump(),
"content": json.loads(doc.content if doc.content else "{}"),
}
)
2024-01-08 08:43:32 +01:00
else:
raise HTTPException(
2024-01-08 10:54:03 +01:00
status_code=status.HTTP_400_BAD_REQUEST,
detail=ERROR_MESSAGES.FILE_EXISTS,
2024-01-08 08:43:32 +01:00
)
else:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
2024-01-08 10:54:03 +01:00
detail=ERROR_MESSAGES.NAME_TAG_TAKEN,
2024-01-08 08:43:32 +01:00
)
############################
# GetDocByName
############################
2024-02-03 23:44:49 +01:00
@router.get("/name/{name}", response_model=Optional[DocumentResponse])
2024-01-08 08:43:32 +01:00
async def get_doc_by_name(name: str, user=Depends(get_current_user)):
doc = Documents.get_doc_by_name(name)
if doc:
2024-02-03 23:44:49 +01: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 06:06:08 +01:00
class TagItem(BaseModel):
name: str
2024-02-03 23:44:49 +01:00
class TagDocumentForm(BaseModel):
name: str
tags: List[dict]
@router.post("/name/{name}/tags", response_model=Optional[DocumentResponse])
async def tag_doc_by_name(form_data: TagDocumentForm, user=Depends(get_current_user)):
doc = Documents.update_doc_content_by_name(form_data.name, {"tags": form_data.tags})
if doc:
return DocumentResponse(
**{
**doc.model_dump(),
"content": json.loads(doc.content if doc.content else "{}"),
}
)
2024-01-08 08:43:32 +01:00
else:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail=ERROR_MESSAGES.NOT_FOUND,
)
############################
# UpdateDocByName
############################
2024-02-03 23:44:49 +01:00
@router.post("/name/{name}/update", response_model=Optional[DocumentResponse])
2024-01-08 08:43:32 +01:00
async def update_doc_by_name(
name: str, form_data: DocumentUpdateForm, user=Depends(get_admin_user)
2024-01-08 08:43:32 +01:00
):
doc = Documents.update_doc_by_name(name, form_data)
if doc:
2024-02-03 23:44:49 +01:00
return DocumentResponse(
**{
**doc.model_dump(),
"content": json.loads(doc.content if doc.content else "{}"),
}
)
2024-01-08 08:43:32 +01:00
else:
raise HTTPException(
2024-01-08 10:49:20 +01:00
status_code=status.HTTP_400_BAD_REQUEST,
detail=ERROR_MESSAGES.NAME_TAG_TAKEN,
2024-01-08 08:43:32 +01:00
)
############################
# DeleteDocByName
############################
@router.delete("/name/{name}/delete", response_model=bool)
async def delete_doc_by_name(name: str, user=Depends(get_admin_user)):
2024-01-08 08:43:32 +01:00
result = Documents.delete_doc_by_name(name)
return result