more crap
This commit is contained in:
@ -1,32 +1,121 @@
|
||||
import click
|
||||
from flask.cli import with_appcontext
|
||||
from plugins.plant.models import Plant
|
||||
from plugins.auth.models import User
|
||||
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('preload-data')
|
||||
@click.command(name='preload-data') # 🔧 changed from preload_data
|
||||
@with_appcontext
|
||||
def preload_data():
|
||||
click.echo("Preloading data...")
|
||||
if not Plant.query.first():
|
||||
plant = Plant(name="Default Plant")
|
||||
db.session.add(plant)
|
||||
db.session.commit()
|
||||
click.echo("Default plant added.")
|
||||
else:
|
||||
click.echo("Plant data already exists.")
|
||||
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
|
||||
|
||||
@click.command('seed-admin')
|
||||
@with_appcontext
|
||||
def seed_admin():
|
||||
click.echo("Seeding admin user...")
|
||||
if not User.query.filter_by(email='admin@example.com').first():
|
||||
user = User(email='admin@example.com', role='admin', is_verified=True)
|
||||
user.set_password('password') # Make sure this method exists in your model
|
||||
db.session.add(user)
|
||||
db.session.commit()
|
||||
click.echo("✅ Admin user created.")
|
||||
else:
|
||||
click.echo("ℹ️ Admin user already exists.")
|
||||
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()
|
||||
|
||||
# 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.")
|
||||
|
Reference in New Issue
Block a user