Refactor Photo model constructor to remove timestamp parameter; update helper function to use getattr for safer attribute access.

This commit is contained in:
Yaro Kasear 2025-07-11 09:34:04 -05:00
parent e1cb99f2d1
commit 3e0faae851
3 changed files with 4 additions and 3 deletions

View file

@ -24,10 +24,9 @@ class Photo(db.Model):
user: Mapped[Optional['User']] = relationship('User', back_populates='photo')
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):
def __init__(self, filename: str, 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})>"

View file

@ -82,6 +82,6 @@ def generate_hashed_filename(file_storage, model_name: str) -> str:
def get_photo_attachable_class_by_name(name: str):
for cls in PhotoAttachable.__subclasses__():
if cls.__tablename__ == name:
if getattr(cls, '__tablename__', None) == name:
return cls
return None

View file

@ -14,6 +14,8 @@ def save_photo(file, model: str) -> str:
rel_path = generate_hashed_filename(file, model)
# Absolute path to save
assert current_app.static_folder
abs_path = os.path.join(current_app.static_folder, rel_path)
os.makedirs(os.path.dirname(abs_path), exist_ok=True)