registre-permissions/crud.py

154 lines
4.2 KiB
Python
Raw Permalink Normal View History

2023-10-07 03:46:25 +00:00
from data_models import Person, Resource, Role, Permission, UserAccount
from pydantic_models import PersonModel, ResourceModel, RoleModel, PermissionModel, UserAccountModel
2023-09-24 16:58:19 +00:00
2023-10-07 18:21:10 +00:00
def db_add_person(person_model: PersonModel):
2023-10-07 03:46:25 +00:00
Person.create(**person_model.model_dump())
2023-09-24 16:58:19 +00:00
return 0
2023-10-07 18:21:10 +00:00
def db_list_person(search_query=None):
2023-10-07 03:46:25 +00:00
query = Person.select()
if search_query:
query = query.where(Person.first_name.contains(search_query))
2023-10-07 18:21:10 +00:00
return query
2023-09-24 16:58:19 +00:00
2023-10-07 18:21:10 +00:00
def db_edit_person(id: int, person_model: PersonModel):
2023-10-07 03:46:25 +00:00
person = Person.get(id=id)
person.first_name = person_model.first_name
person.last_name = person_model.last_name
person.date = person_model.date
person.save()
2023-09-24 16:58:19 +00:00
return 0
2023-10-07 18:21:10 +00:00
def db_delete_person(id: int):
2023-10-07 03:46:25 +00:00
person = Person.get(id=id)
person.delete_instance()
2023-09-24 16:58:19 +00:00
return 0
2023-10-07 18:21:10 +00:00
def db_add_resource(resource_model: ResourceModel):
2023-10-07 03:46:25 +00:00
Resource.create(**resource_model.model_dump())
2023-09-24 16:58:19 +00:00
return 0
2023-10-07 18:21:10 +00:00
def db_list_resource(search_query=None):
2023-10-07 03:46:25 +00:00
query = Resource.select()
if search_query:
query = query.where(Resource.name.contains(search_query))
resource_list = []
for entry in query:
print(entry)
resource_list.append(entry)
return resource_list
2023-09-24 16:58:19 +00:00
2023-10-07 18:21:10 +00:00
def db_edit_resource(resource_id: int, resource_model: ResourceModel):
2023-10-07 03:46:25 +00:00
resource = Resource.get(id=resource_id)
resource.name = resource_model.name
resource.save()
2023-09-24 16:58:19 +00:00
return 0
2023-10-07 18:21:10 +00:00
def db_delete_resource(resource_id: int):
2023-10-07 03:46:25 +00:00
resource = Resource.get(id=resource_id)
resource.delete_instance()
2023-09-24 16:58:19 +00:00
return 0
2023-10-07 18:21:10 +00:00
def db_add_role(role_model: RoleModel):
2023-10-07 03:46:25 +00:00
Role.create(**role_model.model_dump())
2023-09-24 16:58:19 +00:00
return 0
2023-10-07 18:21:10 +00:00
def db_list_role(search_query=None):
2023-10-07 03:46:25 +00:00
query = Role.select()
if search_query:
query = query.where(Role.name.contains(search_query))
role_list = []
for entry in query:
print(entry)
role_list.append(entry)
return role_list
2023-09-24 16:58:19 +00:00
2023-10-07 18:21:10 +00:00
def db_edit_role(role_id: int, role_model: RoleModel):
2023-10-07 03:46:25 +00:00
role = Role.get(id=role_id)
role.name = role_model.name
role.description = role_model.description
role.save()
2023-09-24 16:58:19 +00:00
return 0
2023-10-07 18:21:10 +00:00
def db_delete_role(role_id: int):
2023-10-07 03:46:25 +00:00
role = Role.get(id=role_id)
role.delete_instance()
2023-09-24 16:58:19 +00:00
return 0
2023-10-07 18:21:10 +00:00
def db_add_permission(permission_model: PermissionModel):
2023-10-07 03:46:25 +00:00
Permission.create(**permission_model.model)
2023-09-24 16:58:19 +00:00
return 0
2023-10-07 18:21:10 +00:00
def db_list_permission(search_query=None):
2023-10-07 03:46:25 +00:00
query = Permission.select()
if search_query:
query = query.where(Permission.name.contains(search_query))
permission_list = []
for entry in query:
print(entry)
permission_list.append(entry)
return permission_list
2023-09-24 16:58:19 +00:00
2023-10-07 18:21:10 +00:00
def db_edit_permission(permission_id: int, permission_model: PermissionModel):
2023-10-07 03:46:25 +00:00
permission = Permission.get(id=permission_id)
permission.name = permission_model.name
permission.idResource = Resource.get(id=permission_model.idResource).id
permission.idRole = Role.get(id=permission_model.idRole).id
permission.description = permission_model.description
permission.save()
2023-09-24 16:58:19 +00:00
return 0
2023-10-07 18:21:10 +00:00
def db_delete_permission(permission_id: int):
2023-10-07 03:46:25 +00:00
permission = Permission.get(id=permission_id)
permission.delete_instance()
2023-09-24 16:58:19 +00:00
return 0
2023-10-07 18:21:10 +00:00
def db_add_user_account(user_account_model: UserAccountModel):
2023-10-07 03:46:25 +00:00
UserAccount.create(**user_account_model.model_dump())
2023-09-24 16:58:19 +00:00
return 0
2023-10-07 18:21:10 +00:00
def db_list_user_account(search_query=None):
2023-10-07 03:46:25 +00:00
query = UserAccount.select()
if search_query:
query = query.where(UserAccount.email.contains(search_query))
user_account_list = []
for entry in query:
print(entry)
user_account_list.append(entry)
return user_account_list
2023-10-07 18:21:10 +00:00
def db_edit_user_account(user_account_id: int, user_account_model: UserAccountModel):
2023-10-07 03:46:25 +00:00
user_account = UserAccount.get(id=user_account_id)
user_account.email = user_account_model.email
user_account.idPerson = Person.get(id=user_account_model.idPerson).id
user_account.idResource = Resource.get(id=user_account_model.idResource).id
user_account.idRole = Role.get(id=user_account_model.idRole).id
user_account.date = user_account_model.date
user_account.save()
2023-09-24 16:58:19 +00:00
return 0
2023-10-07 18:21:10 +00:00
def db_delete_user_account(user_account_id: int):
2023-10-07 03:46:25 +00:00
user_account = UserAccount.get(id=user_account_id)
user_account.delete_instance()
2023-09-24 16:58:19 +00:00
return 0