Refactor Photo model constructor to remove timestamp parameter; update helper function to use getattr for safer attribute access.
This commit is contained in:
parent
e1cb99f2d1
commit
3e0faae851
3 changed files with 4 additions and 3 deletions
|
@ -24,10 +24,9 @@ class Photo(db.Model):
|
||||||
user: Mapped[Optional['User']] = relationship('User', back_populates='photo')
|
user: Mapped[Optional['User']] = relationship('User', back_populates='photo')
|
||||||
worklogs: Mapped[List['WorkLog']] = relationship('WorkLog', secondary=worklog_photos, 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):
|
def __init__(self, filename: str, caption: Optional[str] = None):
|
||||||
self.filename = filename
|
self.filename = filename
|
||||||
self.caption = caption or ""
|
self.caption = caption or ""
|
||||||
self.timestamp = timestamp
|
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return f"<Photo(id={self.id}, filename={self.filename})>"
|
return f"<Photo(id={self.id}, filename={self.filename})>"
|
||||||
|
|
|
@ -82,6 +82,6 @@ def generate_hashed_filename(file_storage, model_name: str) -> str:
|
||||||
|
|
||||||
def get_photo_attachable_class_by_name(name: str):
|
def get_photo_attachable_class_by_name(name: str):
|
||||||
for cls in PhotoAttachable.__subclasses__():
|
for cls in PhotoAttachable.__subclasses__():
|
||||||
if cls.__tablename__ == name:
|
if getattr(cls, '__tablename__', None) == name:
|
||||||
return cls
|
return cls
|
||||||
return None
|
return None
|
||||||
|
|
|
@ -14,6 +14,8 @@ def save_photo(file, model: str) -> str:
|
||||||
rel_path = generate_hashed_filename(file, model)
|
rel_path = generate_hashed_filename(file, model)
|
||||||
|
|
||||||
# Absolute path to save
|
# Absolute path to save
|
||||||
|
assert current_app.static_folder
|
||||||
|
|
||||||
abs_path = os.path.join(current_app.static_folder, rel_path)
|
abs_path = os.path.join(current_app.static_folder, rel_path)
|
||||||
os.makedirs(os.path.dirname(abs_path), exist_ok=True)
|
os.makedirs(os.path.dirname(abs_path), exist_ok=True)
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue