feat:增加知识库数据接口
This commit is contained in:
@@ -1,5 +0,0 @@
|
||||
from pydantic import BaseModel
|
||||
|
||||
|
||||
class QueryRequest(BaseModel):
|
||||
message: str
|
||||
43
models/base.py
Normal file
43
models/base.py
Normal file
@@ -0,0 +1,43 @@
|
||||
from datetime import datetime
|
||||
|
||||
from sqlalchemy import Column, BigInteger, String, event, TIMESTAMP, func
|
||||
from sqlalchemy.ext.declarative import declared_attr
|
||||
|
||||
from utils.common import camel_to_snake
|
||||
|
||||
from database import Base
|
||||
from id_generator import options, generator
|
||||
|
||||
# https://github.com/yitter/IdGenerator/tree/master/Python
|
||||
options = options.IdGeneratorOptions(worker_id=23)
|
||||
idgen = generator.DefaultIdGenerator()
|
||||
idgen.set_id_generator(options)
|
||||
|
||||
|
||||
# 第一层基类:包含ID
|
||||
class IdBase(Base):
|
||||
__abstract__ = True
|
||||
|
||||
id = Column(BigInteger, primary_key=True, index=True)
|
||||
|
||||
@declared_attr
|
||||
def __tablename__(cls):
|
||||
# 自动把数据库实体类名驼峰转为数据库表名下划线
|
||||
return camel_to_snake(cls.__name__)
|
||||
|
||||
|
||||
# 自动填充id
|
||||
@event.listens_for(IdBase, 'before_insert', propagate=True)
|
||||
def before_insert_listener(mapper, connection, target):
|
||||
if target.id is None:
|
||||
target.id = idgen.next_id()
|
||||
|
||||
|
||||
# 第二层基类:包含ID和审计字段
|
||||
class AuditBase(IdBase):
|
||||
__abstract__ = True
|
||||
|
||||
create_time = Column(TIMESTAMP, nullable=False, default=datetime.now)
|
||||
create_by = Column(String(255), nullable=True)
|
||||
update_time = Column(TIMESTAMP, nullable=False, default=datetime.now, onupdate=datetime.now)
|
||||
update_by = Column(String(255), nullable=True)
|
||||
33
models/library.py
Normal file
33
models/library.py
Normal file
@@ -0,0 +1,33 @@
|
||||
from sqlalchemy import Column, BigInteger, String, Text, TIMESTAMP, Integer, ForeignKey
|
||||
from sqlalchemy.dialects.postgresql import JSONB
|
||||
from sqlalchemy.orm import relationship
|
||||
|
||||
from models.base import AuditBase
|
||||
|
||||
|
||||
class LibraryFile(AuditBase):
|
||||
subject = Column(String(45), nullable=False)
|
||||
module = Column(String(45), nullable=False)
|
||||
name = Column(String(45), nullable=False)
|
||||
type = Column(String(45), nullable=False)
|
||||
size = Column(Integer, nullable=False)
|
||||
|
||||
questions = relationship(
|
||||
"LibraryFileQuestion",
|
||||
back_populates="library_file",
|
||||
cascade="all, delete-orphan"
|
||||
)
|
||||
|
||||
|
||||
class LibraryFileQuestion(AuditBase):
|
||||
library_file_id = Column(BigInteger, ForeignKey("library_file.id"), nullable=False)
|
||||
|
||||
type = Column(String(45), nullable=False)
|
||||
question = Column(Text, nullable=False)
|
||||
options = Column(JSONB, nullable=False)
|
||||
answer = Column(JSONB, nullable=False)
|
||||
|
||||
library_file = relationship(
|
||||
"LibraryFile",
|
||||
back_populates="questions"
|
||||
)
|
||||
Reference in New Issue
Block a user