| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546 |
- """Item model for Work Statistics System."""
- from datetime import datetime, timezone
- from app import db
- class Item(db.Model):
- """Item model representing a work item with unit price.
-
- Attributes:
- id: Primary key, auto-incremented
- name: Item's name (required, unique)
- unit_price: Price per unit (required, positive float)
- supplier_id: Foreign key to supplier (optional)
- created_at: Timestamp when the record was created
- updated_at: Timestamp when the record was last updated
- """
- __tablename__ = 'items'
-
- id = db.Column(db.Integer, primary_key=True, autoincrement=True)
- name = db.Column(db.String(100), nullable=False, unique=True, index=True)
- unit_price = db.Column(db.Float, nullable=False)
- supplier_id = db.Column(db.Integer, db.ForeignKey('suppliers.id'), nullable=True)
- 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))
-
- # Relationship to Supplier
- supplier = db.relationship('Supplier', backref=db.backref('items', lazy='dynamic'))
-
- def to_dict(self):
- """Convert model to dictionary for JSON serialization.
-
- Returns:
- Dictionary representation of the item
- """
- return {
- 'id': self.id,
- 'name': self.name,
- 'unit_price': self.unit_price,
- 'supplier_id': self.supplier_id,
- 'supplier_name': self.supplier.name if self.supplier else '',
- 'created_at': self.created_at.isoformat() if self.created_at else None,
- 'updated_at': self.updated_at.isoformat() if self.updated_at else None
- }
-
- def __repr__(self):
- return f'<Item {self.id}: {self.name} @ {self.unit_price}>'
|