2024-01-08 07:43:32 +00:00
|
|
|
from pydantic import BaseModel
|
|
|
|
from peewee import *
|
|
|
|
from playhouse.shortcuts import model_to_dict
|
|
|
|
from typing import List, Union, Optional
|
|
|
|
import time
|
2024-03-20 23:11:36 +00:00
|
|
|
import logging
|
2024-01-08 07:43:32 +00:00
|
|
|
|
|
|
|
from utils.utils import decode_token
|
|
|
|
from utils.misc import get_gravatar_url
|
|
|
|
|
2024-05-26 08:15:48 +00:00
|
|
|
from apps.webui.internal.db import 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
|
|
|
|
####################
|
|
|
|
|
|
|
|
|
|
|
|
class Document(Model):
|
|
|
|
collection_name = CharField(unique=True)
|
|
|
|
name = CharField(unique=True)
|
2024-04-24 17:10:18 +00:00
|
|
|
title = TextField()
|
|
|
|
filename = TextField()
|
2024-01-08 07:43:32 +00:00
|
|
|
content = TextField(null=True)
|
|
|
|
user_id = CharField()
|
2024-04-24 17:10:18 +00:00
|
|
|
timestamp = BigIntegerField()
|
2024-01-08 07:43:32 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
database = DB
|
|
|
|
|
|
|
|
|
|
|
|
class DocumentModel(BaseModel):
|
|
|
|
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 __init__(self, db):
|
|
|
|
self.db = db
|
|
|
|
self.db.create_tables([Document])
|
|
|
|
|
|
|
|
def insert_new_doc(
|
|
|
|
self, user_id: str, form_data: DocumentForm
|
|
|
|
) -> Optional[DocumentModel]:
|
|
|
|
document = DocumentModel(
|
|
|
|
**{
|
|
|
|
**form_data.model_dump(),
|
|
|
|
"user_id": user_id,
|
|
|
|
"timestamp": int(time.time()),
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
try:
|
|
|
|
result = Document.create(**document.model_dump())
|
|
|
|
if result:
|
|
|
|
return document
|
|
|
|
else:
|
|
|
|
return None
|
|
|
|
except:
|
|
|
|
return None
|
|
|
|
|
|
|
|
def get_doc_by_name(self, name: str) -> Optional[DocumentModel]:
|
|
|
|
try:
|
|
|
|
document = Document.get(Document.name == name)
|
|
|
|
return DocumentModel(**model_to_dict(document))
|
|
|
|
except:
|
|
|
|
return None
|
|
|
|
|
|
|
|
def get_docs(self) -> List[DocumentModel]:
|
|
|
|
return [
|
|
|
|
DocumentModel(**model_to_dict(doc))
|
|
|
|
for doc in Document.select()
|
|
|
|
# .limit(limit).offset(skip)
|
|
|
|
]
|
|
|
|
|
|
|
|
def update_doc_by_name(
|
|
|
|
self, name: str, form_data: DocumentUpdateForm
|
|
|
|
) -> Optional[DocumentModel]:
|
|
|
|
try:
|
|
|
|
query = Document.update(
|
|
|
|
title=form_data.title,
|
|
|
|
name=form_data.name,
|
|
|
|
timestamp=int(time.time()),
|
|
|
|
).where(Document.name == name)
|
|
|
|
query.execute()
|
|
|
|
|
2024-01-08 09:49:20 +00:00
|
|
|
doc = Document.get(Document.name == form_data.name)
|
2024-01-08 07:43:32 +00:00
|
|
|
return DocumentModel(**model_to_dict(doc))
|
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(
|
|
|
|
self, name: str, updated: dict
|
|
|
|
) -> Optional[DocumentModel]:
|
|
|
|
try:
|
|
|
|
doc = self.get_doc_by_name(name)
|
|
|
|
doc_content = json.loads(doc.content if doc.content else "{}")
|
|
|
|
doc_content = {**doc_content, **updated}
|
|
|
|
|
|
|
|
query = Document.update(
|
|
|
|
content=json.dumps(doc_content),
|
|
|
|
timestamp=int(time.time()),
|
|
|
|
).where(Document.name == name)
|
|
|
|
query.execute()
|
|
|
|
|
|
|
|
doc = Document.get(Document.name == name)
|
|
|
|
return DocumentModel(**model_to_dict(doc))
|
|
|
|
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-01-08 07:43:32 +00:00
|
|
|
def delete_doc_by_name(self, name: str) -> bool:
|
|
|
|
try:
|
|
|
|
query = Document.delete().where((Document.name == name))
|
|
|
|
query.execute() # Remove the rows, return number of rows removed.
|
|
|
|
|
|
|
|
return True
|
|
|
|
except:
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
Documents = DocumentsTable(DB)
|