TRM-coding | 286e678 | 2025-06-13 21:00:11 +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 | |
| 7 | |
| 8 | class User(Base): |
| 9 | __tablename__ = 'users' |
| 10 | |
TRM-coding | c2b517b | 2025-06-13 21:13:49 +0800 | [diff] [blame^] | 11 | def to_dict(self): |
| 12 | return { |
| 13 | 'id': self.id, |
| 14 | 'username': self.username if self.username else None, |
| 15 | 'email': self.email if self.email else None, |
| 16 | 'avatar': self.avatar if self.avatar else None, |
| 17 | 'role': self.role if self.role else None, |
| 18 | 'bio': self.bio if self.bio else None, |
| 19 | 'status': self.status if self.status else None, |
| 20 | 'created_at': self.created_at.isoformat() if self.created_at else None, |
| 21 | 'updated_at': self.updated_at.isoformat() if self.updated_at else None |
| 22 | } |
TRM-coding | 286e678 | 2025-06-13 21:00:11 +0800 | [diff] [blame] | 23 | |
| 24 | |
| 25 | |
| 26 | id = Column(Integer, primary_key=True, autoincrement=True, comment='用户ID') |
| 27 | username = Column(String(50), nullable=False, unique=True, comment='用户名') |
| 28 | password = Column(String(255), nullable=False, comment='加密密码') |
| 29 | email = Column(String(100), nullable=False, unique=True, comment='邮箱') |
| 30 | avatar = Column(String(255), comment='头像URL') |
| 31 | role = Column(Enum('user', 'admin', 'superadmin', name='user_role'), comment='角色') |
| 32 | bio = Column(String(255), comment='个人简介') |
| 33 | status = Column( |
| 34 | Enum('active','banned','muted', name='user_status'), |
| 35 | nullable=False, |
| 36 | server_default=text("'active'"), |
| 37 | comment='账号状态' |
| 38 | ) |
| 39 | created_at = Column( |
| 40 | TIMESTAMP, |
| 41 | nullable=True, |
| 42 | server_default=text('CURRENT_TIMESTAMP'), |
| 43 | comment='创建时间' |
| 44 | ) |
| 45 | updated_at = Column( |
| 46 | TIMESTAMP, |
| 47 | nullable=True, |
| 48 | server_default=text('CURRENT_TIMESTAMP'), |
| 49 | onupdate=text('CURRENT_TIMESTAMP'), |
| 50 | comment='更新时间' |
| 51 | ) |