diff --git a/crud.py b/crud.py index cec9207..8aef5c6 100644 --- a/crud.py +++ b/crud.py @@ -2,23 +2,19 @@ from data_models import Person, Resource, Role, Permission, UserAccount from pydantic_models import PersonModel, ResourceModel, RoleModel, PermissionModel, UserAccountModel -def add_person(person_model: PersonModel): +def db_add_person(person_model: PersonModel): Person.create(**person_model.model_dump()) return 0 -def list_person(search_query=None): +def db_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 + return query -def edit_person(id: int, person_model: PersonModel): +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 @@ -27,18 +23,18 @@ def edit_person(id: int, person_model: PersonModel): return 0 -def delete_person(id: int): +def db_delete_person(id: int): person = Person.get(id=id) person.delete_instance() return 0 -def add_resource(resource_model: ResourceModel): +def db_add_resource(resource_model: ResourceModel): Resource.create(**resource_model.model_dump()) return 0 -def list_resource(search_query=None): +def db_list_resource(search_query=None): query = Resource.select() if search_query: query = query.where(Resource.name.contains(search_query)) @@ -49,25 +45,25 @@ def list_resource(search_query=None): return resource_list -def edit_resource(resource_id: int, resource_model: ResourceModel): +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 delete_resource(resource_id: int): +def db_delete_resource(resource_id: int): resource = Resource.get(id=resource_id) resource.delete_instance() return 0 -def add_role(role_model: RoleModel): +def db_add_role(role_model: RoleModel): Role.create(**role_model.model_dump()) return 0 -def list_role(search_query=None): +def db_list_role(search_query=None): query = Role.select() if search_query: query = query.where(Role.name.contains(search_query)) @@ -78,7 +74,7 @@ def list_role(search_query=None): return role_list -def edit_role(role_id: int, role_model: RoleModel): +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 @@ -86,18 +82,18 @@ def edit_role(role_id: int, role_model: RoleModel): return 0 -def delete_role(role_id: int): +def db_delete_role(role_id: int): role = Role.get(id=role_id) role.delete_instance() return 0 -def add_permission(permission_model: PermissionModel): +def db_add_permission(permission_model: PermissionModel): Permission.create(**permission_model.model) return 0 -def list_permission(search_query=None): +def db_list_permission(search_query=None): query = Permission.select() if search_query: query = query.where(Permission.name.contains(search_query)) @@ -108,7 +104,7 @@ def list_permission(search_query=None): return permission_list -def edit_permission(permission_id: int, permission_model: PermissionModel): +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 @@ -118,18 +114,18 @@ def edit_permission(permission_id: int, permission_model: PermissionModel): return 0 -def delete_permission(permission_id: int): +def db_delete_permission(permission_id: int): permission = Permission.get(id=permission_id) permission.delete_instance() return 0 -def add_user_account(user_account_model: UserAccountModel): +def db_add_user_account(user_account_model: UserAccountModel): UserAccount.create(**user_account_model.model_dump()) return 0 -def list_user_account(search_query=None): +def db_list_user_account(search_query=None): query = UserAccount.select() if search_query: query = query.where(UserAccount.email.contains(search_query)) @@ -140,7 +136,7 @@ def list_user_account(search_query=None): return user_account_list -def edit_user_account(user_account_id: int, user_account_model: UserAccountModel): +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 @@ -151,7 +147,7 @@ def edit_user_account(user_account_id: int, user_account_model: UserAccountModel return 0 -def delete_user_account(user_account_id: int): +def db_delete_user_account(user_account_id: int): user_account = UserAccount.get(id=user_account_id) user_account.delete_instance() return 0 diff --git a/crud_cli.py b/crud_cli.py index a49cddb..87d7199 100644 --- a/crud_cli.py +++ b/crud_cli.py @@ -1,88 +1,266 @@ +import datetime + from crud import * def add_person(): first_name = input('First name: ') last_name = input('Last name: ') - date = input('Date: ') + date = datetime.date.fromisoformat(input('Date (YYYY-MM-DD): ')) person_model = PersonModel(first_name=first_name, last_name=last_name, date=date) print("Adding Person ...") - add_person(person_model) + db_add_person(person_model) print("Person added") return None def list_person(): + query_filter = input('Filter: ') + if query_filter: + person_list = db_list_person(query_filter) + else: + person_list = db_list_person() + for person in person_list.tuples().iterator(): + print(person) return None def edit_person(): + person_id = int(input('Person ID: ')) + person = Person.get(id=person_id) + first_name = input(f'First name [{person.first_name}]: ') + if first_name == '': + first_name = person.first_name + last_name = input(f'Last name [{person.last_name}]: ') + if last_name == '': + last_name = person.last_name + date = datetime.date.fromisoformat(input(f'Date [{datetime.date.isoformat(person.date)}]: ')) + if date == '': + date = person.date + print('Editing Person...') + db_edit_person(person_id, person_model=PersonModel(first_name=first_name, + last_name=last_name, + date=date)) + print('Person edited') return None def delete_person(): + person_id = int(input('Person ID: ')) + print('Deleting Person...') + db_delete_person(person_id) + print('Person deleted') return None def add_resource(): + name = input('Resource name: ') + resource_model = ResourceModel(name=name) + print("Adding Resource...") + db_add_resource(resource_model) + print("Resource added") return None def list_resource(): + query_filter = input('Filter: ') + if query_filter: + resource_list = db_list_resource(query_filter) + else: + resource_list = db_list_resource() + for resource in resource_list.tuples().iterator(): + print(resource) return None def edit_resource(): + resource_id = int(input('Resource ID: ')) + resource = Resource.get(id=resource_id) + # Editing name + name = input(f'Resource name [{resource.name}]: ') + if name == '': + name = resource.name + # Editing resource + print('Editing Resource...') + db_edit_resource(resource_id, resource_model=ResourceModel(name=name)) + print('Resource edited') return None def delete_resource(): + resource_id = int(input('Resource ID: ')) + print('Deleting Resource...') + db_delete_resource(resource_id) + print('Resource deleted') return None def add_role(): + name = input('Role Name: ') + description = input('Role Description: ') + role_model = RoleModel(name=name, description=description) + print("Adding Role...") + db_add_role(role_model) + print("Role added") return None def list_role(): + query_filter = input('Filter: ') + if query_filter: + role_list = db_list_role(query_filter) + else: + role_list = db_list_role() + for role in role_list.tuples().iterator(): + print(role) return None def edit_role(): + role_id = int(input('Role ID: ')) + role = Role.get(id=role_id) + # Editing name + name = input(f'Role name [{role.name}]: ') + if name == '': + name = role.name + # Editing description + description = input(f'Role description [{role.description}]: ') + if description == '': + description = role.description + # Editing role + print('Editing Role...') + db_edit_role(role_id, role_model=RoleModel(name=name, description=description)) + print('Role edited') return None def delete_role(): + role_id = int(input('Role ID: ')) + print('Deleting Role...') + db_delete_role(role_id) + print('Role deleted') return None def add_permission(): + resource_id = int(input('Resource ID: ')) + role_id = int(input('Role ID: ')) + name = input('Permission Name: ') + description = input('Permission Description: ') + permission_model = PermissionModel(name=name, idResource=resource_id, idRole=role_id, description=description) + print("Adding Permission...") + db_add_permission(permission_model) + print("Permission added") return None def list_permission(): + query_filter = input('Filter: ') + if query_filter: + permission_list = db_list_permission(query_filter) + else: + permission_list = db_list_permission() + for permission in permission_list.tuples().iterator(): + print(permission) return None def edit_permission(): + permission_id = int(input('Permission ID: ')) + permission = Permission.get(id=permission_id) + # Editing name + name = input(f'Permission name [{permission.name}]: ') + if name == '': + name = permission.name + # Editing description + description = input(f'Permission description [{permission.description}]: ') + if description == '': + description = permission.description + # Editing resource + resource_id = input(f'Resource ID [{permission.idResource}]: ') + if resource_id == '': + resource_id = permission.idResource + # Editing role + role_id = input(f'Role ID [{permission.idRole}]: ') + if role_id == '': + role_id = permission.idRole + # Editing permission + print('Editing Permission...') + db_edit_permission(permission_id, permission_model=PermissionModel(name=name, idResource=resource_id, idRole=role_id, description=description)) + print('Permission edited') return None def delete_permission(): + permission_id = int(input('Permission ID: ')) + print('Deleting Permission...') + db_delete_permission(permission_id) + print('Permission deleted') return None def add_user_account(): + username = input('Username: ') + person_id = int(input('Person ID: ')) + resource_id = int(input('Resource ID: ')) + role_id = int(input('Role ID: ')) + date = datetime.date.fromisoformat(input('Date (YYYY-MM-DD): ')) + user_account_model = UserAccountModel(username=username, idPerson=person_id, idResource=resource_id, idRole=role_id, date=date) + print("Adding User Account...") + db_add_user_account(user_account_model) + print("User Account added") return None def list_user_account(): + query_filter = input('Filter: ') + if query_filter: + user_account_list = db_list_user_account(query_filter) + else: + user_account_list = db_list_user_account() + for user_account in user_account_list.tuples().iterator(): + print(user_account) return None def edit_user_account(): + user_account_id = int(input('User Account ID: ')) + user_account = UserAccount.get(id=user_account_id) + # Editing username + username = input(f'User Account username [{user_account.username}]: ') + if username == '': + username = user_account.username + # Editing person + person_id = input(f'User Account person ID [{user_account.idPerson}]') + if person_id == '': + person_id = user_account.idPerson + # Editing resource + resource_id = input(f'User Account resource ID [{user_account.idResource}]') + if resource_id == '': + resource_id = user_account.idResource + # Editing role + role_id = input(f'User Account role ID [{user_account.idRole}]') + if role_id == '': + role_id = user_account.idRole + # Editing date + date = datetime.date.fromisoformat(input(f'User Account date [{datetime.date.isoformat(user_account.date)}]: ')) + if date == '': + date = user_account.date + # Editing user account + print('Editing User Account...') + db_edit_user_account(user_account_id, + user_account_model=UserAccountModel(username=username, + idPerson=person_id, + idResource=resource_id, + idRole=role_id, + date=date)) + print('User Account edited') return None def delete_user_account(): + user_account_id = int(input('User Account ID: ')) + print('Deleting User Account...') + db_delete_user_account(user_account_id) + print('User Account deleted') return None diff --git a/requirements.txt b/requirements.txt index c132313..c88a608 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ sqlcipher3_binary~=0.5.2 peewee~=3.16.3 -pydantic~=2.4.2 \ No newline at end of file +pydantic~=2.4.2 +fuzzyfinder~=2.1.0 \ No newline at end of file