Files
natureinpots_community/plugins/plant/models.py
2025-05-27 04:23:51 -05:00

55 lines
2.4 KiB
Python

from datetime import datetime
from app import db
from plugins.search.models import Tag, plant_tags
from plugins.growlog.models import PlantUpdate
class PlantCommonName(db.Model):
__tablename__ = 'plants_common'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(120), unique=True, nullable=False)
class PlantScientificName(db.Model):
__tablename__ = 'plants_scientific'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(255), unique=True, nullable=False)
class PlantLineage(db.Model):
__tablename__ = 'plant_lineage'
id = db.Column(db.Integer, primary_key=True)
parent_plant_id = db.Column(db.Integer, db.ForeignKey('plants.id'), nullable=False)
child_plant_id = db.Column(db.Integer, db.ForeignKey('plants.id'), nullable=False)
class PlantOwnershipLog(db.Model):
__tablename__ = 'plant_ownership_log'
id = db.Column(db.Integer, primary_key=True)
plant_id = db.Column(db.Integer, db.ForeignKey('plants.id'), nullable=False)
user_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False)
date_acquired = db.Column(db.DateTime, default=datetime.utcnow)
date_relinquished = db.Column(db.DateTime, nullable=True)
class Plant(db.Model):
__tablename__ = 'plants'
id = db.Column(db.Integer, primary_key=True)
common_name_id = db.Column(db.Integer, db.ForeignKey('plants_common.id'))
scientific_name_id = db.Column(db.Integer, db.ForeignKey('plants_scientific.id'))
parent_id = db.Column(db.Integer, db.ForeignKey('plants.id'), nullable=True)
is_dead = db.Column(db.Boolean, default=False)
date_added = db.Column(db.DateTime, default=datetime.utcnow)
created_by_user_id = db.Column(db.Integer, db.ForeignKey('users.id'))
# 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')
# → relationships so we can pull in the actual names:
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
)