add docstring and call view instead prints

This commit is contained in:
2025-08-22 11:50:50 +02:00
parent c7c76b3bef
commit eb11a3cd04
2 changed files with 38 additions and 8 deletions

View File

@@ -7,20 +7,38 @@ from sqlalchemy import select, update, insert, delete
class Tools: class Tools:
"""
Generic tools with object as an arg
"""
def __init__(self, db: Session): def __init__(self, db: Session):
self.db = db self.db = db
self.view = View() self.view = View()
def list(self, object): def list(self, object):
"""
Select all objects from DB and return the list
:param object: object in Collaborator, Customer, Contract, Event
:return: list of object
"""
while self.db: while self.db:
return self.db.execute(select(object)).all() return self.db.execute(select(object)).all()
def filter(self, object, filter): def filter(self, object, filter):
"""
Select objects from DB where filter and return the list
:param object: object in Collaborator, Customer, Contract, Event
:param filter: (attribute, value):tuple
:return: list of selected object matching filter
"""
item, value = filter item, value = filter
stmt = (select(object).where(**{item: value})) stmt = (select(object).where(**{item: value}))
print(stmt) while self.db:
# while self.db: result = self.db.execute(
# return self.db.execute(select(object).where(**{item: value})).all() select(object).where(**{item: value})).all()
if not result:
self.view.display_error()
self.view.display_results(result)
class CollaboratorTools: class CollaboratorTools:
""" """
@@ -34,6 +52,8 @@ class CollaboratorTools:
def get_id_by_name(self, username): def get_id_by_name(self, username):
collaborator = self.db.execute( collaborator = self.db.execute(
select(Collaborator).where(Collaborator.name == username)).scalar() select(Collaborator).where(Collaborator.name == username)).scalar()
if not collaborator:
return None
return collaborator.id return collaborator.id
def get_by_team_id(self, team_id): def get_by_team_id(self, team_id):
@@ -111,7 +131,7 @@ class CollaboratorTools:
ret = self.db.execute( ret = self.db.execute(
select(Collaborator).where(Collaborator.name == username)).scalar() select(Collaborator).where(Collaborator.name == username)).scalar()
if ret is None: if ret is None:
print({'message': "This username doesn't exist"}) self.view.display_error()
return ret return ret
else: else:
return ret.team_id return ret.team_id
@@ -190,10 +210,12 @@ class CustomerTools:
ret = self.db.execute( ret = self.db.execute(
select(Customer).where(Customer.commercial_id == my_id)).all() select(Customer).where(Customer.commercial_id == my_id)).all()
if ret is None: if ret is None:
print({'message': "No customer found"}) # print({'message': "No customer found"})
self.view.display_error()
return ret return ret
else: else:
print({'message': "No commercial with this id"}) # print({'message': "No commercial with this id"})
self.view.display_error()
return None return None
@@ -310,8 +332,10 @@ class EventTools:
""" """
result = self.db.execute( result = self.db.execute(
select(Event).filter_by(**{field: value})).all() select(Event).filter_by(**{field: value})).all()
print(field, value, result) if not result:
self.view.display_results(result) self.view.display_error()
else:
self.view.display_results(result)
def filter_owned(self, user_id): def filter_owned(self, user_id):
""" """

View File

@@ -135,5 +135,11 @@ class View:
def display_error(self): def display_error(self):
print("No object matches this query") print("No object matches this query")
def display_no_user(self):
print("This user doesn't exist")
def display_co_failed(self):
print("Connexion failed.")
def display_items(self): def display_items(self):
print() print()