from typing import Optional, Any, TYPE_CHECKING if TYPE_CHECKING: from .inventory import Inventory from .users import User from sqlalchemy import Boolean, ForeignKeyConstraint, Identity, Integer, ForeignKey, Unicode, DateTime, text from sqlalchemy.orm import Mapped, mapped_column, relationship import datetime from . import db class WorkLog(db.Model): __tablename__ = 'Work Log' __table_args__ = ( ForeignKeyConstraint(['Work Item'], ['Inventory.ID'], name='Work Log$InventoryWork Log'), ) id: Mapped[int] = mapped_column("ID", Integer, Identity(start=1, increment=1), primary_key=True) start_time: Mapped[Optional[datetime.datetime]] = mapped_column('Start Timestamp', DateTime) end_time: Mapped[Optional[datetime.datetime]] = mapped_column('End Timestamp', DateTime) notes: Mapped[Optional[str]] = mapped_column('Description & Notes', Unicode()) complete: Mapped[Optional[bool]] = mapped_column("Complete", Boolean, server_default=text('((0))')) followup: Mapped[Optional[bool]] = mapped_column('Needs Follow-Up', Boolean, server_default=text('((0))')) contact_id: Mapped[Optional[int]] = mapped_column('Point of Contact', Integer, ForeignKey("Users.ID")) analysis: Mapped[Optional[bool]] = mapped_column('Needs Quick Analysis', Boolean, server_default=text('((0))')) work_item_id: Mapped[Optional[int]] = mapped_column("Work Item", Integer, ForeignKey("Inventory.ID")) work_item: Mapped[Optional['Inventory']] = relationship('Inventory', back_populates='work_logs') contact: Mapped[Optional['User']] = relationship('User', back_populates='work_logs') def __init__(self, start_time: Optional[datetime.datetime] = None, end_time: Optional[datetime.datetime] = None, notes: Optional[str] = None, complete: Optional[bool] = False, followup: Optional[bool] = False, contact_id: Optional[int] = None, analysis: Optional[bool] = False, work_item_id: Optional[int] = None): self.start_time = start_time self.end_time = end_time self.notes = notes self.complete = complete self.followup = followup self.contact_id = contact_id self.analysis = analysis self.work_item_id = work_item_id def __repr__(self): return f"" def serialize(self): return { 'id': self.id, 'start_time': self.start_time.isoformat() if self.start_time else None, 'end_time': self.end_time.isoformat() if self.end_time else None, 'notes': self.notes, 'complete': self.complete, 'followup': self.followup, 'contact_id': self.contact_id, 'analysis': self.analysis, 'work_item_id': self.work_item_id } @classmethod def from_dict(cls, data: dict[str, Any]) -> "WorkLog": start_time_str = data.get("start_time") end_time_str = data.get("end_time") return cls( start_time=datetime.datetime.fromisoformat(str(start_time_str)) if start_time_str else datetime.datetime.now(), end_time=datetime.datetime.fromisoformat(str(end_time_str)) if end_time_str else None, notes=data.get("notes"), complete=bool(data.get("complete", False)), followup=bool(data.get("followup", False)), analysis=bool(data.get("analysis", False)), contact_id=data.get("contact_id"), work_item_id=data.get("work_item_id") )