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

6
from sqlalchemy import Column, String, BigInteger, Text
7

Timothy J. Baek's avatar
refac  
Timothy J. Baek committed
8
from apps.webui.internal.db import JSONField, Base, get_db
Timothy J. Baek's avatar
Timothy J. Baek committed
9
10
11
12
13
14
15
16
17
18
19
20
21

import json

from config import SRC_LOG_LEVELS

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

####################
# Files DB Schema
####################


22
23
class File(Base):
    __tablename__ = "file"
Timothy J. Baek's avatar
Timothy J. Baek committed
24

25
26
    id = Column(String, primary_key=True)
    user_id = Column(String)
27
    filename = Column(Text)
28
29
    meta = Column(JSONField)
    created_at = Column(BigInteger)
Timothy J. Baek's avatar
Timothy J. Baek committed
30
31
32
33
34
35
36
37
38


class FileModel(BaseModel):
    id: str
    user_id: str
    filename: str
    meta: dict
    created_at: int  # timestamp in epoch

39
    model_config = ConfigDict(from_attributes=True)
Timothy J. Baek's avatar
Timothy J. Baek committed
40

41

Timothy J. Baek's avatar
Timothy J. Baek committed
42
43
44
45
46
####################
# Forms
####################


Timothy J. Baek's avatar
Timothy J. Baek committed
47
class FileModelResponse(BaseModel):
Timothy J. Baek's avatar
Timothy J. Baek committed
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
    id: str
    user_id: str
    filename: str
    meta: dict
    created_at: int  # timestamp in epoch


class FileForm(BaseModel):
    id: str
    filename: str
    meta: dict = {}


class FilesTable:

63
    def insert_new_file(self, user_id: str, form_data: FileForm) -> Optional[FileModel]:
Timothy J. Baek's avatar
refac  
Timothy J. Baek committed
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
        with get_db() as db:

            file = FileModel(
                **{
                    **form_data.model_dump(),
                    "user_id": user_id,
                    "created_at": int(time.time()),
                }
            )

            try:
                result = File(**file.model_dump())
                db.add(result)
                db.commit()
                db.refresh(result)
                if result:
                    return FileModel.model_validate(result)
                else:
                    return None
            except Exception as e:
                print(f"Error creating tool: {e}")
85
                return None
Timothy J. Baek's avatar
Timothy J. Baek committed
86

87
    def get_file_by_id(self, id: str) -> Optional[FileModel]:
Timothy J. Baek's avatar
refac  
Timothy J. Baek committed
88
89
90
91
92
93
94
        with get_db() as db:

            try:
                file = db.get(File, id)
                return FileModel.model_validate(file)
            except:
                return None
Timothy J. Baek's avatar
Timothy J. Baek committed
95

96
    def get_files(self) -> List[FileModel]:
Timothy J. Baek's avatar
refac  
Timothy J. Baek committed
97
98
99
        with get_db() as db:

            return [FileModel.model_validate(file) for file in db.query(File).all()]
Timothy J. Baek's avatar
Timothy J. Baek committed
100

101
    def delete_file_by_id(self, id: str) -> bool:
Timothy J. Baek's avatar
refac  
Timothy J. Baek committed
102
103
104
105
106
107
108
109

        with get_db() as db:

            try:
                db.query(File).filter_by(id=id).delete()
                return True
            except:
                return False
Timothy J. Baek's avatar
Timothy J. Baek committed
110

111
    def delete_all_files(self) -> bool:
Timothy J. Baek's avatar
refac  
Timothy J. Baek committed
112
113
114
115
116
117
118
119

        with get_db() as db:

            try:
                db.query(File).delete()
                return True
            except:
                return False
Timothy J. Baek's avatar
refac  
Timothy J. Baek committed
120

Timothy J. Baek's avatar
Timothy J. Baek committed
121

122
Files = FilesTable()