158 lines
6.2 KiB
Python
158 lines
6.2 KiB
Python
|
|
from models.database import Database
|
|
from models.user import User
|
|
from services.state import State
|
|
from services.log import LogService
|
|
from services.search import Search
|
|
from ui.input_menu import InputMenu, Validator
|
|
from ui.selection_menu import SelectionMenu
|
|
|
|
|
|
class AdvisorMenu:
|
|
@staticmethod
|
|
def search_advisor(browse_mode):
|
|
form = InputMenu("Search for Advisors")
|
|
form.add_option("QUERY", "Query", "STR", None, 1, 250, None)
|
|
form.do_input()
|
|
query = form.get_value("QUERY")
|
|
|
|
users = Search.search_advisors(query)
|
|
|
|
if len(users) == 0:
|
|
input(f"No Advisors found with query: {query}. Press any key to continue.")
|
|
return None
|
|
|
|
for index, user in enumerate(users):
|
|
print(f"/--[{index}]--------------")
|
|
print(f"| Username: {user.username}")
|
|
print(f"| Firstname: {user.firstname}")
|
|
print(f"| Lastname: {user.lastname}")
|
|
print(f"| Address: {user.lastname}")
|
|
print(f"| Zipcode: {user.zipcode}")
|
|
print(f"| City: {user.city_id}")
|
|
print(f"| Email: {user.email}")
|
|
print(f"| Phone: {user.phone}")
|
|
print(f"\--------------------\n")
|
|
|
|
if browse_mode:
|
|
input(f"\nPress any key to continue.")
|
|
return None
|
|
|
|
select_form = InputMenu("Select Advisor by index")
|
|
select_form.add_option("INDEX", "Index", "INT", None, 0, len(users) - 1, None)
|
|
select_form.do_input()
|
|
|
|
return users[select_form.get_value("INDEX")]
|
|
|
|
@staticmethod
|
|
def select_city():
|
|
menu = SelectionMenu("Select City")
|
|
|
|
cities = Search.get_all_cites()
|
|
for city in cities:
|
|
menu.add_option(city.name, city.id)
|
|
|
|
option = menu.display().input_option()
|
|
return option
|
|
|
|
@staticmethod
|
|
def add_advisor():
|
|
form = InputMenu("Add new Advisor")
|
|
form.add_option("USERNAME", "Username", "STR", None, 1, 250, Validator.check_username)
|
|
form.add_option("FIRSTNAME", "Firstname", "STR", None, 1, 250, None)
|
|
form.add_option("LASTNAME", "Lastname", "STR", None, 1, 250, None)
|
|
form.add_option("ADDRESS", "Address", "STR", None, 1, 250, None)
|
|
form.add_option("ZIPCODE", "Zipcode", "STR", None, 6, 6, None)
|
|
form.add_option("EMAIL", "Email", "STR", None, 1, 250, Validator.check_email)
|
|
form.add_option("PHONE", "Phone (+31-6)", "STR", None, 8, 8, None)
|
|
form.add_option("PASSWORD", "Password", "STR", None, 1, 255, Validator.check_password)
|
|
form.do_input()
|
|
|
|
city_id = AdvisorMenu.select_city()
|
|
|
|
new_user = User(Database.connection,
|
|
None,
|
|
form.get_value("USERNAME"),
|
|
form.get_value("FIRSTNAME"),
|
|
form.get_value("LASTNAME"),
|
|
form.get_value("ADDRESS"),
|
|
form.get_value("ZIPCODE"),
|
|
city_id,
|
|
form.get_value("EMAIL"),
|
|
"+31-6" + form.get_value("PHONE"),
|
|
form.get_value("PASSWORD"),
|
|
"ADVISOR"
|
|
)
|
|
|
|
new_user.save()
|
|
LogService.create_log(State.CURRENT_USER.username, "Created advisor", f"Advisor: {new_user.username}", True)
|
|
input("Added new Advisor! Press any key to return.")
|
|
|
|
@staticmethod
|
|
def edit_advisor():
|
|
user = AdvisorMenu.search_advisor(False)
|
|
|
|
edit_form = InputMenu("Edit Advisor (Leave fields empty to not change)")
|
|
edit_form.add_option("USERNAME", "Username", "STR", user.username, 1, 250, Validator.check_username)
|
|
edit_form.add_option("FIRSTNAME", "Firstname", "STR", user.firstname, 1, 250, None)
|
|
edit_form.add_option("LASTNAME", "Lastname", "STR", user.lastname, 1, 250, None)
|
|
edit_form.add_option("ADDRESS", "Address", "STR", user.address, 1, 250, None)
|
|
edit_form.add_option("ZIPCODE", "Zipcode", "STR", user.zipcode, 6, 6, None)
|
|
edit_form.add_option("EMAIL", "Email", "STR", user.email, 1, 250, Validator.check_email)
|
|
edit_form.add_option("PHONE", "Phone (+31-6)", "STR", user.phone, 8, 8, None)
|
|
edit_form.do_input()
|
|
|
|
select_menu = SelectionMenu("Do want to change the city?")
|
|
select_menu.add_option("No", False).add_option("Yes", True).display()
|
|
change_city = select_menu.input_option()
|
|
if change_city:
|
|
user.city_id = AdvisorMenu.select_city()
|
|
|
|
user.username = edit_form.get_value("USERNAME")
|
|
user.firstname = edit_form.get_value("FIRSTNAME")
|
|
user.lastname = edit_form.get_value("LASTNAME")
|
|
user.address = edit_form.get_value("ADDRESS")
|
|
user.zipcode = edit_form.get_value("ZIPCODE")
|
|
user.email = edit_form.get_value("EMAIL")
|
|
user.phone = edit_form.get_value("PHONE")
|
|
|
|
user.update()
|
|
LogService.create_log(State.CURRENT_USER.username, "Updated advisor", f"Advisor: {user.username}", True)
|
|
input("Updated Advisor! Press any key to return.")
|
|
|
|
@staticmethod
|
|
def update_password_advisor():
|
|
user = AdvisorMenu.search_advisor(False)
|
|
if user == None:
|
|
input("Advisor not found. Press any key to continue.")
|
|
return
|
|
|
|
form = InputMenu("Update password")
|
|
form.add_option("PASSWORD", "Password", "STR", None, 1, 255, Validator.check_password)
|
|
form.do_input()
|
|
user.password = form.get_value("PASSWORD")
|
|
user.update()
|
|
|
|
LogService.create_log(State.CURRENT_USER.username, "Updated password of advisor", f"Advisor: {user.username}", True)
|
|
input("Updated Advisor Password! Press any key to return.")
|
|
|
|
@staticmethod
|
|
def remove_advisor():
|
|
user = AdvisorMenu.search_advisor(False)
|
|
if user == None:
|
|
input("Advisor not found. Press any key to continue.")
|
|
return
|
|
|
|
confirm = SelectionMenu("Do you want to delete selected Advisor?")
|
|
confirm.add_option("Yes", True).add_option("No", False).display()
|
|
option = confirm.input_option()
|
|
|
|
if option == True:
|
|
user.delete()
|
|
LogService.create_log(State.CURRENT_USER.username, "Deleted advisor", f"Advisor: {user.username}", True)
|
|
input("Deleted Advisor! Press any key to return.")
|
|
|
|
@staticmethod
|
|
def browse_advisor():
|
|
AdvisorMenu.search_member(True)
|