2024-06-18 13:03:31 +00:00
|
|
|
from pydantic import BaseModel, ConfigDict
|
|
|
|
from typing import List, Optional
|
2024-01-08 07:43:32 +00:00
|
|
|
import time
|
2024-03-20 23:11:36 +00:00
|
|
|
import logging
|
2024-01-08 07:43:32 +00:00
|
|
|
|
2024-06-24 11:21:51 +00:00
|
|
|
from sqlalchemy import String, Column, BigInteger, Text
|
2024-01-08 07:43:32 +00:00
|
|
|
|
2024-07-04 06:32:39 +00:00
|
|
|
from apps.webui.internal.db import Base, get_db
|
2024-01-08 07:43:32 +00:00
|
|
|
|
|
|
|
import json
|
|
|
|
|
2024-03-20 23:11:36 +00:00
|
|
|
from config import SRC_LOG_LEVELS
|
2024-03-31 08:13:39 +00:00
|
|
|
|
2024-03-20 23:11:36 +00:00
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
log.setLevel(SRC_LOG_LEVELS["MODELS"])
|
|
|
|
|
2024-01-08 07:43:32 +00:00
|
|
|
####################
|
|
|
|
# Documents DB Schema
|
|
|
|
####################
|
|
|
|
|
|
|
|
|
2024-06-18 13:03:31 +00:00
|
|
|
class Document(Base):
|
|
|
|
__tablename__ = "document"
|
2024-01-08 07:43:32 +00:00
|
|
|
|
2024-06-18 13:03:31 +00:00
|
|
|
collection_name = Column(String, primary_key=True)
|
|
|
|
name = Column(String, unique=True)
|
2024-06-24 11:21:51 +00:00
|
|
|
title = Column(Text)
|
|
|
|
filename = Column(Text)
|
|
|
|
content = Column(Text, nullable=True)
|
2024-06-18 13:03:31 +00:00
|
|
|
user_id = Column(String)
|
|
|
|
timestamp = Column(BigInteger)
|
2024-01-08 07:43:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
class DocumentModel(BaseModel):
|
2024-06-18 13:03:31 +00:00
|
|
|
model_config = ConfigDict(from_attributes=True)
|
|
|
|
|
2024-01-08 07:43:32 +00:00
|
|
|
collection_name: str
|
|
|
|
name: str
|
|
|
|
title: str
|
|
|
|
filename: str
|
|
|
|
content: Optional[str] = None
|
|
|
|
user_id: str
|
|
|
|
timestamp: int # timestamp in epoch
|
|
|
|
|
|
|
|
|
|
|
|
####################
|
|
|
|
# Forms
|
|
|
|
####################
|
|
|
|
|
|
|
|
|
2024-02-03 22:44:49 +00:00
|
|
|
class DocumentResponse(BaseModel):
|
|
|
|
collection_name: str
|
|
|
|
name: str
|
|
|
|
title: str
|
|
|
|
filename: str
|
|
|
|
content: Optional[dict] = None
|
|
|
|
user_id: str
|
|
|
|
timestamp: int # timestamp in epoch
|
|
|
|
|
|
|
|
|
2024-01-08 07:43:32 +00:00
|
|
|
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(
|
2024-06-21 12:58:57 +00:00
|
|
|
self, user_id: str, form_data: DocumentForm
|
2024-01-08 07:43:32 +00:00
|
|
|
) -> Optional[DocumentModel]:
|
2024-07-04 06:32:39 +00:00
|
|
|
with get_db() as db:
|
2024-01-08 07:43:32 +00:00
|
|
|
|
2024-07-04 06:32:39 +00:00
|
|
|
document = DocumentModel(
|
|
|
|
**{
|
|
|
|
**form_data.model_dump(),
|
|
|
|
"user_id": user_id,
|
|
|
|
"timestamp": int(time.time()),
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
try:
|
|
|
|
result = Document(**document.model_dump())
|
|
|
|
db.add(result)
|
|
|
|
db.commit()
|
|
|
|
db.refresh(result)
|
|
|
|
if result:
|
|
|
|
return DocumentModel.model_validate(result)
|
|
|
|
else:
|
|
|
|
return None
|
|
|
|
except:
|
2024-06-24 11:06:15 +00:00
|
|
|
return None
|
2024-01-08 07:43:32 +00:00
|
|
|
|
2024-06-21 12:58:57 +00:00
|
|
|
def get_doc_by_name(self, name: str) -> Optional[DocumentModel]:
|
2024-01-08 07:43:32 +00:00
|
|
|
try:
|
2024-07-04 06:32:39 +00:00
|
|
|
with get_db() as db:
|
|
|
|
|
|
|
|
document = db.query(Document).filter_by(name=name).first()
|
|
|
|
return DocumentModel.model_validate(document) if document else None
|
2024-01-08 07:43:32 +00:00
|
|
|
except:
|
|
|
|
return None
|
|
|
|
|
2024-06-21 12:58:57 +00:00
|
|
|
def get_docs(self) -> List[DocumentModel]:
|
2024-07-04 06:32:39 +00:00
|
|
|
with get_db() as db:
|
|
|
|
|
|
|
|
return [
|
|
|
|
DocumentModel.model_validate(doc) for doc in db.query(Document).all()
|
|
|
|
]
|
2024-01-08 07:43:32 +00:00
|
|
|
|
|
|
|
def update_doc_by_name(
|
2024-06-21 12:58:57 +00:00
|
|
|
self, name: str, form_data: DocumentUpdateForm
|
2024-01-08 07:43:32 +00:00
|
|
|
) -> Optional[DocumentModel]:
|
|
|
|
try:
|
2024-07-04 06:32:39 +00:00
|
|
|
with get_db() as db:
|
|
|
|
|
|
|
|
db.query(Document).filter_by(name=name).update(
|
|
|
|
{
|
|
|
|
"title": form_data.title,
|
|
|
|
"name": form_data.name,
|
|
|
|
"timestamp": int(time.time()),
|
|
|
|
}
|
|
|
|
)
|
|
|
|
db.commit()
|
|
|
|
return self.get_doc_by_name(form_data.name)
|
2024-01-08 09:49:20 +00:00
|
|
|
except Exception as e:
|
2024-03-20 23:11:36 +00:00
|
|
|
log.exception(e)
|
2024-01-08 07:43:32 +00:00
|
|
|
return None
|
|
|
|
|
2024-02-03 22:44:49 +00:00
|
|
|
def update_doc_content_by_name(
|
2024-06-21 12:58:57 +00:00
|
|
|
self, name: str, updated: dict
|
2024-02-03 22:44:49 +00:00
|
|
|
) -> Optional[DocumentModel]:
|
|
|
|
try:
|
2024-06-24 11:06:15 +00:00
|
|
|
doc = self.get_doc_by_name(name)
|
|
|
|
doc_content = json.loads(doc.content if doc.content else "{}")
|
|
|
|
doc_content = {**doc_content, **updated}
|
|
|
|
|
2024-07-04 06:32:39 +00:00
|
|
|
with get_db() as db:
|
|
|
|
|
|
|
|
db.query(Document).filter_by(name=name).update(
|
|
|
|
{
|
|
|
|
"content": json.dumps(doc_content),
|
|
|
|
"timestamp": int(time.time()),
|
|
|
|
}
|
|
|
|
)
|
|
|
|
db.commit()
|
|
|
|
return self.get_doc_by_name(name)
|
2024-02-03 22:44:49 +00:00
|
|
|
except Exception as e:
|
2024-03-20 23:11:36 +00:00
|
|
|
log.exception(e)
|
2024-02-03 22:44:49 +00:00
|
|
|
return None
|
|
|
|
|
2024-06-21 12:58:57 +00:00
|
|
|
def delete_doc_by_name(self, name: str) -> bool:
|
2024-01-08 07:43:32 +00:00
|
|
|
try:
|
2024-07-04 06:32:39 +00:00
|
|
|
with get_db() as db:
|
|
|
|
|
|
|
|
db.query(Document).filter_by(name=name).delete()
|
|
|
|
return True
|
2024-01-08 07:43:32 +00:00
|
|
|
except:
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
2024-06-18 13:03:31 +00:00
|
|
|
Documents = DocumentsTable()
|