switched to tagging and added views to support it, in progress for other tagging functions.

This commit is contained in:
Bryan Van Deusen
2025-08-14 21:36:27 -04:00
parent e9793ba38c
commit 3ff34ec9c2
12 changed files with 943 additions and 349 deletions
+30 -12
View File
@@ -5,10 +5,18 @@ from datetime import datetime
# tag to object relationship table
image_tags = db.Table(
'image_tags',
db.Column('image_id', db.Integer, db.ForeignKey('image_record.id'), primary_key=True),
db.Column('tag_id', db.Integer, db.ForeignKey('tag.id'), primary_key=True),
db.UniqueConstraint('image_id', 'tag_id', name='uq_image_tag') # prevent duplicate tags per image
"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 User(UserMixin, db.Model):
@@ -19,13 +27,14 @@ class User(UserMixin, db.Model):
is_admin = db.Column(db.Boolean, default=False)
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(16), nullable=True) # hex of 64-bit hash
artist = db.Column(db.String(255), nullable=True)
# artist = db.Column(db.String(255), nullable=True)
file_size = db.Column(db.Integer, nullable=True)
width = db.Column(db.Integer, nullable=True)
height = db.Column(db.Integer, nullable=True)
@@ -34,22 +43,31 @@ class ImageRecord(db.Model):
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')
tags = db.relationship(
"Tag",
secondary=image_tags,
back_populates="images",
passive_deletes=True, # <-- let DB handle deletes
)
class Tag(db.Model):
# __tablename__ = "tag" (implicit)
id = db.Column(db.Integer, primary_key=True)
# Tag names are unique so we can upsert quickly (e.g. "zip:Artist/ArchiveName.zip")
name = db.Column(db.String(255), unique=True, nullable=False)
# Optional “kind” if you want to filter by tag type later ("zip", "import-source", etc.)
kind = db.Column(db.String(64), nullable=True)
images = db.relationship('ImageRecord', secondary=image_tags, back_populates='tags')
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) # full path or logical name
file_size = db.Column(db.BigInteger, nullable=False)
@@ -60,8 +78,8 @@ class ArchiveRecord(db.Model):
artist = db.Column(db.String(255), nullable=True)
# Link to the tag we created for this archive so we can reuse it if the same archive returns
tag_id = db.Column(db.Integer, db.ForeignKey('tag.id'), nullable=False)
tag = db.relationship('Tag', backref=db.backref('archives', lazy='dynamic'))
tag_id = db.Column(db.Integer, db.ForeignKey("tag.id", ondelete="SET NULL"), nullable=True)
tag = db.relationship("Tag")
@login_manager.user_loader
def load_user(user_id):