documents.py 3.88 KB
Newer Older
1
2
from pydantic import BaseModel, ConfigDict
from typing import List, Optional
Timothy J. Baek's avatar
Timothy J. Baek committed
3
import time
4
import logging
Timothy J. Baek's avatar
Timothy J. Baek committed
5

6
7
from sqlalchemy import String, Column, BigInteger
from sqlalchemy.orm import Session
Timothy J. Baek's avatar
Timothy J. Baek committed
8

9
from apps.webui.internal.db import Base
Timothy J. Baek's avatar
Timothy J. Baek committed
10
11
12

import json

13
from config import SRC_LOG_LEVELS
Timothy J. Baek's avatar
Timothy J. Baek committed
14

15
16
17
log = logging.getLogger(__name__)
log.setLevel(SRC_LOG_LEVELS["MODELS"])

Timothy J. Baek's avatar
Timothy J. Baek committed
18
19
20
21
22
####################
# Documents DB Schema
####################


23
24
class Document(Base):
    __tablename__ = "document"
Timothy J. Baek's avatar
Timothy J. Baek committed
25

26
27
28
29
30
31
32
    collection_name = Column(String, primary_key=True)
    name = Column(String, unique=True)
    title = Column(String)
    filename = Column(String)
    content = Column(String, nullable=True)
    user_id = Column(String)
    timestamp = Column(BigInteger)
Timothy J. Baek's avatar
Timothy J. Baek committed
33
34
35


class DocumentModel(BaseModel):
36
37
    model_config = ConfigDict(from_attributes=True)

Timothy J. Baek's avatar
Timothy J. Baek committed
38
39
40
41
42
43
44
45
46
47
48
49
50
51
    collection_name: str
    name: str
    title: str
    filename: str
    content: Optional[str] = None
    user_id: str
    timestamp: int  # timestamp in epoch


####################
# Forms
####################


Timothy J. Baek's avatar
Timothy J. Baek committed
52
53
54
55
56
57
58
59
60
61
class DocumentResponse(BaseModel):
    collection_name: str
    name: str
    title: str
    filename: str
    content: Optional[dict] = None
    user_id: str
    timestamp: int  # timestamp in epoch


Timothy J. Baek's avatar
Timothy J. Baek committed
62
63
64
65
66
67
68
69
70
71
72
73
74
75
class DocumentUpdateForm(BaseModel):
    name: str
    title: str


class DocumentForm(DocumentUpdateForm):
    collection_name: str
    filename: str
    content: Optional[str] = None


class DocumentsTable:

    def insert_new_doc(
76
        self, db: Session, user_id: str, form_data: DocumentForm
Timothy J. Baek's avatar
Timothy J. Baek committed
77
78
79
80
81
82
83
84
85
86
    ) -> Optional[DocumentModel]:
        document = DocumentModel(
            **{
                **form_data.model_dump(),
                "user_id": user_id,
                "timestamp": int(time.time()),
            }
        )

        try:
87
88
89
90
            result = Document(**document.model_dump())
            db.add(result)
            db.commit()
            db.refresh(result)
Timothy J. Baek's avatar
Timothy J. Baek committed
91
            if result:
92
                return DocumentModel.model_validate(result)
Timothy J. Baek's avatar
Timothy J. Baek committed
93
94
95
96
97
            else:
                return None
        except:
            return None

98
    def get_doc_by_name(self, db: Session, name: str) -> Optional[DocumentModel]:
Timothy J. Baek's avatar
Timothy J. Baek committed
99
        try:
100
101
            document = db.query(Document).filter_by(name=name).first()
            return DocumentModel.model_validate(document) if document else None
Timothy J. Baek's avatar
Timothy J. Baek committed
102
103
104
        except:
            return None

105
106
    def get_docs(self, db: Session) -> List[DocumentModel]:
        return [DocumentModel.model_validate(doc) for doc in db.query(Document).all()]
Timothy J. Baek's avatar
Timothy J. Baek committed
107
108

    def update_doc_by_name(
109
        self, db: Session, name: str, form_data: DocumentUpdateForm
Timothy J. Baek's avatar
Timothy J. Baek committed
110
111
    ) -> Optional[DocumentModel]:
        try:
112
113
114
115
116
117
118
119
            db.query(Document).filter_by(name=name).update(
                {
                    "title": form_data.title,
                    "name": form_data.name,
                    "timestamp": int(time.time()),
                }
            )
            return self.get_doc_by_name(db, form_data.name)
120
        except Exception as e:
121
            log.exception(e)
Timothy J. Baek's avatar
Timothy J. Baek committed
122
123
            return None

Timothy J. Baek's avatar
Timothy J. Baek committed
124
    def update_doc_content_by_name(
125
        self, db: Session, name: str, updated: dict
Timothy J. Baek's avatar
Timothy J. Baek committed
126
127
    ) -> Optional[DocumentModel]:
        try:
128
            doc = self.get_doc_by_name(db, name)
Timothy J. Baek's avatar
Timothy J. Baek committed
129
130
131
            doc_content = json.loads(doc.content if doc.content else "{}")
            doc_content = {**doc_content, **updated}

132
133
134
135
136
137
            db.query(Document).filter_by(name=name).update(
                {
                    "content": json.dumps(doc_content),
                    "timestamp": int(time.time()),
                }
            )
Timothy J. Baek's avatar
Timothy J. Baek committed
138

139
            return self.get_doc_by_name(db, name)
Timothy J. Baek's avatar
Timothy J. Baek committed
140
        except Exception as e:
141
            log.exception(e)
Timothy J. Baek's avatar
Timothy J. Baek committed
142
143
            return None

144
    def delete_doc_by_name(self, db: Session, name: str) -> bool:
Timothy J. Baek's avatar
Timothy J. Baek committed
145
        try:
146
            db.query(Document).filter_by(name=name).delete()
Timothy J. Baek's avatar
Timothy J. Baek committed
147
148
149
150
151
            return True
        except:
            return False


152
Documents = DocumentsTable()