registre-permissions/crud_cli.py

350 lines
11 KiB
Python
Raw Normal View History

2023-10-07 18:21:10 +00:00
import datetime
2023-10-07 03:46:25 +00:00
from crud import *
def add_person():
first_name = input('First name: ')
last_name = input('Last name: ')
2023-10-07 18:21:10 +00:00
date = datetime.date.fromisoformat(input('Date (YYYY-MM-DD): '))
2023-10-07 03:46:25 +00:00
person_model = PersonModel(first_name=first_name, last_name=last_name, date=date)
print("Adding Person ...")
2023-10-07 18:21:10 +00:00
db_add_person(person_model)
2023-10-07 03:46:25 +00:00
print("Person added")
return None
def list_person():
2023-10-07 18:21:10 +00:00
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)
2023-10-07 03:46:25 +00:00
return None
def edit_person():
2023-10-07 18:21:10 +00:00
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')
2023-10-07 03:46:25 +00:00
return None
def delete_person():
2023-10-07 18:21:10 +00:00
person_id = int(input('Person ID: '))
print('Deleting Person...')
db_delete_person(person_id)
print('Person deleted')
2023-10-07 03:46:25 +00:00
return None
def add_resource():
2023-10-07 18:21:10 +00:00
name = input('Resource name: ')
resource_model = ResourceModel(name=name)
print("Adding Resource...")
db_add_resource(resource_model)
print("Resource added")
2023-10-07 03:46:25 +00:00
return None
def list_resource():
2023-10-07 18:21:10 +00:00
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)
2023-10-07 03:46:25 +00:00
return None
def edit_resource():
2023-10-07 18:21:10 +00:00
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')
2023-10-07 03:46:25 +00:00
return None
def delete_resource():
2023-10-07 18:21:10 +00:00
resource_id = int(input('Resource ID: '))
print('Deleting Resource...')
db_delete_resource(resource_id)
print('Resource deleted')
2023-10-07 03:46:25 +00:00
return None
def add_role():
2023-10-07 18:21:10 +00:00
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")
2023-10-07 03:46:25 +00:00
return None
def list_role():
2023-10-07 18:21:10 +00:00
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)
2023-10-07 03:46:25 +00:00
return None
def edit_role():
2023-10-07 18:21:10 +00:00
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')
2023-10-07 03:46:25 +00:00
return None
def delete_role():
2023-10-07 18:21:10 +00:00
role_id = int(input('Role ID: '))
print('Deleting Role...')
db_delete_role(role_id)
print('Role deleted')
2023-10-07 03:46:25 +00:00
return None
def add_permission():
2023-10-08 02:10:27 +00:00
conf_resource = 'n'
conf_role = 'n'
resource_id = None
role_id = None
while not conf_resource == 'y':
resource_id = int(input('Resource ID: '))
resource = Resource.get(id=resource_id)
print(f'Resource name: {resource.name}')
conf_resource = input('Is this OK? (y)')
if conf_resource == '':
conf_resource = 'y'
while not conf_role == 'y':
role_id = int(input('Role ID: '))
role = Role.get(id=role_id)
print(f'Role name: {role.name}')
conf_role = input('Is this OK? (y)')
if conf_role == '':
conf_role = 'y'
2023-10-07 18:21:10 +00:00
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")
2023-10-07 03:46:25 +00:00
return None
def list_permission():
2023-10-07 18:21:10 +00:00
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)
2023-10-07 03:46:25 +00:00
return None
def edit_permission():
2023-10-07 18:21:10 +00:00
permission_id = int(input('Permission ID: '))
permission = Permission.get(id=permission_id)
2023-10-08 02:11:43 +00:00
resource_id = permission.idResource
role_id = permission.idRole
2023-10-07 18:21:10 +00:00
# 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
2023-10-08 02:10:27 +00:00
conf_resource = 'n'
while not conf_resource == 'y':
resource_id = input(f'Resource ID [{permission.idResource}]: ')
if resource_id == '':
resource_id = permission.idResource
resource = Resource.get(id=resource_id)
print(f'Resource name: {resource.name}')
conf_resource = input('Is this OK? (y)')
if conf_resource == '':
conf_resource = 'y'
2023-10-07 18:21:10 +00:00
# Editing role
2023-10-08 02:10:27 +00:00
conf_role = 'n'
while not conf_role == 'y':
role_id = input(f'Role ID [{permission.idRole}]: ')
if role_id == '':
role_id = permission.idRole
role = Role.get(id=role_id)
print(f'Role name: {role.name}')
conf_role = input('Is this OK? (y)')
if conf_role == '':
conf_role = 'y'
2023-10-07 18:21:10 +00:00
# Editing permission
print('Editing Permission...')
2023-10-08 02:10:27 +00:00
db_edit_permission(permission_id,
permission_model=PermissionModel(name=name, idResource=resource_id, idRole=role_id,
description=description))
2023-10-07 18:21:10 +00:00
print('Permission edited')
2023-10-07 03:46:25 +00:00
return None
def delete_permission():
2023-10-07 18:21:10 +00:00
permission_id = int(input('Permission ID: '))
print('Deleting Permission...')
db_delete_permission(permission_id)
print('Permission deleted')
2023-10-07 03:46:25 +00:00
return None
def add_user_account():
2023-10-07 18:21:10 +00:00
username = input('Username: ')
2023-10-08 02:10:27 +00:00
conf_person = 'n'
conf_resource = 'n'
conf_role = 'n'
person_id = None
resource_id = None
role_id = None
while not conf_person == 'y':
person_id = int(input('Person ID: '))
person = Person.get(id=person_id)
print(f'Person name: {person.first_name} {person.last_name}')
conf_person = input('Is this OK? (y)')
if conf_person == '':
conf_person = 'y'
while not conf_resource == 'y':
resource_id = int(input('Resource ID: '))
resource = Resource.get(id=resource_id)
print(f'Resource name: {resource.name}')
conf_resource = input('Is this OK? (y)')
if conf_resource == '':
conf_resource = 'y'
while not conf_role == 'y':
role_id = int(input('Role ID: '))
role = Role.get(id=role_id)
print(f'Role name: {role.name}')
conf_role = input('Is this OK? (y)')
if conf_role == '':
conf_role = 'y'
2023-10-07 18:21:10 +00:00
date = datetime.date.fromisoformat(input('Date (YYYY-MM-DD): '))
2023-10-08 02:10:27 +00:00
user_account_model = UserAccountModel(username=username, idPerson=person_id, idResource=resource_id, idRole=role_id,
date=date)
2023-10-07 18:21:10 +00:00
print("Adding User Account...")
db_add_user_account(user_account_model)
print("User Account added")
2023-10-07 03:46:25 +00:00
return None
def list_user_account():
2023-10-07 18:21:10 +00:00
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)
2023-10-07 03:46:25 +00:00
return None
def edit_user_account():
2023-10-07 18:21:10 +00:00
user_account_id = int(input('User Account ID: '))
user_account = UserAccount.get(id=user_account_id)
2023-10-08 02:11:43 +00:00
person_id = user_account.idPerson
resource_id = user_account.idResource
role_id = user_account.idRole
2023-10-07 18:21:10 +00:00
# Editing username
username = input(f'User Account username [{user_account.username}]: ')
if username == '':
username = user_account.username
# Editing person
2023-10-08 02:10:27 +00:00
conf_person = 'n'
while not conf_person == 'y':
person_id = input(f'User Account person ID [{user_account.idPerson}]')
if person_id == '':
person_id = user_account.idPerson
person = Person.get(id=person_id)
print(f'Person name: {person.first_name} {person.last_name}')
conf_person = input('Is this OK? (y)')
if conf_person == '':
conf_person = 'y'
2023-10-07 18:21:10 +00:00
# Editing resource
2023-10-08 02:10:27 +00:00
conf_resource = 'n'
while not conf_resource == 'y':
resource_id = input(f'User Account resource ID [{user_account.idResource}]')
if resource_id == '':
resource_id = user_account.idResource
resource = Resource.get(id=resource_id)
print(f'Resource name: {resource.name}')
conf_resource = input('Is this OK? (y)')
if conf_resource == '':
conf_resource = 'y'
2023-10-07 18:21:10 +00:00
# Editing role
2023-10-08 02:10:27 +00:00
conf_role = 'n'
while not conf_role == 'y':
role_id = input(f'User Account role ID [{user_account.idRole}]')
if role_id == '':
role_id = user_account.idRole
role = Role.get(id=role_id)
print(f'Role name: {role.name}')
conf_role = input('Is this OK? (y)')
if conf_role == '':
conf_role = 'y'
2023-10-07 18:21:10 +00:00
# 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')
2023-10-07 03:46:25 +00:00
return None
def delete_user_account():
2023-10-07 18:21:10 +00:00
user_account_id = int(input('User Account ID: '))
print('Deleting User Account...')
db_delete_user_account(user_account_id)
print('User Account deleted')
2023-10-07 03:46:25 +00:00
return None