inventory/inventory/models/work_log.py

108 lines
4.6 KiB
Python

from typing import Optional, Any, List, TYPE_CHECKING
if TYPE_CHECKING:
from .inventory import Inventory
from .image import Image
from .users import User
from .work_note import WorkNote
from sqlalchemy import Boolean, Identity, Integer, ForeignKey, Unicode, DateTime, text
from sqlalchemy.orm import Mapped, mapped_column, relationship
import datetime
from . import db
from .image import ImageAttachable
from .image_links import worklog_images
from .work_note import WorkNote
class WorkLog(db.Model, ImageAttachable):
__tablename__ = 'work_log'
id: Mapped[int] = mapped_column(Integer, Identity(start=1, increment=1), primary_key=True)
start_time: Mapped[Optional[datetime.datetime]] = mapped_column(DateTime)
end_time: Mapped[Optional[datetime.datetime]] = mapped_column(DateTime)
notes: Mapped[Optional[str]] = mapped_column(Unicode())
complete: Mapped[Optional[bool]] = mapped_column(Boolean, server_default=text('((0))'))
followup: Mapped[Optional[bool]] = mapped_column(Boolean, server_default=text('((0))'))
contact_id: Mapped[Optional[int]] = mapped_column(Integer, ForeignKey("users.id"))
analysis: Mapped[Optional[bool]] = mapped_column(Boolean, server_default=text('((0))'))
work_item_id: Mapped[Optional[int]] = mapped_column(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')
updates: Mapped[List['WorkNote']] = relationship(
'WorkNote',
back_populates='work_log',
cascade='all, delete-orphan',
order_by='WorkNote.timestamp'
)
images: Mapped[List['Image']] = relationship('Image', secondary=worklog_images, back_populates='worklogs', passive_deletes=True)
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,
updates: Optional[List[WorkNote]] = None
) -> 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
self.updates = updates or []
def __repr__(self):
return f"<WorkLog(id={self.id}, start_time={self.start_time}, end_time={self.end_time}, " \
f"notes={repr(self.notes)}, complete={self.complete}, followup={self.followup}, " \
f"contact_id={self.contact_id}, analysis={self.analysis}, work_item_id={self.work_item_id})>"
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,
'updates': [note.serialize() for note in self.updates or []],
'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")
updates_raw = data.get("updates", [])
updates: list[WorkNote] = []
for u in updates_raw:
if isinstance(u, dict):
content = u.get("content", "").strip()
else:
content = str(u).strip()
if content:
updates.append(WorkNote(content=content))
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"), # Soon to be removed and sent to a farm upstate
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"),
updates=updates
)