Seed utenti demo
This commit is contained in:
@@ -0,0 +1,92 @@
|
|||||||
|
import asyncio
|
||||||
|
from sqlalchemy import select
|
||||||
|
from app.core.database import AsyncSessionLocal
|
||||||
|
from app.core.security import hash_password
|
||||||
|
from app.models.user import User, Group, UserRole
|
||||||
|
|
||||||
|
|
||||||
|
DEMO_GROUPS = [
|
||||||
|
{"name": "GMG Sede Principale", "description": "Sede principale GMG SPA"},
|
||||||
|
{"name": "GMG Filiale Nord", "description": "Filiale nord"},
|
||||||
|
]
|
||||||
|
|
||||||
|
DEMO_USERS = [
|
||||||
|
{
|
||||||
|
"email": "venditore@gmg.it",
|
||||||
|
"full_name": "Mario Venditore",
|
||||||
|
"password": "Demo1234!",
|
||||||
|
"role": UserRole.venditore,
|
||||||
|
"group_index": 0,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"email": "valutatore@gmg.it",
|
||||||
|
"full_name": "Luca Valutatore",
|
||||||
|
"password": "Demo1234!",
|
||||||
|
"role": UserRole.valutatore,
|
||||||
|
"group_index": 0,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"email": "backoffice@gmg.it",
|
||||||
|
"full_name": "Sara Backoffice",
|
||||||
|
"password": "Demo1234!",
|
||||||
|
"role": UserRole.backoffice,
|
||||||
|
"group_index": 0,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"email": "operatore@gmg.it",
|
||||||
|
"full_name": "Carlo Operatore",
|
||||||
|
"password": "Demo1234!",
|
||||||
|
"role": UserRole.operatore_perizie,
|
||||||
|
"group_index": 0,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"email": "approvatore@gmg.it",
|
||||||
|
"full_name": "Giulia Approvatore",
|
||||||
|
"password": "Demo1234!",
|
||||||
|
"role": UserRole.approvatore_perizie,
|
||||||
|
"group_index": 0,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"email": "admin@gmg.it",
|
||||||
|
"full_name": "Admin GMG",
|
||||||
|
"password": "Admin1234!",
|
||||||
|
"role": UserRole.admin,
|
||||||
|
"group_index": None,
|
||||||
|
},
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
async def run_seed():
|
||||||
|
async with AsyncSessionLocal() as session:
|
||||||
|
groups = []
|
||||||
|
for g_data in DEMO_GROUPS:
|
||||||
|
result = await session.execute(select(Group).where(Group.name == g_data["name"]))
|
||||||
|
group = result.scalar_one_or_none()
|
||||||
|
if not group:
|
||||||
|
group = Group(**g_data)
|
||||||
|
session.add(group)
|
||||||
|
await session.flush()
|
||||||
|
print(f" Gruppo creato: {group.name}")
|
||||||
|
groups.append(group)
|
||||||
|
|
||||||
|
for u_data in DEMO_USERS:
|
||||||
|
result = await session.execute(select(User).where(User.email == u_data["email"]))
|
||||||
|
user = result.scalar_one_or_none()
|
||||||
|
if not user:
|
||||||
|
group_id = groups[u_data["group_index"]].id if u_data["group_index"] is not None else None
|
||||||
|
user = User(
|
||||||
|
email=u_data["email"],
|
||||||
|
full_name=u_data["full_name"],
|
||||||
|
hashed_password=hash_password(u_data["password"]),
|
||||||
|
role=u_data["role"],
|
||||||
|
group_id=group_id,
|
||||||
|
)
|
||||||
|
session.add(user)
|
||||||
|
print(f" Utente creato: {user.email} ({user.role.value})")
|
||||||
|
|
||||||
|
await session.commit()
|
||||||
|
print("Seed completato.")
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
asyncio.run(run_seed())
|
||||||
Reference in New Issue
Block a user