documents.py 4.53 KB
Newer Older
Timothy J. Baek's avatar
Timothy J. Baek committed
1
2
3
4
5
6
7
8
9
10
11
12
13
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,
Timothy J. Baek's avatar
Timothy J. Baek committed
14
    DocumentResponse,
Timothy J. Baek's avatar
Timothy J. Baek committed
15
16
17
18
19
20
21
22
23
24
25
26
)

from utils.utils import get_current_user
from constants import ERROR_MESSAGES

router = APIRouter()

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


Timothy J. Baek's avatar
Timothy J. Baek committed
27
@router.get("/", response_model=List[DocumentResponse])
Timothy J. Baek's avatar
Timothy J. Baek committed
28
async def get_documents(user=Depends(get_current_user)):
Timothy J. Baek's avatar
Timothy J. Baek committed
29
30
31
32
33
34
35
36
37
38
    docs = [
        DocumentResponse(
            **{
                **doc.model_dump(),
                "content": json.loads(doc.content if doc.content else "{}"),
            }
        )
        for doc in Documents.get_docs()
    ]
    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])
Timothy J. Baek's avatar
Timothy J. Baek committed
47
48
49
50
51
52
53
54
55
56
57
58
async def create_new_doc(form_data: DocumentForm, user=Depends(get_current_user)):
    if user.role != "admin":
        raise HTTPException(
            status_code=status.HTTP_401_UNAUTHORIZED,
            detail=ERROR_MESSAGES.ACCESS_PROHIBITED,
        )

    doc = Documents.get_doc_by_name(form_data.name)
    if doc == None:
        doc = Documents.insert_new_doc(user.id, form_data)

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


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


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

    if doc:
Timothy J. Baek's avatar
Timothy J. Baek committed
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
        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
############################


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 "{}"),
            }
        )
Timothy J. Baek's avatar
Timothy J. Baek committed
121
122
123
124
125
126
127
128
129
130
131
132
    else:
        raise HTTPException(
            status_code=status.HTTP_401_UNAUTHORIZED,
            detail=ERROR_MESSAGES.NOT_FOUND,
        )


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


Timothy J. Baek's avatar
Timothy J. Baek committed
133
@router.post("/name/{name}/update", response_model=Optional[DocumentResponse])
Timothy J. Baek's avatar
Timothy J. Baek committed
134
135
136
137
138
139
140
141
142
143
144
async def update_doc_by_name(
    name: str, form_data: DocumentUpdateForm, user=Depends(get_current_user)
):
    if user.role != "admin":
        raise HTTPException(
            status_code=status.HTTP_401_UNAUTHORIZED,
            detail=ERROR_MESSAGES.ACCESS_PROHIBITED,
        )

    doc = Documents.update_doc_by_name(name, form_data)
    if doc:
Timothy J. Baek's avatar
Timothy J. Baek committed
145
146
147
148
149
150
        return DocumentResponse(
            **{
                **doc.model_dump(),
                "content": json.loads(doc.content if doc.content else "{}"),
            }
        )
Timothy J. Baek's avatar
Timothy J. Baek committed
151
152
    else:
        raise HTTPException(
153
154
            status_code=status.HTTP_400_BAD_REQUEST,
            detail=ERROR_MESSAGES.NAME_TAG_TAKEN,
Timothy J. Baek's avatar
Timothy J. Baek committed
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
        )


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


@router.delete("/name/{name}/delete", response_model=bool)
async def delete_doc_by_name(name: str, user=Depends(get_current_user)):
    if user.role != "admin":
        raise HTTPException(
            status_code=status.HTTP_401_UNAUTHORIZED,
            detail=ERROR_MESSAGES.ACCESS_PROHIBITED,
        )

    result = Documents.delete_doc_by_name(name)
    return result