File size: 2,971 Bytes
b7a7f32
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import re

from sqlalchemy.orm import Session

import cruds
import schemas
from core import settings
from core.db import Base, SessionLocal
from cruds import crud_user
from models import *  # noqa: F401


def pascal_case_to_snake(name):
    name = re.sub("(.)([A-Z][a-z]+)", r"\1_\2", name)
    return re.sub("([a-z0-9])([A-Z])", r"\1_\2", name).lower()


def init_db(db: Session = SessionLocal()) -> None:
    super_user = cruds.crud_user.get_by_email(db, email=settings.FIRST_SUPERUSER)
    if not super_user:
        user_in = schemas.UserCreate(
            full_name="Yugesh",
            dob="2021-04-14",
            address="Dhulikhel",
            contact_number="986152526272",
            email=settings.FIRST_SUPERUSER,
            password=settings.FIRST_SUPERUSER_PASSWORD,
            is_superuser=True,
            user_type=1,
        )
        super_user = cruds.crud_user.create(db, obj_in=user_in)  # noqa: F841


# def init_permissions(db: Session = SessionLocal()) -> None:
#     super_user = crud_user.get_by_id(db, id=1)
#     for model in Base.__subclasses__():
#         try:
#             name = pascal_case_to_snake(model.__name__)
#             permission_create = schemas.UserPermissionCreate(name=f"{name}_create")
#             permission_create = cruds.crud_user_permission.create(
#                 db,
#                 obj_in=permission_create,
#             )
#         except Exception:  # noqa
#             pass

#         try:
#             name = pascal_case_to_snake(model.__name__)
#             permission_update = schemas.UserPermissionCreate(name=f"{name}_update")
#             permission_update = cruds.crud_user_permission.create(
#                 db,
#                 obj_in=permission_update,
#             )
#         except Exception:  # noqa
#             pass

#         try:
#             name = pascal_case_to_snake(model.__name__)
#             permission_retrieve = schemas.UserPermissionCreate(name=f"{name}_get")
#             permission_retrieve = cruds.crud_user_permission.create(
#                 db,
#                 obj_in=permission_retrieve,
#             )
#         except Exception:  # noqa
#             pass
#         try:
#             name = pascal_case_to_snake(model.__name__)
#             permission_retrieve = schemas.UserPermissionCreate(name=f"{name}_get_self")
#             permission_retrieve = cruds.crud_user_permission.create(
#                 db,
#                 obj_in=permission_retrieve,
#             )
#         except Exception:  # noqa
#             pass

#         try:
#             name = pascal_case_to_snake(model.__name__)
#             permission_retrieve = schemas.UserPermissionCreate(
#                 name=f"{name}_update_self"
#             )
#             permission_retrieve = cruds.crud_user_permission.create(
#                 db,
#                 obj_in=permission_retrieve,
#             )
#         except Exception:  # noqa
#             pass