71 lines
1.9 KiB
Python
71 lines
1.9 KiB
Python
from app.db.database import engine, Base, SessionLocal
|
|
from app.models import User, Project, Specialty, Contractor, Activity, NonConformity, Evidence, UserRole
|
|
from app.security import get_password_hash # Import hashing function
|
|
import datetime
|
|
|
|
def init_db():
|
|
print("Creating tables...")
|
|
Base.metadata.create_all(bind=engine)
|
|
print("Tables created.")
|
|
|
|
def seed_data():
|
|
db = SessionLocal()
|
|
|
|
# Check if data exists
|
|
if db.query(User).first():
|
|
print("Data already exists.")
|
|
db.close()
|
|
return
|
|
|
|
print("Seeding data...")
|
|
|
|
# Users - Password is 'secret' for everyone
|
|
hashed = get_password_hash("secret")
|
|
|
|
admin = User(email="admin@sumaq.com", hashed_password=hashed, full_name="Admin User", role=UserRole.ADMIN)
|
|
supervisor = User(email="super@sumaq.com", hashed_password=hashed, full_name="Juan Perez", role=UserRole.SUPERVISOR)
|
|
db.add(admin)
|
|
db.add(supervisor)
|
|
db.commit()
|
|
|
|
# Specialties
|
|
specs = ["Civil", "Mecánica", "Eléctrica", "SSOMA"]
|
|
for s_name in specs:
|
|
db.add(Specialty(name=s_name))
|
|
db.commit()
|
|
|
|
# Project
|
|
proj = Project(name="Planta ATE - Expansion", code="ATE-EXP-2025", location="Ate, Lima", start_date=datetime.datetime.now())
|
|
db.add(proj)
|
|
db.commit()
|
|
|
|
# Contractor
|
|
cont = Contractor(name="Constructora Global", ruc="20123456789")
|
|
db.add(cont)
|
|
db.commit()
|
|
|
|
# Activity
|
|
civil_spec = db.query(Specialty).filter_by(name="Civil").first()
|
|
act = Activity(
|
|
project_id=proj.id,
|
|
specialty_id=civil_spec.id,
|
|
contractor_id=cont.id,
|
|
user_id=supervisor.id,
|
|
description="Inspeccion de cimientos",
|
|
area="Zona Norte"
|
|
)
|
|
db.add(act)
|
|
db.commit()
|
|
|
|
# NC
|
|
nc = NonConformity(activity_id=act.id, description="Grieta en muro", level="major")
|
|
db.add(nc)
|
|
db.commit()
|
|
|
|
print("Seeding complete.")
|
|
db.close()
|
|
|
|
if __name__ == "__main__":
|
|
init_db()
|
|
seed_data()
|