registre-permissions/crud.py
2023-10-07 14:21:10 -04:00

153 lines
4.2 KiB
Python

from data_models import Person, Resource, Role, Permission, UserAccount
from pydantic_models import PersonModel, ResourceModel, RoleModel, PermissionModel, UserAccountModel
def db_add_person(person_model: PersonModel):
Person.create(**person_model.model_dump())
return 0
def db_list_person(search_query=None):
query = Person.select()
if search_query:
query = query.where(Person.first_name.contains(search_query))
return query
def db_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()
return 0
def db_delete_person(id: int):
person = Person.get(id=id)
person.delete_instance()
return 0
def db_add_resource(resource_model: ResourceModel):
Resource.create(**resource_model.model_dump())
return 0
def db_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
def db_edit_resource(resource_id: int, resource_model: ResourceModel):
resource = Resource.get(id=resource_id)
resource.name = resource_model.name
resource.save()
return 0
def db_delete_resource(resource_id: int):
resource = Resource.get(id=resource_id)
resource.delete_instance()
return 0
def db_add_role(role_model: RoleModel):
Role.create(**role_model.model_dump())
return 0
def db_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
def db_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()
return 0
def db_delete_role(role_id: int):
role = Role.get(id=role_id)
role.delete_instance()
return 0
def db_add_permission(permission_model: PermissionModel):
Permission.create(**permission_model.model)
return 0
def db_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
def db_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()
return 0
def db_delete_permission(permission_id: int):
permission = Permission.get(id=permission_id)
permission.delete_instance()
return 0
def db_add_user_account(user_account_model: UserAccountModel):
UserAccount.create(**user_account_model.model_dump())
return 0
def db_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 db_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()
return 0
def db_delete_user_account(user_account_id: int):
user_account = UserAccount.get(id=user_account_id)
user_account.delete_instance()
return 0