files.py 2.42 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
7

from sqlalchemy import Column, String, BigInteger

8
from apps.webui.internal.db import JSONField, Base, Session
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
27
28
29
    id = Column(String, primary_key=True)
    user_id = Column(String)
    filename = Column(String)
    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
Timothy J. Baek committed
64
65
66
67
68
69
70
71
72
        file = FileModel(
            **{
                **form_data.model_dump(),
                "user_id": user_id,
                "created_at": int(time.time()),
            }
        )

        try:
73
74
75
76
77
78
79
80
            result = File(**file.model_dump())
            Session.add(result)
            Session.commit()
            Session.refresh(result)
            if result:
                return FileModel.model_validate(result)
            else:
                return None
Timothy J. Baek's avatar
Timothy J. Baek committed
81
82
83
84
        except Exception as e:
            print(f"Error creating tool: {e}")
            return None

85
    def get_file_by_id(self, id: str) -> Optional[FileModel]:
Timothy J. Baek's avatar
Timothy J. Baek committed
86
        try:
87
88
            file = Session.get(File, id)
            return FileModel.model_validate(file)
Timothy J. Baek's avatar
Timothy J. Baek committed
89
90
91
        except:
            return None

92
    def get_files(self) -> List[FileModel]:
93
        return [FileModel.model_validate(file) for file in Session.query(File).all()]
Timothy J. Baek's avatar
Timothy J. Baek committed
94

95
    def delete_file_by_id(self, id: str) -> bool:
Timothy J. Baek's avatar
Timothy J. Baek committed
96
        try:
97
            Session.query(File).filter_by(id=id).delete()
Timothy J. Baek's avatar
Timothy J. Baek committed
98
99
100
101
            return True
        except:
            return False

102
    def delete_all_files(self) -> bool:
Timothy J. Baek's avatar
refac  
Timothy J. Baek committed
103
        try:
104
            Session.query(File).delete()
Timothy J. Baek's avatar
refac  
Timothy J. Baek committed
105
106
107
108
            return True
        except:
            return False

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

110
Files = FilesTable()