bunch of changes

This commit is contained in:
2025-06-04 23:24:16 -05:00
parent 98c868113c
commit 6cf2fdec61
41 changed files with 1580 additions and 286 deletions

View File

@ -1,96 +1,129 @@
from flask_sqlalchemy import SQLAlchemy
import uuid as uuid_lib
# plugins/plant/models.py
from datetime import datetime
import uuid as uuid_lib
# Import the central SQLAlchemy instance, not a new one
from app import db
# Association table for tags
# If your User model lives in plugins/auth/models.py, import it here:
from plugins.auth.models import User
# -----------------------------
# (We no longer need PlantLineage)
# -----------------------------
# Association table for tags (unchanged)
plant_tags = db.Table(
'plant_tags',
db.metadata,
db.Column('plant_id', db.Integer, db.ForeignKey('plant.id'), primary_key=True),
db.Column('tag_id', db.Integer, db.ForeignKey('tag.id'), primary_key=True),
db.Column('tag_id', db.Integer, db.ForeignKey('tag.id'), primary_key=True),
extend_existing=True
)
class Tag(db.Model):
__tablename__ = 'tag'
__table_args__ = {'extend_existing': True}
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(255), unique=True, nullable=False)
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'
__table_args__ = {'extend_existing': True}
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(255), unique=True, nullable=False)
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(128), unique=True, nullable=False)
created_at = db.Column(db.DateTime, default=datetime.utcnow)
scientific_names = db.relationship(
'plugins.plant.models.PlantScientificName',
backref=db.backref('common', lazy='joined'),
lazy=True,
cascade='all, delete-orphan'
)
class PlantScientificName(db.Model):
__tablename__ = 'plant_scientific_name'
__table_args__ = {'extend_existing': True}
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(255), unique=True, nullable=False)
common_id = db.Column(db.Integer, db.ForeignKey('plant_common_name.id'), nullable=False)
class PlantLineage(db.Model):
__tablename__ = 'plant_lineage'
__table_args__ = {'extend_existing': True}
id = db.Column(db.Integer, primary_key=True)
child_plant_id = db.Column(db.Integer, db.ForeignKey('plant.id'), nullable=False)
parent_plant_id = db.Column(db.Integer, db.ForeignKey('plant.id'), nullable=False)
type = db.Column(db.String(50), nullable=False) # cutting, seed, division
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(256), unique=True, nullable=False)
common_id = db.Column(db.Integer, db.ForeignKey('plant_common_name.id'), nullable=False)
created_at = db.Column(db.DateTime, default=datetime.utcnow)
plants = db.relationship(
'plugins.plant.models.Plant',
backref='scientific',
lazy='dynamic'
)
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)
start_time = db.Column(db.DateTime, nullable=False)
end_time = db.Column(db.DateTime, nullable=True)
transfer_note = db.Column(db.Text, 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)
graph_node_id = db.Column(db.String(255), nullable=True) # optional
is_verified = db.Column(db.Boolean, default=False, nullable=False)
user = db.relationship('plugins.auth.models.User', backref='ownership_logs', 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)
status = db.Column(db.String(50), nullable=False, default='active')
notes = db.Column(db.Text, nullable=True)
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)
created_at = db.Column(db.DateTime, default=datetime.utcnow)
transferred = db.Column(db.Boolean, default=False)
graph_node_id = db.Column(db.String(255), nullable=True)
is_verified = db.Column(db.Boolean, nullable=False, default=False)
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'
)
# Relationships
updates = db.relationship('PlantUpdate', backref='growlog', lazy=True)
lineage = db.relationship('PlantLineage', backref='child', lazy=True, foreign_keys='PlantLineage.child_plant_id')
tags = db.relationship('Tag', secondary=plant_tags, backref='plants')
common_name = db.relationship(
'PlantCommonName',
backref=db.backref('plants', lazy='dynamic'),
lazy=True
)
common_name = db.relationship(
'plugins.plant.models.PlantCommonName',
backref=db.backref('plants', lazy='dynamic'),
lazy=True
)
scientific_name = db.relationship(
'PlantScientificName',
backref=db.backref('plants', lazy='dynamic'),
lazy=True
)
PlantCommon = PlantCommonName
PlantScientific = PlantScientificName
'plugins.plant.models.PlantScientificName',
backref=db.backref('plants', lazy='dynamic'),
lazy=True
)
ownership_logs = db.relationship(
'plugins.plant.models.PlantOwnershipLog',
backref='plant',
lazy=True,
cascade='all, delete-orphan'
)
def __repr__(self):
return f"<Plant {self.uuid} ({self.plant_type})>"