-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathseed.py
More file actions
60 lines (52 loc) · 2.15 KB
/
seed.py
File metadata and controls
60 lines (52 loc) · 2.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
from sqlalchemy.orm import Session
from passlib.context import CryptContext
from database import engine, SessionLocal
from models import Employee, Department, Position
# Контекст для хэширования паролей
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
# Данные для отделов и должностей
departments_data = [
{"name": "Закупки"},
{"name": "Склад"},
{"name": "Продажи"},
]
positions_data = [
{"name": "Менеджер по закупкам"},
{"name": "Кладовщик"},
{"name": "Менеджер по продажам"},
]
def seed_data():
db: Session = SessionLocal()
try:
# Добавляем отделы
for department in departments_data:
existing = db.query(Department).filter(Department.name == department["name"]).first()
if not existing:
db.add(Department(**department))
# Добавляем должности
for position in positions_data:
existing = db.query(Position).filter(Position.name == position["name"]).first()
if not existing:
db.add(Position(**position))
# Добавляем аккаунт администратора
admin_exists = db.query(Employee).filter(Employee.phone == "1").first()
if not admin_exists:
admin = Employee(
first_name="Админ",
last_name="Администратор",
phone="1",
password=pwd_context.hash("admin"), # Хэшируем пароль
role="admin",
department_id=None, # Админ не привязан к отделу
position_id=None, # Админ не привязан к должности
)
db.add(admin)
db.commit()
print("Данные успешно добавлены в базу.")
except Exception as e:
print(f"Ошибка при заполнении базы данных: {e}")
db.rollback()
finally:
db.close()
if __name__ == "__main__":
seed_data()