def reset_seating_arrangement():
    #reset seating arrangement object when go back to display, and various setting menus (set_seating_arrangement, class_seating_arrangement, special_class_seating_arrangement)
    student_lst = execute_sql("SELECT * FROM SeatingArrangement WHERE ClassLst != ''")
    for student in student_lst:
        StudentName, CannotSeatNextTo, SeatInFront, WeakSubjects, StrongSubjects, ClassLst, SeatByGrades, RowNo, ColumnNo = student
        student = SeatingArrangement(StudentName, CannotSeatNextTo, SeatInFront, WeakSubjects, StrongSubjects, ClassLst, SeatByGrades, RowNo, ColumnNo)
        student.set_CannotSeatNextTo('')
        student.set_SeatInFront(False)
        student.set_ClassLst('')
        student.set_SeatByGrades('')
        student.set_RowNo(0)
        student.set_ColumnNo(0)
        execute_sql(student.update_record())
def set_student_details():
    Subject = None
    lst = []

    if class_seating_arrangement() != None:
        ClassName = class_seating_arrangement()
        print(ClassName)
        lst = execute_sql(
            "SELECT * FROM Student WHERE ClassName = '{}'".format(ClassName))
        lst = list(map(lambda x: x[0],
                       lst))  #lst with all names of valid students
        print(lst)

    if special_class_seating_arrangement()[1] != None:
        ClassList, Subject = special_class_seating_arrangement()
        ClassList = ClassList.split(' ')
        lst = []
        for i in ClassList:
            temp = execute_sql(
                "SELECT * FROM Student WHERE ClassName = '{}'".format(i))
            for student in temp:
                if Subject in student[3].split(' '):
                    lst.append(student)
        lst = list(map(lambda x: x[0], lst))

    if lst != []:
        string = ''
        for i in lst:
            string += i + ','
        string = string[:-1]

        for i in lst:
            #To identify, whether students belong to class which seating arrangement is generated for
            #set._ClassLst to lst for students in that class and set the rest of the students' ClassLst to default = []

            #set._ClassLst to lst for students in this class
            temp = execute_sql(
                "SELECT * FROM SeatingArrangement WHERE StudentName = '{}'".
                format(i))[0]
            StudentName, CannotSeatNextTo, SeatInFront, WeakSubjects, StrongSubjects, ClassLst, SeatByGrades, RowNo, ColumnNo = temp
            new_classlst = SeatingArrangement(StudentName, CannotSeatNextTo,
                                              SeatInFront, WeakSubjects,
                                              StrongSubjects, ClassLst,
                                              SeatByGrades, RowNo, ColumnNo)
            new_classlst.set_ClassLst(string)
            execute_sql(new_classlst.update_record())

        #setting rest to default ClassLst = []
        student_lst = execute_sql(
            "SELECT * FROM SeatingArrangement WHERE ClassLst != '{}'".format(
                string))
        print(student_lst)
        student_lst = list(map(lambda x: x[0], student_lst))
        for i in student_lst:
            temp = execute_sql(
                "SELECT * FROM SeatingArrangement WHERE StudentName = '{}'".
                format(i))[0]
            StudentName, CannotSeatNextTo, SeatInFront, WeakSubjects, StrongSubjects, ClassLst, SeatByGrades, RowNo, ColumnNo = temp
            new_classlst = SeatingArrangement(StudentName, CannotSeatNextTo,
                                              SeatInFront, WeakSubjects,
                                              StrongSubjects, ClassLst,
                                              SeatByGrades, RowNo, ColumnNo)
            new_classlst.set_ClassLst('')
            execute_sql(new_classlst.update_record())

        return render_template('set_student_details.html',
                               Students=lst,
                               student_range=range(len(lst)),
                               range=range(5),
                               Subject=Subject)

    if request.method == 'POST':
        seatbygrades = request.form.get(
            'SeatByGrades')  # strong pupils will seat next to weak pupils
        rowno = request.form.get('RowNo')
        columnno = request.form.get('ColumnNo')
        print('set_student_details', seatbygrades, rowno, columnno)

        lst = execute_sql(
            "SELECT * FROM SeatingArrangement WHERE ClassLst != ''")
        for student in lst:
            StudentName, CannotSeatNextTo, SeatInFront, WeakSubjects, StrongSubjects, ClassLst, SeatByGrades, RowNo, ColumnNo = student
            student = SeatingArrangement(StudentName, CannotSeatNextTo,
                                         SeatInFront, WeakSubjects,
                                         StrongSubjects, ClassLst,
                                         SeatByGrades, RowNo, ColumnNo)
            if seatbygrades == 'Yes':
                student.set_SeatByGrades(seatbygrades)
            if rowno != 0:
                student.set_RowNo(rowno)
                student.set_ColumnNo(columnno)
            execute_sql(student.update_record())

        lst = list(map(lambda x: x[0], lst))  #To get valid names

        for i in range(len(lst)):
            StudentName = lst[i]
            SeatInFront = request.form.get('SeatInFront{}'.format(i))
            if SeatInFront != None:
                student = execute_sql(
                    'SELECT * FROM SeatingArrangement WHERE StudentName = "{}"'
                    .format(StudentName))[0]
                StudentName, CannotSeatNextTo, SeatInFront, WeakSubjects, StrongSubjects, ClassLst, SeatByGrades, RowNo, ColumnNo = student
                set_student = SeatingArrangement(StudentName, CannotSeatNextTo,
                                                 SeatInFront, WeakSubjects,
                                                 StrongSubjects, ClassLst,
                                                 SeatByGrades, RowNo, ColumnNo)
                set_student.set_SeatInFront(True)
                execute_sql(set_student.update_record())

            StudentName1 = request.form.get('StudentName1{}'.format(i))
            StudentName2 = request.form.get('StudentName2{}'.format(i))

            if StudentName1 != '' and StudentName1 != None:
                print(StudentName1)
                print(
                    execute_sql(
                        'SELECT * FROM SeatingArrangement WHERE StudentName = "{}"'
                        .format(StudentName1)))
                student1 = execute_sql(
                    'SELECT * FROM SeatingArrangement WHERE StudentName = "{}"'
                    .format(StudentName1))[0]
                StudentName, CannotSeatNextTo, SeatInFront, WeakSubjects, StrongSubjects, ClassLst, SeatByGrades, RowNo, ColumnNo = student1
                set_student = SeatingArrangement(StudentName, CannotSeatNextTo,
                                                 SeatInFront, WeakSubjects,
                                                 StrongSubjects, ClassLst,
                                                 SeatByGrades, RowNo, ColumnNo)
                set_student.set_CannotSeatNextTo(StudentName2)
                execute_sql(set_student.update_record())

                student2 = execute_sql(
                    'SELECT * FROM SeatingArrangement WHERE StudentName = "{}"'
                    .format(StudentName2))[0]
                StudentName, CannotSeatNextTo, SeatInFront, WeakSubjects, StrongSubjects, ClassLst, SeatByGrades, RowNo, ColumnNo = student2
                set_student = SeatingArrangement(StudentName, CannotSeatNextTo,
                                                 SeatInFront, WeakSubjects,
                                                 StrongSubjects, ClassLst,
                                                 SeatByGrades, RowNo, ColumnNo)
                set_student.set_CannotSeatNextTo(StudentName1)
                execute_sql(set_student.update_record())

    else:
        return render_template('set_student_details.html',
                               Students=lst,
                               student_range=range(len(lst)),
                               range=range(5),
                               Subject=Subject)