合并JWL,WZY,TRM代码
Change-Id: Ifb4fcad3c06733e1e005e7d8d9403e3561010fb4
diff --git a/Merge/back_trm/app/models/__init__.py b/Merge/back_trm/app/models/__init__.py
new file mode 100644
index 0000000..f726a19
--- /dev/null
+++ b/Merge/back_trm/app/models/__init__.py
@@ -0,0 +1,8 @@
+from sqlalchemy.ext.declarative import declarative_base
+
+Base = declarative_base()
+
+# 先定义好 Base,再把所有 model import 进来,让 SQLAlchemy 一次性注册它们
+from .users import User
+from .topics import Topic
+from .post import Post
\ No newline at end of file
diff --git a/Merge/back_trm/app/models/__pycache__/__init__.cpython-310.pyc b/Merge/back_trm/app/models/__pycache__/__init__.cpython-310.pyc
new file mode 100644
index 0000000..015de51
--- /dev/null
+++ b/Merge/back_trm/app/models/__pycache__/__init__.cpython-310.pyc
Binary files differ
diff --git a/Merge/back_trm/app/models/__pycache__/post.cpython-310.pyc b/Merge/back_trm/app/models/__pycache__/post.cpython-310.pyc
new file mode 100644
index 0000000..8d33351
--- /dev/null
+++ b/Merge/back_trm/app/models/__pycache__/post.cpython-310.pyc
Binary files differ
diff --git a/Merge/back_trm/app/models/__pycache__/topics.cpython-310.pyc b/Merge/back_trm/app/models/__pycache__/topics.cpython-310.pyc
new file mode 100644
index 0000000..fba569b
--- /dev/null
+++ b/Merge/back_trm/app/models/__pycache__/topics.cpython-310.pyc
Binary files differ
diff --git a/Merge/back_trm/app/models/__pycache__/users.cpython-310.pyc b/Merge/back_trm/app/models/__pycache__/users.cpython-310.pyc
new file mode 100644
index 0000000..155a86c
--- /dev/null
+++ b/Merge/back_trm/app/models/__pycache__/users.cpython-310.pyc
Binary files differ
diff --git a/Merge/back_trm/app/models/post.py b/Merge/back_trm/app/models/post.py
new file mode 100644
index 0000000..041e263
--- /dev/null
+++ b/Merge/back_trm/app/models/post.py
@@ -0,0 +1,111 @@
+from .users import User
+from . import Base
+
+from sqlalchemy import (
+ Column, Integer, String, Text, JSON, Enum,
+ TIMESTAMP, ForeignKey, Index, func, text
+)
+from sqlalchemy.ext.declarative import declarative_base
+from sqlalchemy.orm import relationship
+
+
+class Post(Base):
+ __tablename__ = 'posts'
+ __table_args__ = (
+ # 索引
+ Index('idx_posts_heat', 'heat'),
+ # MySQL 引擎、字符集、校对规则、表注释
+ {
+ 'mysql_engine': 'InnoDB',
+ 'mysql_charset': 'utf8mb4',
+ 'mysql_collate': 'utf8mb4_general_ci',
+ 'comment': '内容帖子表'
+ }
+ )
+
+ def to_dict(self):
+ return {
+ 'id': self.id if self.id else None,
+ 'user_id': self.user_id if self.user_id else None,
+ 'topic_id': self.topic_id if self.topic_id else None,
+ 'type': self.type if self.type else None,
+ 'title': self.title if self.title else None,
+ 'content': self.content if self.content else None,
+ 'media_urls': self.media_urls if self.media_urls else None,
+ 'status': self.status if self.status else None,
+ 'heat': self.heat if self.heat else None,
+ 'created_at': self.created_at.isoformat() if self.created_at else None,
+ 'updated_at': self.updated_at.isoformat() if self.updated_at else None
+ }
+
+
+ id = Column(
+ Integer,
+ primary_key=True,
+ autoincrement=True,
+ comment='帖子ID'
+ )
+ user_id = Column(
+ Integer,
+ ForeignKey('users.id', ondelete='CASCADE'),
+ nullable=False,
+ index=True,
+ comment='作者ID'
+ )
+ topic_id = Column(
+ Integer,
+ ForeignKey('topics.id', ondelete='SET NULL'),
+ nullable=True,
+ index=True,
+ comment='所属话题ID'
+ )
+ type = Column(
+ Enum('text', 'image', 'video', 'document', name='post_type'),
+ nullable=False,
+ server_default=text("'text'"),
+ comment='内容类型'
+ )
+ title = Column(
+ String(255),
+ nullable=False,
+ comment='标题'
+ )
+ content = Column(
+ Text,
+ nullable=False,
+ comment='正文内容'
+ )
+ media_urls = Column(
+ JSON,
+ nullable=True,
+ comment='媒体资源URL数组'
+ )
+ status = Column(
+ Enum('draft', 'pending', 'published', 'deleted', 'rejected', name='post_status'),
+ nullable=False,
+ server_default=text("'draft'"),
+ comment='状态'
+ )
+ heat = Column(
+ Integer,
+ nullable=False,
+ server_default=text('0'),
+ comment='热度值'
+ )
+ created_at = Column(
+ TIMESTAMP,
+ nullable=True,
+ server_default=func.current_timestamp(),
+ comment='创建时间'
+ )
+ updated_at = Column(
+ TIMESTAMP,
+ nullable=True,
+ server_default=func.current_timestamp(),
+ onupdate=func.current_timestamp(),
+ comment='更新时间'
+ )
+
+ # 可选:与 User/Topic 模型的关系(需要在 User、Topic 中也定义 back_populates)
+ # user = relationship('User', back_populates='posts')
+ # topic = relationship('Topic', back_populates='posts')
diff --git a/Merge/back_trm/app/models/token.py b/Merge/back_trm/app/models/token.py
new file mode 100644
index 0000000..cbe864b
--- /dev/null
+++ b/Merge/back_trm/app/models/token.py
@@ -0,0 +1,27 @@
+from sqlalchemy import Column, Integer, String, DateTime, TIMESTAMP, Index
+from sqlalchemy.ext.declarative import declarative_base
+from sqlalchemy.sql import func
+from datetime import datetime
+
+Base = declarative_base()
+
+class Token(Base):
+ __tablename__ = 'tokens'
+
+ id = Column(Integer, primary_key=True, autoincrement=True)
+ token = Column(String(255), nullable=False, unique=True)
+ expires_at = Column(DateTime, nullable=False)
+ created_at = Column(TIMESTAMP, default=func.current_timestamp())
+ updated_at = Column(TIMESTAMP, default=func.current_timestamp(), onupdate=func.current_timestamp())
+
+ __table_args__ = (
+ Index('idx_token', 'token'),
+ Index('idx_expires_at', 'expires_at'),
+ )
+
+ def __repr__(self):
+ return f"<Token(id={self.id}, token='{self.token[:10]}...', expires_at={self.expires_at})>"
+
+ def is_expired(self):
+ """检查token是否已过期"""
+ return datetime.now() > self.expires_at
\ No newline at end of file
diff --git a/Merge/back_trm/app/models/topics.py b/Merge/back_trm/app/models/topics.py
new file mode 100644
index 0000000..1a35a38
--- /dev/null
+++ b/Merge/back_trm/app/models/topics.py
@@ -0,0 +1,26 @@
+from . import Base
+from sqlalchemy import Column, Integer, String, Text, Enum, TIMESTAMP
+from sqlalchemy.sql import func
+
+class Topic(Base):
+ __tablename__ = 'topics'
+ __table_args__ = {
+ 'mysql_engine': 'InnoDB',
+ 'mysql_charset': 'utf8mb4',
+ 'mysql_collate': 'utf8mb4_general_ci',
+ 'comment': '话题/超话表'
+ }
+
+ id = Column(Integer, primary_key=True, autoincrement=True, comment='话题ID')
+ name = Column(String(100, collation='utf8mb4_general_ci'), nullable=False, unique=True, comment='话题名称')
+ description = Column(Text(collation='utf8mb4_general_ci'), comment='话题描述')
+ status = Column(
+ Enum('active', 'archived', name='topic_status', collation='utf8mb4_general_ci'),
+ default='active',
+ comment='状态'
+ )
+ created_at = Column(
+ TIMESTAMP,
+ server_default=func.current_timestamp(),
+ comment='创建时间'
+ )
\ No newline at end of file
diff --git a/Merge/back_trm/app/models/users.py b/Merge/back_trm/app/models/users.py
new file mode 100644
index 0000000..0505e86
--- /dev/null
+++ b/Merge/back_trm/app/models/users.py
@@ -0,0 +1,51 @@
+from . import Base
+from sqlalchemy import (
+ Column, Integer, String, Enum, TIMESTAMP, text
+)
+from sqlalchemy.ext.declarative import declarative_base
+
+
+class User(Base):
+ __tablename__ = 'users'
+
+ def to_dict(self):
+ return {
+ 'id': self.id,
+ 'username': self.username if self.username else None,
+ 'email': self.email if self.email else None,
+ 'avatar': self.avatar if self.avatar else None,
+ 'role': self.role if self.role else None,
+ 'bio': self.bio if self.bio else None,
+ 'status': self.status if self.status else None,
+ 'created_at': self.created_at.isoformat() if self.created_at else None,
+ 'updated_at': self.updated_at.isoformat() if self.updated_at else None
+ }
+
+
+
+ id = Column(Integer, primary_key=True, autoincrement=True, comment='用户ID')
+ username = Column(String(50), nullable=False, unique=True, comment='用户名')
+ password = Column(String(255), nullable=False, comment='加密密码')
+ email = Column(String(100), nullable=False, unique=True, comment='邮箱')
+ avatar = Column(String(255), comment='头像URL')
+ role = Column(Enum('user', 'admin', 'superadmin', name='user_role'), comment='角色')
+ bio = Column(String(255), comment='个人简介')
+ status = Column(
+ Enum('active','banned','muted', name='user_status'),
+ nullable=False,
+ server_default=text("'active'"),
+ comment='账号状态'
+ )
+ created_at = Column(
+ TIMESTAMP,
+ nullable=True,
+ server_default=text('CURRENT_TIMESTAMP'),
+ comment='创建时间'
+ )
+ updated_at = Column(
+ TIMESTAMP,
+ nullable=True,
+ server_default=text('CURRENT_TIMESTAMP'),
+ onupdate=text('CURRENT_TIMESTAMP'),
+ comment='更新时间'
+ )
\ No newline at end of file