documents.py 4.27 KB
Newer Older
Timothy J. Baek's avatar
Timothy J. Baek committed
1
2
3
4
5
6
7
8
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

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

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

router = APIRouter()

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


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


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


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

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


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


Timothy J. Baek's avatar
Timothy J. Baek committed
79
@router.get("/doc", response_model=Optional[DocumentResponse])
80
81
82
83
async def get_doc_by_name(
    name: str, user=Depends(get_current_user), db=Depends(get_db)
):
    doc = Documents.get_doc_by_name(db, name)
Timothy J. Baek's avatar
Timothy J. Baek committed
84
85

    if doc:
Timothy J. Baek's avatar
Timothy J. Baek committed
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
        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
############################


104
105
106
107
class TagItem(BaseModel):
    name: str


Timothy J. Baek's avatar
Timothy J. Baek committed
108
109
110
111
112
class TagDocumentForm(BaseModel):
    name: str
    tags: List[dict]


Timothy J. Baek's avatar
Timothy J. Baek committed
113
@router.post("/doc/tags", response_model=Optional[DocumentResponse])
114
115
116
117
118
119
async def tag_doc_by_name(
    form_data: TagDocumentForm, user=Depends(get_current_user), db=Depends(get_db)
):
    doc = Documents.update_doc_content_by_name(
        db, form_data.name, {"tags": form_data.tags}
    )
Timothy J. Baek's avatar
Timothy J. Baek committed
120
121
122
123
124
125
126
127

    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
128
129
130
131
132
133
134
135
136
137
138
139
    else:
        raise HTTPException(
            status_code=status.HTTP_401_UNAUTHORIZED,
            detail=ERROR_MESSAGES.NOT_FOUND,
        )


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


Timothy J. Baek's avatar
Timothy J. Baek committed
140
@router.post("/doc/update", response_model=Optional[DocumentResponse])
Timothy J. Baek's avatar
Timothy J. Baek committed
141
async def update_doc_by_name(
142
143
144
145
    name: str,
    form_data: DocumentUpdateForm,
    user=Depends(get_admin_user),
    db=Depends(get_db),
Timothy J. Baek's avatar
Timothy J. Baek committed
146
):
147
    doc = Documents.update_doc_by_name(db, name, form_data)
Timothy J. Baek's avatar
Timothy J. Baek committed
148
    if doc:
Timothy J. Baek's avatar
Timothy J. Baek committed
149
150
151
152
153
154
        return DocumentResponse(
            **{
                **doc.model_dump(),
                "content": json.loads(doc.content if doc.content else "{}"),
            }
        )
Timothy J. Baek's avatar
Timothy J. Baek committed
155
156
    else:
        raise HTTPException(
157
158
            status_code=status.HTTP_400_BAD_REQUEST,
            detail=ERROR_MESSAGES.NAME_TAG_TAKEN,
Timothy J. Baek's avatar
Timothy J. Baek committed
159
160
161
162
163
164
165
166
        )


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


Timothy J. Baek's avatar
Timothy J. Baek committed
167
@router.delete("/doc/delete", response_model=bool)
168
169
170
171
async def delete_doc_by_name(
    name: str, user=Depends(get_admin_user), db=Depends(get_db)
):
    result = Documents.delete_doc_by_name(db, name)
Timothy J. Baek's avatar
Timothy J. Baek committed
172
    return result