More files
This commit is contained in:
@ -1,2 +1,10 @@
|
||||
from .seed import seed_admin
|
||||
from .preload import preload_data
|
||||
from .seed import preload_data, seed_admin
|
||||
|
||||
cli_commands = [
|
||||
preload_data,
|
||||
seed_admin
|
||||
]
|
||||
|
||||
def register_cli(app):
|
||||
for command in cli_commands:
|
||||
app.cli.add_command(command)
|
||||
|
@ -1,79 +0,0 @@
|
||||
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.")
|
@ -1,20 +0,0 @@
|
||||
# plugins/cli/routes.py
|
||||
|
||||
import click
|
||||
from flask.cli import with_appcontext
|
||||
from app.extensions import db
|
||||
from plugins.plant.models import Plant
|
||||
|
||||
@click.command('preload-data')
|
||||
@with_appcontext
|
||||
def preload_data():
|
||||
"""Preloads plant data into the database."""
|
||||
if not Plant.query.first():
|
||||
db.session.add(Plant(name="Example Plant"))
|
||||
db.session.commit()
|
||||
click.echo("✅ Preloaded sample plant.")
|
||||
else:
|
||||
click.echo("ℹ️ Plant data already exists.")
|
||||
|
||||
# Export command(s) so __init__.py can register them
|
||||
cli_commands = [preload_data]
|
@ -1,26 +1,32 @@
|
||||
import click
|
||||
from flask.cli import with_appcontext
|
||||
from werkzeug.security import generate_password_hash
|
||||
from ..core.models import User
|
||||
from .. import db
|
||||
from plugins.plant.models import Plant
|
||||
from plugins.auth.models import User
|
||||
from app import db
|
||||
|
||||
@click.command("seed-admin")
|
||||
|
||||
@click.command('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.")
|
||||
|
||||
|
||||
@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}")
|
||||
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.")
|
||||
|
Reference in New Issue
Block a user