def edit_team():
    team_id = request.get_json()['id']
    new_team_name = request.get_json()['name']
    if request.get_json()['new_member'] != '' and (
            new_team_name == Team.get_team_by_id(team_id).get_name()
            or not Team.get_team_by_name(new_team_name)):
        new_member = request.get_json()['new_member']
        Team.get_team_by_id(team_id).set_name(new_team_name)
        student_id = int(''.join(filter(lambda x: x.isdigit(), new_member)))
        if Student.get_student_team_id(student_id):
            Team.get_team_by_id(team_id).relocate_member(student_id)
        else:
            Team.get_team_by_id(team_id).add_member(
                User.get_user_by_id(student_id))
        student_name = Student.get_user_by_id(student_id).get_name()
        edited_data = (new_team_name, student_name, student_id)
        edited_data_json = json.dumps(edited_data)
        return edited_data_json
    elif new_team_name == Team.get_team_by_id(
            team_id).get_name() or not Team.get_team_by_name(new_team_name):
        Team.get_team_by_id(team_id).set_name(new_team_name)
        edited_data = new_team_name
        edited_data_json = json.dumps(edited_data)
        return edited_data_json
    else:
        return ''
def get_team_by_id():
    team_id = request.get_json()
    students = User.get_user_list_by_role("student")
    students_for_json = []
    for student in students:
        students_for_json.append((student.get_id(), student.get_name()))
    team = Team.get_team_by_id(team_id)
    team_data = (students_for_json, team.get_name())
    team_data_json = json.dumps(team_data)
    return team_data_json
def team_edit(team_id):
    if request.method == "POST":
        if request.form['name']:
            team_name = request.form['name']
            Team.get_team_by_id(team_id).set_name(team_name)
        if request.form['student']:
            student_id = int(''.join(
                filter(lambda x: x.isdigit(), request.form['student'])))
            if Student.get_student_team_id(student_id):
                Team.get_team_by_id(team_id).relocate_member(student_id)
            else:
                Team.get_team_by_id(team_id).add_member(
                    User.get_user_by_id(student_id))
    return render_template("edit_team_form.html",
                           team=Team.get_team_by_id(team_id),
                           student_list=User.get_user_list_by_role('student'),
                           mainmenu=mainmenu)
def team_details(team_id):
    return render_template("team_details.html",
                           team=Team.get_team_by_id(team_id),
                           mainmenu=mainmenu)
def remove_member():
    team_id = request.get_json()['team_id']
    student_id = request.get_json()['member_id']
    Team.get_team_by_id(team_id).remove_member(User.get_user_by_id(student_id))
    return ''
def team_remove_student(team_id, student_id):
    Team.get_team_by_id(team_id).remove_member(User.get_user_by_id(student_id))
    return redirect(url_for('teams_ctrl.team_edit', team_id=team_id))