Raver | afc93da | 2025-06-15 18:12:49 +0800 | [diff] [blame] | 1 | from . import Base |
| 2 | from sqlalchemy import ( |
| 3 | Column, Integer, String, Enum, TIMESTAMP, text |
| 4 | ) |
| 5 | from sqlalchemy.ext.declarative import declarative_base |
| 6 | from sqlalchemy.orm import relationship |
| 7 | |
| 8 | |
| 9 | class User(Base): |
| 10 | __tablename__ = 'users' |
| 11 | |
| 12 | def to_dict(self): |
| 13 | return { |
| 14 | 'id': self.id, |
| 15 | 'username': self.username if self.username else None, |
| 16 | 'email': self.email if self.email else None, |
| 17 | 'avatar': self.avatar if self.avatar else None, |
| 18 | 'role': self.role if self.role else None, |
| 19 | 'bio': self.bio if self.bio else None, |
| 20 | 'status': self.status if self.status else None, |
| 21 | 'created_at': self.created_at.isoformat() if self.created_at else None, |
| 22 | 'updated_at': self.updated_at.isoformat() if self.updated_at else None |
| 23 | } |
| 24 | |
| 25 | id = Column(Integer, primary_key=True, autoincrement=True, comment='用户ID') |
| 26 | username = Column(String(50), nullable=False, unique=True, comment='用户名') |
| 27 | password = Column(String(255), nullable=False, comment='加密密码') |
| 28 | email = Column(String(100), nullable=False, unique=True, comment='邮箱') |
| 29 | avatar = Column(String(255), comment='头像URL') |
| 30 | role = Column(Enum('user', 'admin', 'superadmin', name='user_role'), comment='角色') |
| 31 | bio = Column(String(255), comment='个人简介') |
| 32 | status = Column( |
| 33 | Enum('active','banned','muted', name='user_status'), |
| 34 | nullable=False, |
| 35 | server_default=text("'active'"), |
| 36 | comment='账号状态' |
| 37 | ) |
| 38 | created_at = Column( |
| 39 | TIMESTAMP, |
| 40 | nullable=True, |
| 41 | server_default=text('CURRENT_TIMESTAMP'), |
| 42 | comment='创建时间' |
| 43 | ) |
| 44 | updated_at = Column( |
| 45 | TIMESTAMP, |
| 46 | nullable=True, |
| 47 | server_default=text('CURRENT_TIMESTAMP'), |
| 48 | onupdate=text('CURRENT_TIMESTAMP'), |
| 49 | comment='更新时间' |
| 50 | ) |
| 51 | |
| 52 | # 关联关系 |
| 53 | email_verifications = relationship("EmailVerification", back_populates="user") |