documents.py 4.05 KB
Newer Older
Timothy J. Baek's avatar
Timothy J. Baek committed
1
2
from fastapi import Depends, FastAPI, HTTPException, status
from datetime import datetime, timedelta
Michael Poluektov's avatar
Michael Poluektov committed
3
from typing import Union, Optional
Timothy J. Baek's avatar
Timothy J. Baek committed
4
5
6
7
8

from fastapi import APIRouter
from pydantic import BaseModel
import json

9
from apps.webui.models.documents import (
Timothy J. Baek's avatar
Timothy J. Baek committed
10
11
12
13
    Documents,
    DocumentForm,
    DocumentUpdateForm,
    DocumentModel,
Timothy J. Baek's avatar
Timothy J. Baek committed
14
    DocumentResponse,
Timothy J. Baek's avatar
Timothy J. Baek committed
15
16
)

Timothy J. Baek's avatar
refac  
Timothy J. Baek committed
17
from utils.utils import get_verified_user, get_admin_user
Timothy J. Baek's avatar
Timothy J. Baek committed
18
19
20
21
22
23
24
25
26
from constants import ERROR_MESSAGES

router = APIRouter()

############################
# GetDocuments
############################


Michael Poluektov's avatar
Michael Poluektov committed
27
@router.get("/", response_model=list[DocumentResponse])
Timothy J. Baek's avatar
refac  
Timothy J. Baek committed
28
async def get_documents(user=Depends(get_verified_user)):
Timothy J. Baek's avatar
Timothy J. Baek committed
29
30
31
32
33
34
35
    docs = [
        DocumentResponse(
            **{
                **doc.model_dump(),
                "content": json.loads(doc.content if doc.content else "{}"),
            }
        )
36
        for doc in Documents.get_docs()
Timothy J. Baek's avatar
Timothy J. Baek committed
37
38
    ]
    return docs
Timothy J. Baek's avatar
Timothy J. Baek committed
39
40
41
42
43
44
45


############################
# CreateNewDoc
############################


Timothy J. Baek's avatar
Timothy J. Baek committed
46
@router.post("/create", response_model=Optional[DocumentResponse])
47
async def create_new_doc(form_data: DocumentForm, user=Depends(get_admin_user)):
48
    doc = Documents.get_doc_by_name(form_data.name)
49
    if doc is None:
50
        doc = Documents.insert_new_doc(user.id, form_data)
Timothy J. Baek's avatar
Timothy J. Baek committed
51
52

        if doc:
Timothy J. Baek's avatar
Timothy J. Baek committed
53
54
55
56
57
58
            return DocumentResponse(
                **{
                    **doc.model_dump(),
                    "content": json.loads(doc.content if doc.content else "{}"),
                }
            )
Timothy J. Baek's avatar
Timothy J. Baek committed
59
60
        else:
            raise HTTPException(
61
62
                status_code=status.HTTP_400_BAD_REQUEST,
                detail=ERROR_MESSAGES.FILE_EXISTS,
Timothy J. Baek's avatar
Timothy J. Baek committed
63
64
65
66
            )
    else:
        raise HTTPException(
            status_code=status.HTTP_400_BAD_REQUEST,
67
            detail=ERROR_MESSAGES.NAME_TAG_TAKEN,
Timothy J. Baek's avatar
Timothy J. Baek committed
68
69
70
71
72
73
74
75
        )


############################
# GetDocByName
############################


Timothy J. Baek's avatar
Timothy J. Baek committed
76
@router.get("/doc", response_model=Optional[DocumentResponse])
Timothy J. Baek's avatar
refac  
Timothy J. Baek committed
77
async def get_doc_by_name(name: str, user=Depends(get_verified_user)):
78
    doc = Documents.get_doc_by_name(name)
Timothy J. Baek's avatar
Timothy J. Baek committed
79
80

    if doc:
Timothy J. Baek's avatar
Timothy J. Baek committed
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
        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
############################


99
100
101
102
class TagItem(BaseModel):
    name: str


Timothy J. Baek's avatar
Timothy J. Baek committed
103
104
class TagDocumentForm(BaseModel):
    name: str
Michael Poluektov's avatar
Michael Poluektov committed
105
    tags: list[dict]
Timothy J. Baek's avatar
Timothy J. Baek committed
106
107


Timothy J. Baek's avatar
Timothy J. Baek committed
108
@router.post("/doc/tags", response_model=Optional[DocumentResponse])
Timothy J. Baek's avatar
refac  
Timothy J. Baek committed
109
async def tag_doc_by_name(form_data: TagDocumentForm, user=Depends(get_verified_user)):
110
    doc = Documents.update_doc_content_by_name(form_data.name, {"tags": form_data.tags})
Timothy J. Baek's avatar
Timothy J. Baek committed
111
112
113
114
115
116
117
118

    if doc:
        return DocumentResponse(
            **{
                **doc.model_dump(),
                "content": json.loads(doc.content if doc.content else "{}"),
            }
        )
Timothy J. Baek's avatar
Timothy J. Baek committed
119
120
121
122
123
124
125
126
127
128
129
130
    else:
        raise HTTPException(
            status_code=status.HTTP_401_UNAUTHORIZED,
            detail=ERROR_MESSAGES.NOT_FOUND,
        )


############################
# UpdateDocByName
############################


Timothy J. Baek's avatar
Timothy J. Baek committed
131
@router.post("/doc/update", response_model=Optional[DocumentResponse])
Timothy J. Baek's avatar
Timothy J. Baek committed
132
async def update_doc_by_name(
133
134
135
    name: str,
    form_data: DocumentUpdateForm,
    user=Depends(get_admin_user),
Timothy J. Baek's avatar
Timothy J. Baek committed
136
):
137
    doc = Documents.update_doc_by_name(name, form_data)
Timothy J. Baek's avatar
Timothy J. Baek committed
138
    if doc:
Timothy J. Baek's avatar
Timothy J. Baek committed
139
140
141
142
143
144
        return DocumentResponse(
            **{
                **doc.model_dump(),
                "content": json.loads(doc.content if doc.content else "{}"),
            }
        )
Timothy J. Baek's avatar
Timothy J. Baek committed
145
146
    else:
        raise HTTPException(
147
148
            status_code=status.HTTP_400_BAD_REQUEST,
            detail=ERROR_MESSAGES.NAME_TAG_TAKEN,
Timothy J. Baek's avatar
Timothy J. Baek committed
149
150
151
152
153
154
155
156
        )


############################
# DeleteDocByName
############################


Timothy J. Baek's avatar
Timothy J. Baek committed
157
@router.delete("/doc/delete", response_model=bool)
158
async def delete_doc_by_name(name: str, user=Depends(get_admin_user)):
159
    result = Documents.delete_doc_by_name(name)
Timothy J. Baek's avatar
Timothy J. Baek committed
160
    return result