build seeder

This commit is contained in:
2024-05-10 06:31:31 +00:00
parent bc8815f40e
commit 392dee8640
5 changed files with 46 additions and 34 deletions

View File

@ -1,5 +1,9 @@
from db_setup import engine
from fuware.db.seeder import initialize_table
from models._model_base import Model
from models.users import *
from sqlalchemy import event
from models.users import User
event.listen(User.__table__, 'after_create', initialize_table)
Model.metadata.create_all(bind=engine)

28
fuware/db/seeder.py Normal file
View File

@ -0,0 +1,28 @@
from fuware.core.security import get_hasher
hasher = get_hasher()
INITIAL_DATA = {
'users': [
{
'username': 'sam',
'password': hasher.hash('admin'),
'name': 'Sam',
'is_admin': 1,
'is_lock': 0,
},
{
'username': 'sam1',
'password': hasher.hash('admin'),
'name': 'Sam1',
'is_admin': 0,
'is_lock': 1
},
]
}
# This method receives a table, a connection and inserts data to that table.
def initialize_table(target, connection, **kwargs):
tablename = str(target)
if tablename in INITIAL_DATA and len(INITIAL_DATA[tablename]) > 0:
connection.execute(target.insert(), INITIAL_DATA[tablename])