a ton of fun happened, refactored alot
This commit is contained in:
@ -1,4 +1,5 @@
|
||||
# plugins/media/models.py
|
||||
|
||||
from datetime import datetime
|
||||
from flask import url_for
|
||||
from app import db
|
||||
@ -17,45 +18,40 @@ class Media(db.Model):
|
||||
plant_id = db.Column(db.Integer, db.ForeignKey("plant.id"), nullable=True)
|
||||
growlog_id = db.Column(db.Integer, db.ForeignKey("grow_logs.id"), nullable=True)
|
||||
created_at = db.Column(db.DateTime, default=datetime.utcnow, nullable=False)
|
||||
|
||||
# You already have a file_url column in your DB
|
||||
file_url = db.Column(db.String(512), nullable=False)
|
||||
|
||||
hearts = db.relationship(
|
||||
"plugins.media.models.ImageHeart",
|
||||
"ImageHeart",
|
||||
backref="media",
|
||||
lazy="dynamic",
|
||||
cascade="all, delete-orphan",
|
||||
)
|
||||
featured_entries = db.relationship(
|
||||
"plugins.media.models.FeaturedImage",
|
||||
"FeaturedImage",
|
||||
backref="media",
|
||||
lazy="dynamic",
|
||||
cascade="all, delete-orphan",
|
||||
)
|
||||
|
||||
# ↔ Media items attached to a Plant
|
||||
plant = db.relationship(
|
||||
"plugins.plant.models.Plant",
|
||||
"Plant",
|
||||
back_populates="media_items",
|
||||
foreign_keys=[plant_id],
|
||||
lazy="joined",
|
||||
)
|
||||
|
||||
# ↔ Media items attached to a GrowLog
|
||||
growlog = db.relationship(
|
||||
"plugins.growlog.models.GrowLog",
|
||||
"GrowLog",
|
||||
back_populates="media_items",
|
||||
foreign_keys=[growlog_id],
|
||||
lazy="joined",
|
||||
)
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
"""
|
||||
Infer plugin & related_id from whichever FK is set,
|
||||
and build the file_url path immediately so that INSERT
|
||||
never tries to write plugin=None or related_id=None.
|
||||
"""
|
||||
super().__init__(*args, **kwargs)
|
||||
|
||||
# If they passed plant_id or growlog_id in kwargs, pick one:
|
||||
if self.plant_id:
|
||||
self.plugin = "plant"
|
||||
self.related_id = self.plant_id
|
||||
@ -63,7 +59,6 @@ class Media(db.Model):
|
||||
self.plugin = "growlog"
|
||||
self.related_id = self.growlog_id
|
||||
else:
|
||||
# fallback (you might choose to raise instead)
|
||||
self.plugin = kwargs.get("plugin", "")
|
||||
self.related_id = kwargs.get("related_id", 0)
|
||||
|
||||
@ -81,6 +76,16 @@ class Media(db.Model):
|
||||
)
|
||||
|
||||
|
||||
class ZipJob(db.Model):
|
||||
__tablename__ = 'zip_jobs'
|
||||
id = db.Column(db.Integer, primary_key=True)
|
||||
user_id = db.Column(db.Integer, nullable=False)
|
||||
filename = db.Column(db.String(255), nullable=False)
|
||||
created_at = db.Column(db.DateTime, default=datetime.utcnow)
|
||||
status = db.Column(db.String(20), default='queued') # queued|processing|done|failed
|
||||
error = db.Column(db.Text, nullable=True)
|
||||
|
||||
|
||||
class ImageHeart(db.Model):
|
||||
__tablename__ = "image_hearts"
|
||||
__table_args__ = {"extend_existing": True}
|
||||
|
Reference in New Issue
Block a user