80 lines
2.6 KiB
Python
80 lines
2.6 KiB
Python
import click
|
|
from flask.cli import with_appcontext
|
|
from datetime import datetime
|
|
from app import db
|
|
from app.models.user import (
|
|
User, Plant, PlantCommonName, PlantScientificName, Submission, SubmissionImage
|
|
)
|
|
|
|
@click.command("preload-data")
|
|
@with_appcontext
|
|
def preload_data():
|
|
click.echo("[📦] Preloading demo data...")
|
|
|
|
if not User.query.filter_by(email="demo@example.com").first():
|
|
demo_user = User(
|
|
email="demo@example.com",
|
|
password_hash="fakehash123", # you can hash a real one if needed
|
|
role="user",
|
|
is_verified=True
|
|
)
|
|
db.session.add(demo_user)
|
|
db.session.commit()
|
|
|
|
common_entries = {
|
|
"Monstera Albo": "Monstera deliciosa 'Albo-Variegata'",
|
|
"Philodendron Pink Princess": "Philodendron erubescens",
|
|
"Cebu Blue Pothos": "Epipremnum pinnatum"
|
|
}
|
|
|
|
for common_name, sci_name in common_entries.items():
|
|
common = PlantCommonName.query.filter_by(name=common_name).first()
|
|
if not common:
|
|
common = PlantCommonName(name=common_name)
|
|
db.session.add(common)
|
|
|
|
scientific = PlantScientificName.query.filter_by(name=sci_name).first()
|
|
if not scientific:
|
|
scientific = PlantScientificName(name=sci_name)
|
|
db.session.add(scientific)
|
|
|
|
db.session.commit()
|
|
|
|
common_map = {c.name: c.id for c in PlantCommonName.query.all()}
|
|
scientific_map = {s.name: s.id for s in PlantScientificName.query.all()}
|
|
demo_user = User.query.filter_by(email="demo@example.com").first()
|
|
|
|
plants = []
|
|
for i in range(1, 4):
|
|
plant = Plant(
|
|
common_name_id=common_map["Monstera Albo"],
|
|
scientific_name_id=scientific_map["Monstera deliciosa 'Albo-Variegata'"],
|
|
created_by_user_id=demo_user.id
|
|
)
|
|
db.session.add(plant)
|
|
plants.append(plant)
|
|
|
|
db.session.commit()
|
|
|
|
for plant in plants:
|
|
for i in range(2):
|
|
submission = Submission(
|
|
user_id=demo_user.id,
|
|
plant_id=plant.id,
|
|
common_name="Monstera Albo",
|
|
scientific_name="Monstera deliciosa 'Albo-Variegata'",
|
|
price=85.0 + i * 15,
|
|
source="Etsy",
|
|
height=12 + i * 2,
|
|
width=10 + i,
|
|
potting_mix="Pumice:Bark:Coco 2:1:1",
|
|
container_size="4 inch",
|
|
health_status="Healthy",
|
|
notes="Good variegation",
|
|
timestamp=datetime.utcnow()
|
|
)
|
|
db.session.add(submission)
|
|
|
|
db.session.commit()
|
|
click.echo("[✅] Demo data loaded.")
|