def edit_staff(staff_id): # ensure staff exits staff = staff_q.is_found(staff_id) if not staff: flash("Staff member not found.") return redirect(url_for("staff.staff_index")) errors = [] # handle staff-client team deletions if request.method == "DELETE": status = 1 error_message = "Failed to delete client from staff's team." # delete team id if exists team_id = util.safe_int(request.json.get("staff_cli_team_id")) if team_id and staff_q.del_team(team_id): status = 0 # flash message on error if status != 0: flash(error_message) return json.dumps({"status": status}) # handle updates to staff information if request.method == "POST": # update staff data staff_data = util.parse_staff_data(request.form) errors.extend(staff_val.validate_staff(staff_data, True)) if not errors: errors.extend(staff_q.update(staff_data, staff_id)) # get arbitary number of client initials client_count = util.safe_int(request.form.get("client_count")) client_initials = [] for i in range(client_count): client_initials.append(request.form.get("client_initials" + str(i))) # create new staff-client teams errors.extend(staff_q.create_teams(staff_id, client_initials)) if not errors: flash("All Staff Information Updated.") # render page staff = staff_q.get_details(staff_id) clients = staff_q.get_clients(staff_id) supervisors = superv_q.get_all_names() return render_template( "staff/edit_staff.html", clients=clients, supervisors=supervisors, staff=staff, errors=errors, )
def add_staff(): errors = [] staff = util.parse_staff_data(request.form) client_count = 2 if request.method == "POST": # get arbitrary clients data client_count = util.safe_int(request.form.get("client_count", 0)) for i in range(client_count): client = request.form.get("client_initials" + str(i)) if client and client not in staff["clients"]: staff["clients"].append(client) # validate data errors = staff_val.validate_staff(staff) if not errors: staff_q.create(staff) flash("Staff Member Added") else: flash("Staff Creation Failed") # render page clients = client_q.get_all_initials() supervisors = superv_q.get_all_names() return render_template("staff/add_staff.html", staff=staff, errors=errors, client_count=client_count, clients=clients, supervisors=supervisors)
def check_color(value: int) -> list: """ Validates a new staff member's category value """ errors = [] value = util.safe_int(value) if value not in [1, 2, 3]: errors.append("Invalid category data.") return errors
def check_tier(value: int) -> list: """ Validates a new staff member's tier value """ errors = [] value = util.safe_int(value) if value not in [1, 2, 3]: errors.append("Invalid Tier data.") return errors
def check_supervisor(supervisor_id: int) -> list: """ Validates a supervisor's id number """ errors = [] supervisor_id = util.safe_int(supervisor_id) if not supervisor_id or supervisor_id < 1: errors.append("Invalid supervisor data") else: if not supervisor_q.is_found(supervisor_id): errors.append(f"Supervisor with id: {supervisor_id} not found.") return errors
def delete_staff(): staff_id = util.safe_int(request.form.get("staff_id")) # validate staff_id error = staff_val.check_staff_id(staff_id) if error: flash(error) return redirect(url_for("staff.staff_index")) # delete all related entries from the database db = database.connect() db.execute("DELETE FROM staff WHERE staff_id = ?", (staff_id, )) db.commit() flash("Staff Member Deleted.") return redirect(url_for("staff.staff_index"))
def accounts(): """ Renders all user accounts. Logged in user and admin privileges required. """ page = util.safe_int(request.args.get("page"), 0) users = user_q.get_all(page) count = len(users) new_key = util.generate_new_pw(25) # remove 11th row if count > 10: del users[10] session["key"] = new_key return render_template("user/all_accounts.html", users=users, page=page, count=count, new_key=new_key)
def staff_index(): # prepare variables page = util.safe_int(request.args.get("page"), 0) staff = staff_q.get_all(page) count = len(staff) # remove 11th row if count > 10: del staff[10] # check for no staff if not staff: flash("No Staff Found") staff = [] return render_template( "staff/staff_index.html", staff=staff, page=page, count=count, )
def test_safe_int(self): # ensure no exceptions are raised self.assertIsNone(util.safe_int(None)) self.assertIsNone(util.safe_int(False)) self.assertIsNone(util.safe_int(True)) self.assertIsNone(util.safe_int("")) self.assertIsNone(util.safe_int([1])) self.assertIsNone(util.safe_int("hello")) self.assertIsNone(util.safe_int("1234five")) self.assertIsNone(util.safe_int("1.5")) # test general functionality self.assertEqual(util.safe_int(None, 7), 7) self.assertEqual(util.safe_int("fire", "Error"), "Error") self.assertEqual(util.safe_int("1"), 1) self.assertEqual(util.safe_int(1), 1) self.assertEqual(util.safe_int(1.9), 1) self.assertEqual(util.safe_int("12940"), 12940)