Add WorkNote model and integrate updates into WorkLog functionality

This commit is contained in:
Yaro Kasear 2025-07-09 15:39:55 -05:00
parent 58f8a040b7
commit 39a8e64c90
6 changed files with 123 additions and 18 deletions

View file

@ -10,6 +10,7 @@ from .room_functions import RoomFunction
from .users import User
from .work_log import WorkLog
from .rooms import Room
from .work_note import WorkNote
__all__ = [
"db",
@ -21,4 +22,5 @@ __all__ = [
"User",
"WorkLog",
"Room",
"WorkNote"
]

View file

@ -1,13 +1,15 @@
from typing import Optional, Any, TYPE_CHECKING
from typing import Optional, Any, List, TYPE_CHECKING
if TYPE_CHECKING:
from .inventory import Inventory
from .users import User
from .work_note import WorkNote
from sqlalchemy import Boolean, ForeignKeyConstraint, Identity, Integer, ForeignKey, Unicode, DateTime, text
from sqlalchemy import Boolean, Identity, Integer, ForeignKey, Unicode, DateTime, text
from sqlalchemy.orm import Mapped, mapped_column, relationship
import datetime
from . import db
from .work_note import WorkNote
class WorkLog(db.Model):
__tablename__ = 'work_log'
@ -24,11 +26,25 @@ class WorkLog(db.Model):
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'
)
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):
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
@ -37,7 +53,8 @@ class WorkLog(db.Model):
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}, " \
@ -49,6 +66,7 @@ class WorkLog(db.Model):
'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,
@ -60,14 +78,27 @@ class WorkLog(db.Model):
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"),
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")
)
work_item_id=data.get("work_item_id"),
updates=updates
)

View file

@ -0,0 +1,35 @@
import datetime
from sqlalchemy import ForeignKey, DateTime, UnicodeText, func
from sqlalchemy.orm import Mapped, mapped_column, relationship
from . import db
class WorkNote(db.Model):
__tablename__ = 'work_note'
__table_args__ = (
db.Index('ix_work_note_work_log_id', 'work_log_id'),
)
id: Mapped[int] = mapped_column(primary_key=True)
work_log_id: Mapped[int] = mapped_column(ForeignKey('work_log.id', ondelete='CASCADE'), nullable=False)
timestamp: Mapped[datetime.datetime] = mapped_column(DateTime, default=func.now(), server_default=func.now())
content: Mapped[str] = mapped_column(UnicodeText, nullable=False)
work_log = relationship('WorkLog', back_populates='updates')
def __init__(self, content: str, timestamp: datetime.datetime | None = None) -> None:
self.content = content
self.timestamp = timestamp or datetime.datetime.now()
def __repr__(self) -> str:
preview = self.content[:30].replace("\n", " ") + "..." if len(self.content) > 30 else self.content
return f"<WorkNote(id={self.id}), log_id={self.work_log_id}, ts={self.timestamp}, content={preview!r}>"
def serialize(self) -> dict:
return {
'id': self.id,
'work_log_id': self.work_log_id,
'timestamp': self.timestamp.isoformat(),
'content': self.content
}