This commit is contained in:
50
database/tcontentdispatch/curd.py
Normal file
50
database/tcontentdispatch/curd.py
Normal file
@ -0,0 +1,50 @@
|
||||
from database.tcontentdispatch.model import TContentDispatch
|
||||
|
||||
|
||||
def create_content(db, content: TContentDispatch):
|
||||
db.add(content)
|
||||
db.commit()
|
||||
db.refresh(content)
|
||||
return content
|
||||
|
||||
|
||||
def create_or_update_content(db, content: TContentDispatch):
|
||||
content_in_db = db.query(TContentDispatch).filter(TContentDispatch.id == content.id).first()
|
||||
|
||||
if content_in_db:
|
||||
# Update existing content
|
||||
db.commit() # Save changes to the database
|
||||
db.refresh(content) # Refresh the object with the updated values
|
||||
else:
|
||||
# Create new content if not found in DB
|
||||
db.add(content)
|
||||
db.commit() # Save new content to the database
|
||||
db.refresh(content) # Refresh to get the content's updated state (e.g., ID if it's auto-generated)
|
||||
|
||||
def get_content_by_id(db, content_id: int):
|
||||
return db.query(TContentDispatch).filter(TContentDispatch.id == content_id).first()
|
||||
|
||||
def get_content_by_title_and_category(db, title: str, category: str):
|
||||
return db.query(TContentDispatch).filter(
|
||||
TContentDispatch.title == title,
|
||||
TContentDispatch.category == category
|
||||
).first()
|
||||
|
||||
def update_content(db, content_id: int, updates: dict):
|
||||
content = db.query(TContentDispatch).filter(TContentDispatch.id == content_id).first()
|
||||
if content:
|
||||
for key, value in updates.items():
|
||||
setattr(content, key, value)
|
||||
db.commit()
|
||||
db.refresh(content)
|
||||
return content
|
||||
|
||||
def update(db):
|
||||
db.commit()
|
||||
|
||||
def delete_content(db, content_id: int):
|
||||
content = db.query(TContentDispatch).filter(TContentDispatch.id == content_id).first()
|
||||
if content:
|
||||
db.delete(content)
|
||||
db.commit()
|
||||
return content
|
||||
27
database/tcontentdispatch/model.py
Normal file
27
database/tcontentdispatch/model.py
Normal file
@ -0,0 +1,27 @@
|
||||
from sqlalchemy import Column, Integer, String, Text, Boolean, TIMESTAMP, func
|
||||
from sqlalchemy.dialects.postgresql import BIGINT
|
||||
from dataclasses import dataclass
|
||||
from database.database import Base
|
||||
|
||||
@dataclass
|
||||
class TContentDispatch(Base):
|
||||
__tablename__ = 't_content_dispatch'
|
||||
|
||||
id: int = Column(BIGINT, primary_key=True, autoincrement=True, comment='自动递增的唯一任务ID')
|
||||
creation_date: str = Column(TIMESTAMP(timezone=True), server_default=func.now(), nullable=False, comment='记录数据条目的创建时间')
|
||||
is_sent: bool = Column(Boolean, default=False, nullable=False, comment='表示数据条目是否已被发送')
|
||||
category: str = Column(String(255), nullable=False, comment='类别')
|
||||
title: str = Column(String(255), nullable=False, comment='标题')
|
||||
cover_image: str = Column(Text, nullable=True, comment='封面')
|
||||
poster_image: str = Column(Text, nullable=True, comment='海报')
|
||||
opening_text: str = Column(Text, nullable=True, comment='开头语')
|
||||
content: str = Column(Text, nullable=False, comment='内容')
|
||||
ai_content: str = Column(Text, nullable=True, comment='AI编辑的内容')
|
||||
closing_text: str = Column(Text, nullable=True, comment='结束语')
|
||||
is_scheduled: bool = Column(Boolean, default=False, nullable=False, comment='是否设置为定时发送')
|
||||
schedule_time: str = Column(TIMESTAMP, nullable=True, comment='定时发送的具体时间')
|
||||
format: str = Column(String(50), nullable=True, comment='数据条目的格式')
|
||||
ai_generate: int = Column(Integer, default=0, nullable=False, comment='是否AI生成。0否,1部分参与,2是。')
|
||||
|
||||
def __repr__(self):
|
||||
return f"<TContentDispatch(id={self.id}, title={self.title}, category={self.category})>"
|
||||
Reference in New Issue
Block a user