*** Wartungsfenster jeden ersten Mittwoch vormittag im Monat ***

Skip to content
Snippets Groups Projects

Update formatscaper according to identified requirements

Merged Moser, Maximilian requested to merge mm/rework into master
+ 42
7
"""Data models for formatscaper."""
import dataclasses
from typing import List, Optional
from sqlalchemy import ForeignKey, create_engine
from sqlalchemy import ForeignKey, UniqueConstraint, create_engine
from sqlalchemy.orm import DeclarativeBase, Mapped, Session, mapped_column, relationship
@@ -28,12 +30,18 @@ class Format(ModelBase):
puid: Mapped[Optional[str]] = mapped_column(unique=True)
name: Mapped[Optional[str]] = mapped_column()
mime: Mapped[Optional[str]] = mapped_column()
endangered: Mapped[bool] = mapped_column(default=False)
# actually the probability of becoming obsolete, but that's quite a mouthful
risk: Mapped[int] = mapped_column(default=0)
results: Mapped[List["Result"]] = relationship(
back_populates="format",
cascade="all, delete-orphan",
)
comments: Mapped[List["FormatComment"]] = relationship(
back_populates="format",
cascade="all, delete-orphan",
)
def as_dict(self):
"""Dump the data as dictionary."""
@@ -41,7 +49,7 @@ class Format(ModelBase):
"puid": self.puid,
"name": self.name,
"mime": self.mime,
"endangered": self.endangered,
"risk": self.risk,
}
@classmethod
@@ -55,7 +63,24 @@ class Format(ModelBase):
def __repr__(self):
"""Return repr(self) without the list of results."""
return f"Format(puid='{self.puid}', name='{self.name}', mime='{self.mime}', endangered={self.endangered})" # noqa
return f"Format(puid='{self.puid}', name='{self.name}', mime='{self.mime}', risk={self.risk})" # noqa
@dataclasses.dataclass
class FormatComment(ModelBase):
"""Comment about a file format."""
__tablename__ = "format_comment"
id: Mapped[int] = mapped_column(primary_key=True)
comment: Mapped[str] = mapped_column()
format_id: Mapped[Optional[int]] = mapped_column(ForeignKey("format.id"))
format: Mapped[Format] = relationship(back_populates="comments")
def __repr__(self):
"""Return repr(self)."""
return f"FormatComment(comment='{self.comment}')"
@dataclasses.dataclass
@@ -67,20 +92,30 @@ class Result(ModelBase):
id: Mapped[int] = mapped_column(primary_key=True)
filename: Mapped[str] = mapped_column()
record: Mapped[Optional[str]] = mapped_column(default=None)
safe: Mapped[bool] = mapped_column(default=False)
impact: Mapped[int] = mapped_column(default=0)
# flag indicating that the result was overridden manually
overridden: Mapped[bool] = mapped_column(default=False)
format_id: Mapped[Optional[int]] = mapped_column(ForeignKey("format.id"))
format: Mapped[Format] = relationship(back_populates="results")
# filenames are unique per record
__table_args__ = (UniqueConstraint("record", "filename"),)
@property
def risk(self):
"""Calculate the risk assessment for the file."""
return self.format.risk * self.impact
def as_dict(self):
"""Dump the data as dictionary."""
result = {
"filename": self.filename,
"record": self.record,
"format": self.format.as_dict(),
"impact": self.impact,
}
if self.safe:
result["safe"] = self.safe
return result
Loading