| 1234567891011121314151617181920212223242526272829303132333435363738394041424344 |
- """Admin model for Work Statistics System."""
- from datetime import datetime, timezone
- from app import db
- class Admin(db.Model):
- """Admin model representing an administrator in the system.
-
- Attributes:
- id: Primary key, auto-incremented
- username: Admin's username (required, unique, non-empty)
- password_hash: Bcrypt hashed password (required)
- created_at: Timestamp when the record was created
- updated_at: Timestamp when the record was last updated
- """
- __tablename__ = 'admins'
-
- id = db.Column(db.Integer, primary_key=True, autoincrement=True)
- username = db.Column(db.String(100), nullable=False, unique=True, index=True)
- password_hash = db.Column(db.String(255), nullable=False)
- created_at = db.Column(db.DateTime, default=lambda: datetime.now(timezone.utc))
- updated_at = db.Column(db.DateTime, default=lambda: datetime.now(timezone.utc), onupdate=lambda: datetime.now(timezone.utc))
-
- def to_dict(self, include_password_hash=False):
- """Convert model to dictionary for JSON serialization.
-
- Args:
- include_password_hash: Whether to include password_hash in output (default False)
-
- Returns:
- Dictionary representation of the admin
- """
- result = {
- 'id': self.id,
- 'username': self.username,
- 'created_at': self.created_at.isoformat() if self.created_at else None,
- 'updated_at': self.updated_at.isoformat() if self.updated_at else None
- }
- if include_password_hash:
- result['password_hash'] = self.password_hash
- return result
-
- def __repr__(self):
- return f'<Admin {self.id}: {self.username}>'
|