admin.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. """Admin model for Work Statistics System."""
  2. from datetime import datetime, timezone
  3. from app import db
  4. class Admin(db.Model):
  5. """Admin model representing an administrator in the system.
  6. Attributes:
  7. id: Primary key, auto-incremented
  8. username: Admin's username (required, unique, non-empty)
  9. password_hash: Bcrypt hashed password (required)
  10. created_at: Timestamp when the record was created
  11. updated_at: Timestamp when the record was last updated
  12. """
  13. __tablename__ = 'admins'
  14. id = db.Column(db.Integer, primary_key=True, autoincrement=True)
  15. username = db.Column(db.String(100), nullable=False, unique=True, index=True)
  16. password_hash = db.Column(db.String(255), nullable=False)
  17. created_at = db.Column(db.DateTime, default=lambda: datetime.now(timezone.utc))
  18. updated_at = db.Column(db.DateTime, default=lambda: datetime.now(timezone.utc), onupdate=lambda: datetime.now(timezone.utc))
  19. def to_dict(self, include_password_hash=False):
  20. """Convert model to dictionary for JSON serialization.
  21. Args:
  22. include_password_hash: Whether to include password_hash in output (default False)
  23. Returns:
  24. Dictionary representation of the admin
  25. """
  26. result = {
  27. 'id': self.id,
  28. 'username': self.username,
  29. 'created_at': self.created_at.isoformat() if self.created_at else None,
  30. 'updated_at': self.updated_at.isoformat() if self.updated_at else None
  31. }
  32. if include_password_hash:
  33. result['password_hash'] = self.password_hash
  34. return result
  35. def __repr__(self):
  36. return f'<Admin {self.id}: {self.username}>'