changes
This commit is contained in:
2
plugins/cli/__init__.py
Normal file
2
plugins/cli/__init__.py
Normal file
@ -0,0 +1,2 @@
|
||||
from .seed import seed_admin
|
||||
from .preload import preload_data
|
79
plugins/cli/preload.py
Normal file
79
plugins/cli/preload.py
Normal file
@ -0,0 +1,79 @@
|
||||
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.")
|
26
plugins/cli/seed.py
Normal file
26
plugins/cli/seed.py
Normal file
@ -0,0 +1,26 @@
|
||||
import click
|
||||
from flask.cli import with_appcontext
|
||||
from werkzeug.security import generate_password_hash
|
||||
from ..core.models import User
|
||||
from .. import db
|
||||
|
||||
@click.command("seed-admin")
|
||||
@with_appcontext
|
||||
def seed_admin():
|
||||
"""Seed a default admin user if none exists."""
|
||||
admin_email = "admin@example.com"
|
||||
admin_password = "admin123"
|
||||
|
||||
if User.query.filter_by(email=admin_email).first():
|
||||
click.echo("[ℹ] Admin user already exists.")
|
||||
return
|
||||
|
||||
user = User(
|
||||
email=admin_email,
|
||||
password_hash=generate_password_hash(admin_password),
|
||||
role="admin",
|
||||
is_verified=True
|
||||
)
|
||||
db.session.add(user)
|
||||
db.session.commit()
|
||||
click.echo(f"[✔] Created default admin: {admin_email}")
|
Reference in New Issue
Block a user