This repository has been archived on 2026-05-31. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
imagerepo/app/models.py
T

79 lines
2.9 KiB
Python

from . import db
# tag to object relationship table
image_tags = db.Table(
"image_tags",
db.Column(
"image_id", db.Integer,
db.ForeignKey("image_record.id", ondelete="CASCADE"),
primary_key=True,
),
db.Column(
"tag_id", db.Integer,
db.ForeignKey("tag.id", ondelete="CASCADE"),
primary_key=True,
),
db.UniqueConstraint("image_id", "tag_id", name="uq_image_tag"),
)
class ImageRecord(db.Model):
__tablename__ = "image_record"
id = db.Column(db.Integer, primary_key=True)
filename = db.Column(db.String(255), nullable=False)
filepath = db.Column(db.String(512), nullable=False)
thumb_path = db.Column(db.String(512))
hash = db.Column(db.String(64), unique=True, nullable=False) # SHA256
perceptual_hash = db.Column(db.String(64), nullable=True) # hex of 256-bit hash
file_size = db.Column(db.Integer, nullable=True)
width = db.Column(db.Integer, nullable=True)
height = db.Column(db.Integer, nullable=True)
format = db.Column(db.String(10), nullable=True)
camera_model = db.Column(db.String(255), nullable=True)
taken_at = db.Column(db.DateTime, nullable=True)
imported_at = db.Column(db.DateTime, default=db.func.now())
is_thumbnail = db.Column(db.Boolean, default=False)
tags = db.relationship(
"Tag",
secondary=image_tags,
back_populates="images",
passive_deletes=True,
)
class Tag(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(255), unique=True, nullable=False)
kind = db.Column(db.String(64), nullable=True)
images = db.relationship(
"ImageRecord",
secondary=image_tags,
back_populates="tags",
passive_deletes=True,
)
class ArchiveRecord(db.Model):
"""
Tracks archives we've processed so we can skip re-importing the same large file.
Also holds a Tag that marks images that came from this archive.
"""
__tablename__ = "archive_record"
id = db.Column(db.Integer, primary_key=True)
filename = db.Column(db.String(512), nullable=False)
file_size = db.Column(db.BigInteger, nullable=False)
hash = db.Column(db.String(64), unique=True, nullable=False) # SHA256 of the archive file
imported_at = db.Column(db.DateTime, default=db.func.now())
artist = db.Column(db.String(255), nullable=True)
tag_id = db.Column(db.Integer, db.ForeignKey("tag.id", ondelete="SET NULL"), nullable=True)
tag = db.relationship("Tag")
class AppSettings(db.Model):
"""
Key-value store for application settings and version tracking.
Used to persist configuration and trigger data migrations on version changes.
"""
__tablename__ = "app_settings"
id = db.Column(db.Integer, primary_key=True)
key = db.Column(db.String(64), unique=True, nullable=False)
value = db.Column(db.Text, nullable=True)
updated_at = db.Column(db.DateTime, default=db.func.now(), onupdate=db.func.now())