item.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. """Item model for Work Statistics System."""
  2. from datetime import datetime, timezone
  3. from app import db
  4. class Item(db.Model):
  5. """Item model representing a work item with unit price.
  6. Attributes:
  7. id: Primary key, auto-incremented
  8. name: Item's name (required, non-empty)
  9. unit_price: Price per unit (required, positive float)
  10. created_at: Timestamp when the record was created
  11. updated_at: Timestamp when the record was last updated
  12. """
  13. __tablename__ = 'items'
  14. id = db.Column(db.Integer, primary_key=True, autoincrement=True)
  15. name = db.Column(db.String(100), nullable=False, index=True)
  16. unit_price = db.Column(db.Float, 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):
  20. """Convert model to dictionary for JSON serialization.
  21. Returns:
  22. Dictionary representation of the item
  23. """
  24. return {
  25. 'id': self.id,
  26. 'name': self.name,
  27. 'unit_price': self.unit_price,
  28. 'created_at': self.created_at.isoformat() if self.created_at else None,
  29. 'updated_at': self.updated_at.isoformat() if self.updated_at else None
  30. }
  31. def __repr__(self):
  32. return f'<Item {self.id}: {self.name} @ {self.unit_price}>'