files.py 2.47 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
8
9

from sqlalchemy import Column, String, BigInteger
from sqlalchemy.orm import Session

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

import json

from config import SRC_LOG_LEVELS

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

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


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

26
27
28
29
30
    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
31
32
33
34
35
36
37
38
39


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

40
    model_config = ConfigDict(from_attributes=True)
Timothy J. Baek's avatar
Timothy J. Baek committed
41
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, db: Session, 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
            result = File(**file.model_dump())
            db.add(result)
            db.commit()
            db.refresh(result)
Timothy J. Baek's avatar
Timothy J. Baek committed
77
            if result:
78
                return FileModel.model_validate(result)
Timothy J. Baek's avatar
Timothy J. Baek committed
79
80
81
82
83
84
            else:
                return None
        except Exception as e:
            print(f"Error creating tool: {e}")
            return None

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

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

95
    def delete_file_by_id(self, db: Session, id: str) -> bool:
Timothy J. Baek's avatar
Timothy J. Baek committed
96
        try:
97
            db.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, db: Session) -> bool:
Timothy J. Baek's avatar
refac  
Timothy J. Baek committed
103
        try:
104
            db.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()