import arlo
This commit is contained in:
0
database/tcontentdispatch/__init__.py
Normal file
0
database/tcontentdispatch/__init__.py
Normal file
BIN
database/tcontentdispatch/__pycache__/__init__.cpython-312.pyc
Normal file
BIN
database/tcontentdispatch/__pycache__/__init__.cpython-312.pyc
Normal file
Binary file not shown.
BIN
database/tcontentdispatch/__pycache__/curd.cpython-312.pyc
Normal file
BIN
database/tcontentdispatch/__pycache__/curd.cpython-312.pyc
Normal file
Binary file not shown.
BIN
database/tcontentdispatch/__pycache__/model.cpython-312.pyc
Normal file
BIN
database/tcontentdispatch/__pycache__/model.cpython-312.pyc
Normal file
Binary file not shown.
69
database/tcontentdispatch/curd.py
Normal file
69
database/tcontentdispatch/curd.py
Normal file
@ -0,0 +1,69 @@
|
||||
from datetime import datetime, timedelta, timezone
|
||||
|
||||
from sqlalchemy import desc
|
||||
|
||||
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 get_contents_to_dispatch(db) -> list[TContentDispatch]:
|
||||
return db.query(TContentDispatch).filter(TContentDispatch.is_sent == False).all()
|
||||
|
||||
def get_recent_24_hours_content_to_dispatch(db, category: str) -> TContentDispatch:
|
||||
# 获取查询日期的起始和结束时间
|
||||
end_time = datetime.now(timezone.utc)
|
||||
start_time = end_time - timedelta(hours=24)
|
||||
# 查询最近的一条未发送的内容
|
||||
return db.query(TContentDispatch).filter(
|
||||
TContentDispatch.category == category,
|
||||
# TContentDispatch.is_sent == False,
|
||||
TContentDispatch.creation_date >= start_time,
|
||||
TContentDispatch.creation_date < end_time
|
||||
).order_by(desc(TContentDispatch.creation_date)).first()
|
||||
|
||||
def finish_contents_to_dispatch(db, ids):
|
||||
db.query(TContentDispatch).filter(TContentDispatch.id.in_(ids)).update({'is_sent': True})
|
||||
db.commit()
|
||||
|
||||
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 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
|
||||
33
database/tcontentdispatch/model.py
Normal file
33
database/tcontentdispatch/model.py
Normal file
@ -0,0 +1,33 @@
|
||||
from datetime import datetime
|
||||
|
||||
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: datetime = 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})>"
|
||||
|
||||
def get_creation_date_in_localtime(self) -> datetime:
|
||||
# 将 UTC 时间转换为localtime
|
||||
return self.creation_date.astimezone()
|
||||
Reference in New Issue
Block a user