Implement PhotoAttachable interface in Inventory, User, and WorkLog models; add photo upload API endpoint with file handling and attachment logic.
This commit is contained in:
parent
92dce08d1c
commit
e1cb99f2d1
7 changed files with 101 additions and 7 deletions
|
@ -11,8 +11,9 @@ from sqlalchemy.orm import Mapped, mapped_column, relationship
|
|||
import datetime
|
||||
|
||||
from . import db
|
||||
from .photo import PhotoAttachable
|
||||
|
||||
class Inventory(db.Model):
|
||||
class Inventory(db.Model, PhotoAttachable):
|
||||
__tablename__ = 'inventory'
|
||||
__table_args__ = (
|
||||
Index('Inventory$Barcode', 'barcode'),
|
||||
|
|
|
@ -10,6 +10,7 @@ from sqlalchemy import Integer, Unicode, DateTime, func
|
|||
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
||||
|
||||
from . import db
|
||||
from .photo_links import worklog_photos
|
||||
|
||||
class Photo(db.Model):
|
||||
__tablename__ = 'photos'
|
||||
|
@ -21,7 +22,16 @@ class Photo(db.Model):
|
|||
|
||||
inventory: Mapped[Optional['Inventory']] = relationship('Inventory', back_populates='photo')
|
||||
user: Mapped[Optional['User']] = relationship('User', back_populates='photo')
|
||||
worklogs: Mapped[List['WorkLog']] = relationship('WorkLog', back_populates='photos')
|
||||
worklogs: Mapped[List['WorkLog']] = relationship('WorkLog', secondary=worklog_photos, back_populates='photos')
|
||||
|
||||
def __init__(self, filename: str, timestamp: Optional[datetime.datetime] = None, caption: Optional[str] = None):
|
||||
self.filename = filename
|
||||
self.caption = caption or ""
|
||||
self.timestamp = timestamp
|
||||
|
||||
def __repr__(self):
|
||||
return f"<Photo(id={self.id}, filename={self.filename})>"
|
||||
|
||||
class PhotoAttachable:
|
||||
def attach_photo(self, photo: 'Photo') -> None:
|
||||
raise NotImplementedError("This model doesn't know how to attach photos.")
|
||||
|
|
|
@ -9,8 +9,9 @@ from sqlalchemy import Boolean, ForeignKey, Identity, Integer, Unicode, text
|
|||
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
||||
|
||||
from . import db
|
||||
from .photo import PhotoAttachable
|
||||
|
||||
class User(db.Model):
|
||||
class User(db.Model, PhotoAttachable):
|
||||
__tablename__ = 'users'
|
||||
|
||||
id: Mapped[int] = mapped_column(Integer, Identity(start=1, increment=1), primary_key=True)
|
||||
|
|
|
@ -10,10 +10,11 @@ from sqlalchemy.orm import Mapped, mapped_column, relationship
|
|||
import datetime
|
||||
|
||||
from . import db
|
||||
from .photo import PhotoAttachable
|
||||
from .photo_links import worklog_photos
|
||||
from .work_note import WorkNote
|
||||
|
||||
class WorkLog(db.Model):
|
||||
class WorkLog(db.Model, PhotoAttachable):
|
||||
__tablename__ = 'work_log'
|
||||
|
||||
id: Mapped[int] = mapped_column(Integer, Identity(start=1, increment=1), primary_key=True)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue