138 lines
4.4 KiB
Python
138 lines
4.4 KiB
Python
import click
|
||
from flask.cli import with_appcontext
|
||
from datetime import datetime, timedelta
|
||
|
||
from app import db
|
||
from plugins.auth.models import User
|
||
from plugins.plant.models import (
|
||
Plant, PlantCommonName, PlantScientificName, PlantOwnershipLog,
|
||
PlantLineage
|
||
)
|
||
from plugins.growlog.models import PlantUpdate
|
||
from plugins.media.models import Media, ImageHeart, FeaturedImage
|
||
from plugins.submission.models import Submission, SubmissionImage
|
||
|
||
|
||
@click.command(name='preload-data') # 🔧 changed from preload_data
|
||
@with_appcontext
|
||
def preload_data_cli():
|
||
preload_data(auto=False)
|
||
|
||
def preload_data(auto=False):
|
||
if User.query.filter_by(email='admin@example.com').first():
|
||
if not auto:
|
||
click.echo("⚠️ Demo data already exists, skipping.")
|
||
return
|
||
|
||
if not auto:
|
||
click.echo("🌱 Seeding demo data...")
|
||
|
||
# USERS
|
||
admin = User(email='admin@example.com', role='admin', is_verified=True)
|
||
admin.set_password('password123')
|
||
|
||
user = User(email='user@example.com', role='user', is_verified=True)
|
||
user.set_password('password123')
|
||
|
||
db.session.add_all([admin, user])
|
||
db.session.commit()
|
||
|
||
# COMMON & SCIENTIFIC NAMES
|
||
#monstera_common = PlantCommonName(name='Monstera')
|
||
#deliciosa_sci = PlantScientificName(name='Monstera deliciosa')
|
||
#aurea_sci = PlantScientificName(name='Monstera aurea')
|
||
#db.session.add_all([monstera_common, deliciosa_sci, aurea_sci])
|
||
#db.session.commit()
|
||
|
||
joepii_common = PlantCommonName(name='Philodendron Joepii')
|
||
queen_common = PlantCommonName(name='Anthurium Warocqueanum Queen')
|
||
thai_common = PlantCommonName(name='Thai Constellation Monstera')
|
||
generic_common = PlantCommonName(name='Monstera')
|
||
|
||
db.session.add_all([joepii_common, queen_common, thai_common, generic_common])
|
||
db.session.flush() # ensures all common_name IDs are available
|
||
|
||
joepii_sci = PlantScientificName(name='Philodendron × joepii', common_id=joepii_common.id)
|
||
queen_sci = PlantScientificName(name='Anthurium warocqueanum', common_id=queen_common.id)
|
||
thai_sci = PlantScientificName(name="Monstera deliciosa 'Thai Constellation'", common_id=thai_common.id)
|
||
generic_sci = PlantScientificName(name='Monstera deliciosa', common_id=generic_common.id)
|
||
|
||
db.session.add_all([joepii_sci, queen_sci, thai_sci, generic_sci])
|
||
db.session.commit()
|
||
|
||
# PLANTS
|
||
""" parent_plant = Plant(
|
||
common_name_id=monstera_common.id,
|
||
scientific_name_id=deliciosa_sci.id,
|
||
created_by_user_id=admin.id
|
||
)
|
||
child_plant = Plant(
|
||
common_name_id=monstera_common.id,
|
||
scientific_name_id=aurea_sci.id,
|
||
created_by_user_id=user.id,
|
||
parent_id=1
|
||
)
|
||
db.session.add_all([parent_plant, child_plant])
|
||
db.session.flush()
|
||
|
||
# LINEAGE & OWNERSHIP
|
||
db.session.add(PlantLineage(parent_plant_id=parent_plant.id, child_plant_id=child_plant.id))
|
||
db.session.add(PlantOwnershipLog(
|
||
plant_id=child_plant.id,
|
||
user_id=user.id,
|
||
date_acquired=datetime.utcnow() - timedelta(days=20)
|
||
))
|
||
db.session.commit()
|
||
|
||
# UPDATE & MEDIA
|
||
update = PlantUpdate(
|
||
plant_id=child_plant.id,
|
||
update_type='Repotted',
|
||
description='Moved to a 6" pot with a new moss pole.',
|
||
)
|
||
db.session.add(update)
|
||
db.session.flush()
|
||
|
||
db.session.add(Media(
|
||
file_url='uploads/demo_plant_update.jpg',
|
||
update_id=update.id,
|
||
caption='Freshly repotted.'
|
||
))
|
||
db.session.commit()
|
||
|
||
# SUBMISSION & IMAGE
|
||
submission = Submission(
|
||
user_id=user.id,
|
||
plant_id=child_plant.id,
|
||
common_name='Monstera',
|
||
scientific_name='Monstera aurea',
|
||
price=120.00,
|
||
source='Etsy',
|
||
height=45,
|
||
width=30,
|
||
leaf_count=5,
|
||
potting_mix='2:1:1 bark:pumice:coco',
|
||
container_size='6"',
|
||
health_status='Healthy',
|
||
notes='Some minor yellowing on one leaf.'
|
||
)
|
||
db.session.add(submission)
|
||
db.session.flush()
|
||
|
||
image = SubmissionImage(
|
||
submission_id=submission.id,
|
||
file_path='uploads/demo_submission.jpg',
|
||
is_visible=True
|
||
)
|
||
db.session.add(image)
|
||
db.session.flush()
|
||
|
||
db.session.add_all([
|
||
ImageHeart(user_id=admin.id, submission_image_id=image.id),
|
||
FeaturedImage(submission_image_id=image.id, override_text='Gorgeous coloration', is_featured=True)
|
||
])
|
||
db.session.commit() """
|
||
|
||
if not auto:
|
||
click.echo("🎉 Demo data seeded successfully.")
|