registre-permissions/crud.py

158 lines
4.2 KiB
Python
Raw 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 03:46:25 +00:00
def add_person(person_model: PersonModel):
Person.create(**person_model.model_dump())
2023-09-24 16:58:19 +00:00
return 0
2023-10-07 03:46:25 +00:00
def list_person(search_query=None):
query = Person.select()
if search_query:
query = query.where(Person.first_name.contains(search_query))
person_list = []
for entry in query:
print(entry)
person_list.append(entry)
return person_list
2023-09-24 16:58:19 +00:00
2023-10-07 03:46:25 +00:00
def edit_person(id: int, person_model: PersonModel):
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 03:46:25 +00:00
def delete_person(id: int):
person = Person.get(id=id)
person.delete_instance()
2023-09-24 16:58:19 +00:00
return 0
2023-10-07 03:46:25 +00:00
def add_resource(resource_model: ResourceModel):
Resource.create(**resource_model.model_dump())
2023-09-24 16:58:19 +00:00
return 0
2023-10-07 03:46:25 +00:00
def list_resource(search_query=None):
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 03:46:25 +00:00
def edit_resource(resource_id: int, resource_model: ResourceModel):
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 03:46:25 +00:00
def delete_resource(resource_id: int):
resource = Resource.get(id=resource_id)
resource.delete_instance()
2023-09-24 16:58:19 +00:00
return 0
2023-10-07 03:46:25 +00:00
def add_role(role_model: RoleModel):
Role.create(**role_model.model_dump())
2023-09-24 16:58:19 +00:00
return 0
2023-10-07 03:46:25 +00:00
def list_role(search_query=None):
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 03:46:25 +00:00
def edit_role(role_id: int, role_model: RoleModel):
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 03:46:25 +00:00
def delete_role(role_id: int):
role = Role.get(id=role_id)
role.delete_instance()
2023-09-24 16:58:19 +00:00
return 0
2023-10-07 03:46:25 +00:00
def add_permission(permission_model: PermissionModel):
Permission.create(**permission_model.model)
2023-09-24 16:58:19 +00:00
return 0
2023-10-07 03:46:25 +00:00
def list_permission(search_query=None):
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 03:46:25 +00:00
def edit_permission(permission_id: int, permission_model: PermissionModel):
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 03:46:25 +00:00
def delete_permission(permission_id: int):
permission = Permission.get(id=permission_id)
permission.delete_instance()
2023-09-24 16:58:19 +00:00
return 0
2023-10-07 03:46:25 +00:00
def add_user_account(user_account_model: UserAccountModel):
UserAccount.create(**user_account_model.model_dump())
2023-09-24 16:58:19 +00:00
return 0
2023-10-07 03:46:25 +00:00
def list_user_account(search_query=None):
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
def edit_user_account(user_account_id: int, user_account_model: UserAccountModel):
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 03:46:25 +00:00
def delete_user_account(user_account_id: int):
user_account = UserAccount.get(id=user_account_id)
user_account.delete_instance()
2023-09-24 16:58:19 +00:00
return 0