a ton of fun happened, refactored alot
This commit is contained in:
@ -1,8 +1,10 @@
|
||||
# plugins/plant/models.py
|
||||
|
||||
from datetime import datetime
|
||||
import uuid as uuid_lib
|
||||
|
||||
import string, random # for generate_short_id
|
||||
from app import db
|
||||
from plugins.plant.growlog.models import GrowLog
|
||||
|
||||
# Association table for Plant ↔ Tag
|
||||
plant_tags = db.Table(
|
||||
@ -82,7 +84,7 @@ class Plant(db.Model):
|
||||
|
||||
plant_type = db.Column(db.String(50), nullable=False)
|
||||
notes = db.Column(db.Text, nullable=True)
|
||||
short_id = db.Column(db.String(8), unique=True, nullable=True, index=True)
|
||||
short_id = db.Column(db.String(8), unique=True, nullable=True, index=True)
|
||||
|
||||
vendor_name = db.Column(db.String(255), nullable=True)
|
||||
price = db.Column(db.Numeric(10, 2), nullable=True)
|
||||
@ -102,14 +104,14 @@ class Plant(db.Model):
|
||||
media_items = db.relationship(
|
||||
'plugins.media.models.Media',
|
||||
back_populates='plant',
|
||||
lazy='select', # ← this is the fix
|
||||
lazy='select',
|
||||
cascade='all, delete-orphan',
|
||||
foreign_keys='plugins.media.models.Media.plant_id'
|
||||
)
|
||||
|
||||
@property
|
||||
def media(self):
|
||||
return self.media_items # already a list when lazy='select'
|
||||
return self.media_items
|
||||
|
||||
# the one you see on the detail page
|
||||
featured_media = db.relationship(
|
||||
@ -120,7 +122,7 @@ class Plant(db.Model):
|
||||
|
||||
# ↔ GrowLog instances for this plant
|
||||
updates = db.relationship(
|
||||
'plugins.growlog.models.GrowLog',
|
||||
GrowLog,
|
||||
backref='plant',
|
||||
lazy=True,
|
||||
cascade='all, delete-orphan'
|
||||
@ -152,7 +154,7 @@ class Plant(db.Model):
|
||||
|
||||
def __repr__(self):
|
||||
return f"<Plant {self.uuid} ({self.plant_type})>"
|
||||
|
||||
|
||||
@classmethod
|
||||
def generate_short_id(cls, length: int = 6) -> str:
|
||||
"""
|
||||
@ -162,6 +164,5 @@ class Plant(db.Model):
|
||||
alphabet = string.ascii_lowercase + string.digits
|
||||
while True:
|
||||
candidate = ''.join(random.choices(alphabet, k=length))
|
||||
# Check uniqueness
|
||||
if not cls.query.filter_by(short_id=candidate).first():
|
||||
return candidate
|
||||
|
Reference in New Issue
Block a user