Ajout de fonctions CRUD

This commit is contained in:
François Pelletier 2023-10-06 23:46:25 -04:00
parent 43653afd08
commit fe66c52dbd
5 changed files with 277 additions and 58 deletions

170
crud.py
View file

@ -1,99 +1,157 @@
def add_person():
# TODO: add
from data_models import Person, Resource, Role, Permission, UserAccount
from pydantic_models import PersonModel, ResourceModel, RoleModel, PermissionModel, UserAccountModel
def add_person(person_model: PersonModel):
Person.create(**person_model.model_dump())
return 0
def list_person():
# TODO: list
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
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()
return 0
def edit_person():
# TODO: edit
def delete_person(id: int):
person = Person.get(id=id)
person.delete_instance()
return 0
def delete_person():
# TODO: delete
def add_resource(resource_model: ResourceModel):
Resource.create(**resource_model.model_dump())
return 0
def add_resource():
# TODO: add
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
def edit_resource(resource_id: int, resource_model: ResourceModel):
resource = Resource.get(id=resource_id)
resource.name = resource_model.name
resource.save()
return 0
def list_resource():
# TODO: list
def delete_resource(resource_id: int):
resource = Resource.get(id=resource_id)
resource.delete_instance()
return 0
def edit_resource():
# TODO: edit
def add_role(role_model: RoleModel):
Role.create(**role_model.model_dump())
return 0
def delete_resource():
# TODO: delete
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
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()
return 0
def add_role():
# TODO: add
def delete_role(role_id: int):
role = Role.get(id=role_id)
role.delete_instance()
return 0
def list_role():
# TODO: list
def add_permission(permission_model: PermissionModel):
Permission.create(**permission_model.model)
return 0
def edit_role():
# TODO: edit
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
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()
return 0
def delete_role():
# TODO: delete
def delete_permission(permission_id: int):
permission = Permission.get(id=permission_id)
permission.delete_instance()
return 0
def add_permission():
# TODO: add
def add_user_account(user_account_model: UserAccountModel):
UserAccount.create(**user_account_model.model_dump())
return 0
def list_permission():
# TODO: list
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()
return 0
def edit_permission():
# TODO: edit
return 0
def delete_permission():
# TODO: delete
return 0
def add_user_account():
# TODO: add
return 0
def list_user_account():
# TODO: list
return 0
def edit_user_account():
# TODO: edit
return 0
def delete_user_account():
# TODO: delete
def delete_user_account(user_account_id: int):
user_account = UserAccount.get(id=user_account_id)
user_account.delete_instance()
return 0

88
crud_cli.py Normal file
View file

@ -0,0 +1,88 @@
from crud import *
def add_person():
first_name = input('First name: ')
last_name = input('Last name: ')
date = input('Date: ')
person_model = PersonModel(first_name=first_name, last_name=last_name, date=date)
print("Adding Person ...")
add_person(person_model)
print("Person added")
return None
def list_person():
return None
def edit_person():
return None
def delete_person():
return None
def add_resource():
return None
def list_resource():
return None
def edit_resource():
return None
def delete_resource():
return None
def add_role():
return None
def list_role():
return None
def edit_role():
return None
def delete_role():
return None
def add_permission():
return None
def list_permission():
return None
def edit_permission():
return None
def delete_permission():
return None
def add_user_account():
return None
def list_user_account():
return None
def edit_user_account():
return None
def delete_user_account():
return None

View file

@ -8,7 +8,7 @@ from collections import OrderedDict
from getpass import getpass
import sys
import data_models as dm
from crud import *
from crud_cli import *
def initialize(db_passphrase):

72
pydantic_models.py Normal file
View file

@ -0,0 +1,72 @@
from pydantic import BaseModel
from datetime import datetime
class PersonModel(BaseModel):
first_name: str
last_name: str
date: datetime
class ReadPersonModel(PersonModel):
id: int
class UpdatePersonModel(PersonModel):
id: int
class ResourceModel(BaseModel):
name: str
class ReadResourceModel(ResourceModel):
id: int
class UpdateResourceModel(ResourceModel):
id: int
class RoleModel(BaseModel):
name: str
description: str
class ReadRoleModel(RoleModel):
id: int
class UpdateRoleModel(RoleModel):
id: int
class PermissionModel(BaseModel):
name: str
idResource: int
idRole: int
description: str
class ReadPermissionModel(PermissionModel):
id: int
class UpdatePermissionModel(PermissionModel):
id: int
class UserAccountModel(BaseModel):
username: str
idPerson: int
idResource: int
idRole: int
date: datetime
class ReadUserAccountModel(UserAccountModel):
id: int
class UpdateUserAccountModel(UserAccountModel):
id: int

View file

@ -1,2 +1,3 @@
sqlcipher3_binary~=0.5.2
peewee~=3.16.3
peewee~=3.16.3
pydantic~=2.4.2