sort of working, more changes

This commit is contained in:
2025-06-09 05:45:58 -05:00
parent d442cad0bb
commit f0b1abd622
102 changed files with 1448 additions and 2264 deletions

View File

@ -3,8 +3,7 @@
from datetime import datetime
import uuid as uuid_lib
from app import db
# from plugins.auth.models import User
from plugins.media.models import Media # import Media so we can refer to Media.plant_id
# Association table for Plant ↔ Tag (unchanged)
plant_tags = db.Table(
@ -21,7 +20,7 @@ class Tag(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(128), unique=True, nullable=False)
# … any other columns you had …
class PlantCommonName(db.Model):
__tablename__ = 'plant_common_name'
@ -38,6 +37,7 @@ class PlantCommonName(db.Model):
cascade='all, delete-orphan'
)
class PlantScientificName(db.Model):
__tablename__ = 'plant_scientific_name'
__table_args__ = {'extend_existing': True}
@ -47,22 +47,17 @@ class PlantScientificName(db.Model):
common_id = db.Column(db.Integer, db.ForeignKey('plant_common_name.id'), nullable=False)
created_at = db.Column(db.DateTime, default=datetime.utcnow)
# We removed the “plants” relationship from here to avoid backref conflicts.
# If you need it, you can still do Plant.query.filter_by(scientific_id=<this id>).
class PlantOwnershipLog(db.Model):
__tablename__ = 'plant_ownership_log'
__table_args__ = {'extend_existing': True}
id = db.Column(db.Integer, primary_key=True)
plant_id = db.Column(db.Integer, db.ForeignKey('plant.id'), nullable=False)
user_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False)
date_acquired = db.Column(db.DateTime, default=datetime.utcnow)
transferred = db.Column(db.Boolean, default=False, nullable=False)
is_verified = db.Column(db.Boolean, default=False, nullable=False)
# Optional: if you ever want to store a pointer to the Neo4j node, you can re-add:
# graph_node_id = db.Column(db.String(255), nullable=True)
id = db.Column(db.Integer, primary_key=True)
plant_id = db.Column(db.Integer, db.ForeignKey('plant.id'), nullable=False)
user_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False)
date_acquired = db.Column(db.DateTime, default=datetime.utcnow)
transferred = db.Column(db.Boolean, default=False, nullable=False)
is_verified = db.Column(db.Boolean, default=False, nullable=False)
user = db.relationship(
'plugins.auth.models.User',
@ -70,56 +65,73 @@ class PlantOwnershipLog(db.Model):
lazy=True
)
class Plant(db.Model):
__tablename__ = 'plant'
__table_args__ = {'extend_existing': True}
id = db.Column(db.Integer, primary_key=True)
uuid = db.Column(db.String(36), default=lambda: str(uuid_lib.uuid4()), unique=True, nullable=False)
custom_slug = db.Column(db.String(255), unique=True, nullable=True)
id = db.Column(db.Integer, primary_key=True)
uuid = db.Column(db.String(36), default=lambda: str(uuid_lib.uuid4()), unique=True, nullable=False)
custom_slug = db.Column(db.String(255), unique=True, nullable=True)
owner_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False)
common_id = db.Column(db.Integer, db.ForeignKey('plant_common_name.id'), nullable=False)
scientific_id = db.Column(db.Integer, db.ForeignKey('plant_scientific_name.id'), nullable=False)
owner_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False)
common_id = db.Column(db.Integer, db.ForeignKey('plant_common_name.id'), nullable=False)
scientific_id = db.Column(db.Integer, db.ForeignKey('plant_scientific_name.id'), nullable=False)
plant_type = db.Column(db.String(50), nullable=False)
created_at = db.Column(db.DateTime, default=datetime.utcnow)
updated_at = db.Column(db.DateTime, onupdate=datetime.utcnow)
mother_uuid = db.Column(db.String(36), db.ForeignKey('plant.uuid'), nullable=True)
# ─── NEW: Flag that indicates whether the common/scientific name pair was human-verified ─────────────────
data_verified = db.Column(db.Boolean, default=False, nullable=False)
plant_type = db.Column(db.String(50), nullable=False)
notes = db.Column(db.Text, nullable=True)
is_active = db.Column(db.Boolean, default=True, nullable=False)
featured_media_id = db.Column(db.Integer, db.ForeignKey('media.id'), nullable=True)
# Relationships
updates = db.relationship(
'plugins.growlog.models.PlantUpdate',
backref='plant',
lazy=True,
cascade='all, delete-orphan'
)
tags = db.relationship(
'plugins.plant.models.Tag',
secondary=plant_tags,
backref='plants',
lazy='dynamic'
)
created_at = db.Column(db.DateTime, default=datetime.utcnow)
updated_at = db.Column(db.DateTime, onupdate=datetime.utcnow)
data_verified = db.Column(db.Boolean, default=False, nullable=False)
common_name = db.relationship(
'plugins.plant.models.PlantCommonName',
backref=db.backref('plants', lazy='dynamic'),
lazy=True
)
scientific_name = db.relationship(
'plugins.plant.models.PlantScientificName',
backref=db.backref('plants', lazy='dynamic'),
lazy=True
)
# ─── FIXED: explicitly join on Media.plant_id ──────────────────────────────
media = db.relationship(
Media,
backref='plant',
lazy=True,
cascade='all, delete-orphan',
foreign_keys=[Media.plant_id]
)
ownership_logs = db.relationship(
'plugins.plant.models.PlantOwnershipLog',
backref='plant',
lazy=True,
cascade='all, delete-orphan'
)
featured_media = db.relationship(
Media,
foreign_keys=[featured_media_id],
uselist=False
)
updates = db.relationship(
'plugins.growlog.models.PlantUpdate',
backref='plant',
lazy=True,
cascade='all, delete-orphan'
)
tags = db.relationship(
Tag,
secondary=plant_tags,
backref='plants',
lazy='dynamic'
)
common_name = db.relationship(
PlantCommonName,
backref=db.backref('plants', lazy='dynamic'),
lazy=True
)
scientific_name = db.relationship(
PlantScientificName,
backref=db.backref('plants', lazy='dynamic'),
lazy=True
)
ownership_logs = db.relationship(
PlantOwnershipLog,
backref='plant',
lazy=True,
cascade='all, delete-orphan'
)
def __repr__(self):
return f"<Plant {self.uuid} ({self.plant_type})>"