def read_file(file_name):
    with open(file_name) as f:
        line = f.readline()
        line = f.readline()
        while line:
            lst = line.split(';')
            ClassName = lst[0]
            TotalStudents = lst[1]
            StudentRegNo = lst[2]
            StudentName = lst[3]
            StudentGender = lst[4]
            StudentSubjectCombi = lst[5]
            SubjectName = lst[6]
            Description = lst[7]
            SubjectGrade = lst[8][:-1] #because of "\n" at the back
            c1 = Class(ClassName, TotalStudents)
            st1 = Student(StudentName, StudentRegNo, ClassName, StudentSubjectCombi, StudentGender, AllSubjectGrades = '')
            sr1 = StudentRecords(StudentName, SubjectGrade, SubjectName)
            sb1 = Subject(SubjectName, Description)
            sa1 = SeatingArrangement(StudentName, CannotSeatNextTo= '', SeatInFront= False,WeakSubjects='',StrongSubjects='', ClassLst = '', SeatByGrades= '' , RowNo = 0, ColumnNo=0)
            execute_sql(c1.create_new_record())
            execute_sql(st1.create_new_record())
            execute_sql(sr1.create_new_record())
            execute_sql(sb1.create_new_record())
            execute_sql(sa1.create_new_record())
            line = f.readline()
    #f.close()

#read_file('result_data.csv')
def delete_student_record():
    student_name = request.form.get('delete')
    student_details = execute_sql("SELECT * FROM Student WHERE StudentName = '{}'".format(student_name))[0]
    StudentName, StudentRegNo, ClassName, StudentSubjectCombi, StudentGender, AllSubjectGrades = student_details
    delete_student_details = Student(StudentName, StudentRegNo, ClassName, StudentSubjectCombi, StudentGender, AllSubjectGrades)

    student_records = execute_sql("SELECT * FROM StudentRecords WHERE StudentName = '{}'".format(student_name))

    seating_arrangement_record = execute_sql("SELECT * FROM SeatingArrangement WHERE StudentName = '{}'".format(student_name))[0]
    StudentName, CannotSeatNextTo, SeatInFront, WeakSubjects, StrongSubjects, ClassLst, SeatByGrades, RowNo, ColumnNo = seating_arrangement_record
    delete_seating_arrangement_record = SeatingArrangement(StudentName, CannotSeatNextTo, SeatInFront, WeakSubjects, StrongSubjects, ClassLst, SeatByGrades, RowNo, ColumnNo)

    #Delete the database
    execute_sql(delete_student_details.delete_record())
    execute_sql(delete_seating_arrangement_record.delete_record())
    for i in range(len(student_records)):
        StudentName, SubjectGrade, SubjectName = student_records[i]
        delete_student_record = StudentRecords(StudentName, SubjectGrade, SubjectName)
        execute_sql(delete_student_record.delete_record())

    #remove class if the class no longer has any students in it
    students_from_class = execute_sql("SELECT * FROM Student WHERE ClassName == '{}'".format(ClassName))
    if students_from_class == []:
        class_details = execute_sql("SELECT * FROM Class WHERE ClassName == '{}'".format(ClassName))[0]
        ClassName, TotalStudents = class_details
        delete_class = Class(ClassName, TotalStudents)
        execute_sql(delete_class.delete_record())

    #Return to the main page
    return redirect(url_for("display_all_student_records"))
def create_student_record():
    if request.method == 'POST':
        #Create Class object
        new_class = Class(request.form.get('ClassName'), '')
        execute_sql(new_class.create_new_record())

        #Create Student object
        new_student_details = Student(
            request.form.get('StudentName').strip(),
            request.form.get('StudentRegNo').strip(),
            request.form.get('ClassName').strip(),
            request.form.get('StudentSubjectCombi').strip(),
            request.form.get('StudentGender').strip(),
            request.form.get('AllSubjectGrades').strip())
        execute_sql(new_student_details.create_new_record())

        #Create StudentRecords object
        StudentSubjectCombi = request.form.get(
            'StudentSubjectCombi').strip().split(' ')
        AllSubjectGrades = request.form.get('AllSubjectGrades').strip().split(
            ' ')
        for i in range(len(StudentSubjectCombi)):
            new_student_record = StudentRecords(
                request.form.get('StudentName').strip(), AllSubjectGrades[i],
                StudentSubjectCombi[i])
            execute_sql(new_student_record.create_new_record())

        subject_lst = execute_sql('SELECT * FROM Subject')
        subject_lst = list(map(lambda tuple: tuple[0], subject_lst))
        for subject in StudentSubjectCombi:
            if subject not in subject_lst:
                new_subject = Subject(subject, subject_description(subject))
                execute_sql(new_subject.create_new_record())

        #Create Seating Arrangement object
        new_seating_arrangement = SeatingArrangement(
            request.form.get('StudentName'),
            CannotSeatNextTo='',
            SeatInFront=False,
            WeakSubjects='',
            StrongSubjects='',
            ClassLst='',
            SeatByGrades='',
            RowNo=0,
            ColumnNo=0)
        execute_sql(new_seating_arrangement.create_new_record())

        return redirect(url_for('display_all_student_records'))

    else:
        return render_template('create_student_record.html')
Ejemplo n.º 4
0
def create_table():
    execute_sql(Class.create_table())
    execute_sql(Student.create_table())
    execute_sql(StudentRecords.create_table())
    execute_sql(Subject.create_table())
    execute_sql(SeatingArrangement.create_table())
    execute_sql(User.create_table())
    execute_sql(CurrentUser.create_table())
    execute_sql(SavedSeatArr.create_table())
    execute_sql(Comment.create_table())
    execute_sql(UserInfo.create_table())
def delete_student_record(student_name):
    student_details = execute_sql(
        "SELECT * FROM Student WHERE StudentName = '{}'".format(
            student_name))[0]
    StudentName, StudentRegNo, ClassName, StudentSubjectCombi, StudentGender, AllSubjectGrades = student_details
    delete_student_details = Student(StudentName, StudentRegNo, ClassName,
                                     StudentSubjectCombi, StudentGender,
                                     AllSubjectGrades)

    student_records = execute_sql(
        "SELECT * FROM StudentRecords WHERE StudentName = '{}'".format(
            student_name))

    seating_arrangement_record = execute_sql(
        "SELECT * FROM SeatingArrangement WHERE StudentName = '{}'".format(
            student_name))[0]
    StudentName, CannotSeatNextTo, SeatInFront, WeakSubjects, StrongSubjects, ClassLst, SeatByGrades, RowNo, ColumnNo = seating_arrangement_record
    delete_seating_arrangement_record = SeatingArrangement(
        StudentName, CannotSeatNextTo, SeatInFront, WeakSubjects,
        StrongSubjects, ClassLst, SeatByGrades, RowNo, ColumnNo)

    if request.method == 'POST':
        #Delete the database
        execute_sql(delete_student_details.delete_record())
        execute_sql(delete_seating_arrangement_record.delete_record())
        for i in range(len(student_records)):
            StudentName, SubjectGrade, SubjectName = student_records[i]
            delete_student_record = StudentRecords(StudentName, SubjectGrade,
                                                   SubjectName)
            execute_sql(delete_student_record.delete_record())

        #Return to the main page
        return redirect(url_for("display_all_student_records"))

    else:
        return render_template('delete_student_record.html',
                               delete_student_details=delete_student_details)
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 generate_seating_arrangement():
    SeatInFront_lst = []
    not_SeatInFront_lst = [
    ]  #for students who do not need to seat in front, separate lst to randomly shuffle these students and then append it to SeatingArrangement_lst
    CannotSeatNextTo_lst = []
    SeatingArrangement_lst = []
    result = []

    student_lst = execute_sql(
        "SELECT * FROM SeatingArrangement WHERE ClassLst != ''")
    StudentName_lst = list(map(lambda x: x[0], student_lst))
    ClassSize = len(StudentName_lst)

    StudentName, CannotSeatNextTo, SeatInFront, WeakSubjects, StrongSubjects, ClassLst, SeatByGrades, RowNo, ColumnNo = student_lst[
        0]
    if RowNo == 0:
        set_student_details()

    student_lst = execute_sql(
        "SELECT * FROM SeatingArrangement WHERE ClassLst != ''")
    StudentName, CannotSeatNextTo, SeatInFront, WeakSubjects, StrongSubjects, ClassLst, SeatByGrades, RowNo, ColumnNo = student_lst[
        0]

    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)
        if student.get_CannotSeatNextTo() != '' and [
                student.get_StudentName(),
                student.get_CannotSeatNextTo()
        ][::
          -1] not in SeatInFront_lst:  #Assuming only one person cannot seat to that student
            CannotSeatNextTo_lst.append(
                [student.get_StudentName(),
                 student.get_CannotSeatNextTo()])
        if student.get_SeatInFront() == True:
            SeatInFront_lst.append(student.get_StudentName())

    if SeatByGrades == 'Yes':
        grade_lst = sort_by_grades(student_lst)
        #find pairs in cannotseatnextto_lst  that are also in grade_lst as those pairs can't seat next to each other
        for i in grade_lst:
            if i in CannotSeatNextTo_lst:
                grade_lst.remove(i)
            if i[::-1] in CannotSeatNextTo_lst:
                grade_lst.remove(i[::-1])

        #find pairs in grade_lst that are also in SeatInFrontLst to arrange the pairs in the front by appending confirmed pairs into SeatingArrangement_lst
        for a in range(len(SeatInFront_lst)):
            for b in range(a + 1, len(SeatInFront_lst)):
                temp = [SeatInFront_lst[a], SeatInFront_lst[b]]
                if temp in grade_lst and temp[0] in StudentName_lst and temp[
                        1] in StudentName_lst:
                    grade_lst.remove(temp)
                    SeatingArrangement_lst.append(
                        temp)  #added into confirmed seating arrangement
                    StudentName_lst.remove(temp[0])
                    StudentName_lst.remove(temp[1])

                if temp[::-1] in grade_lst and temp[
                        0] in StudentName_lst and temp[1] in StudentName_lst:
                    grade_lst.remove(temp)
                    SeatingArrangement_lst.append(
                        temp[::-1])  #added into confirmed seating arrangement
                    StudentName_lst.remove(temp[0])
                    StudentName_lst.remove(temp[1])

        # shuffle lst so that there will be more varieties of seating arrangement
        random.shuffle(CannotSeatNextTo_lst)
        random.shuffle(SeatInFront_lst)
        random.shuffle(grade_lst)

        # pair up those who are suppose to seat in front, seating in front prioritised to seating with those that can help with grades
        for s in range(len(SeatInFront_lst)):
            for r in range(s + 1, len(SeatInFront_lst)):
                temp = [SeatInFront_lst[s], SeatInFront_lst[r]]
                if temp[0] in StudentName_lst and temp[1] in StudentName_lst:
                    SeatingArrangement_lst.append(temp)
                    StudentName_lst.remove(temp[0])
                    StudentName_lst.remove(temp[1])

        # all students supposed to seat in front are added
        random.shuffle(SeatingArrangement_lst
                       )  # so that pairs in grade_lst won't always be in front

        # choose pairs from grade_lst
        for c in grade_lst:
            if c[0] in StudentName_lst and c[1] in StudentName_lst:
                not_SeatInFront_lst.append(c)
                StudentName_lst.remove(c[0])
                StudentName_lst.remove(c[1])

    else:
        # shuffle lst so that there will be more varieties of seating arrangement
        random.shuffle(CannotSeatNextTo_lst)
        random.shuffle(SeatInFront_lst)

        # pair up those who are suppose to seat in front
        for s in range(len(SeatInFront_lst)):
            for r in range(s + 1, len(SeatInFront_lst)):
                temp = [SeatInFront_lst[s], SeatInFront_lst[r]]
                if temp[0] in StudentName_lst and temp[1] in StudentName_lst:
                    SeatingArrangement_lst.append(temp)
                    StudentName_lst.remove(temp[0])
                    StudentName_lst.remove(temp[1])

    # pairing up any remaining students
    random.shuffle(StudentName_lst)
    while len(
            StudentName_lst
    ) > 1:  # possible to have 1 student left if no pair for that student
        temp = [StudentName_lst[0], StudentName_lst[1]]
        if temp not in CannotSeatNextTo_lst and temp[::
                                                     -1] not in CannotSeatNextTo_lst:  #cannot be paired up if not suppose to seat next to each other
            not_SeatInFront_lst.append(temp)
            StudentName_lst.remove(temp[0])
            StudentName_lst.remove(temp[1])

    random.shuffle(not_SeatInFront_lst)
    SeatingArrangement_lst.extend(not_SeatInFront_lst)
    if StudentName_lst != []:
        SeatingArrangement_lst.append([StudentName_lst[0]])

    count = 0
    temp = []

    for row in range(RowNo):
        for column in range(ColumnNo):
            if count < ClassSize:
                temp.append(SeatingArrangement_lst[row * ColumnNo + column])
                if len(SeatingArrangement_lst[row * ColumnNo + column]) == 2:
                    count += 2
                if len(SeatingArrangement_lst[row * ColumnNo + column]) == 1:
                    count += 1
        result.append(temp)
        temp = []

    return render_template('generate_seating_arrangement.html',
                           SeatingArrangement_lst=result,
                           RowNo=RowNo,
                           ColumnNo=ColumnNo,
                           RowNoRange=range(RowNo),
                           ColumnNoRange=range(ColumnNo),
                           ClassSize=ClassSize)
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)
def display_all_student_records():
    classes = execute_sql("SELECT * FROM Class")
    students = execute_sql("SELECT * FROM Student")
    records = execute_sql("SELECT * FROM StudentRecords")
    subjects = execute_sql("SELECT * FROM Subject")

    for x in range(
            len(classes) - 1
    ):  #to sort classes in order, so that when displayed will be according to class.
        smallest = x
        for y in range(x + 1, len(classes)):
            if classes[y][0] < classes[smallest][0]:
                smallest = y
        if smallest != x:
            classes[smallest], classes[x] = classes[x], classes[smallest]

    for i in range(
            len(students) - 1
    ):  #to sort reg.no in order, so that when displayed will be according to reg.no.
        smallest = i
        for j in range(i + 1, len(students)):
            if students[j][1] < students[smallest][1]:
                smallest = j
        if smallest != i:
            students[smallest], students[i] = students[i], students[smallest]

    classes_oop = list(map(lambda a: Class(a[0], a[1]), classes))
    students_oop = list(
        map(lambda b: Student(b[0], b[1], b[2], b[3], b[4], b[5]), students))
    records_oop = list(map(lambda c: StudentRecords(c[0], c[1], c[2]),
                           records))
    subjects_oop = list(map(lambda d: Subject(d[0], d[1]), subjects))

    # collating all grades and adding it as attribute to Students as AllSubjectGrades
    for student in students_oop:
        if student.get_AllSubjectGrades() == '':
            temp = ''
            index = 0
            while len(temp.split(' ')) - 1 != len(
                    student.get_StudentSubjectCombi().split(' ')):
                for record in records:
                    if student.get_StudentName(
                    ) == record[0] and student.get_StudentSubjectCombi().split(
                            ' ')[index] == record[2]:
                        temp += '{} '.format(record[1])
                index += 1
            temp = temp[:-1]
            student.set_AllSubjectGrades(temp)
            execute_sql(student.update_record())

    # Adding to WeakSubjects and StrongSubjects for seating arrangement
    # done in display so that after editing new strong subjects will be added and some removed as well
    # same for weak subjects
    for student in students_oop:
        student_seating_arrangement = execute_sql(
            'SELECT * FROM SeatingArrangement WHERE StudentName = "{}"'.format(
                student.get_StudentName()))[0]
        StudentName, CannotSeatNextTo, SeatInFront, WeakSubjects, StrongSubjects, ClassLst, SeatByGrades, RowNo, ColumnNo = student_seating_arrangement
        student_seating_arrangement = SeatingArrangement(
            StudentName, CannotSeatNextTo, SeatInFront, WeakSubjects,
            StrongSubjects, ClassLst, SeatByGrades, RowNo, ColumnNo)
        StrongSubjects = ''
        WeakSubjects = ''
        for i in range(len(student.get_AllSubjectGrades().split(' '))):
            if student.get_AllSubjectGrades().split(' ')[i] < 'C':  #'A', 'B'
                StrongSubjects += student.get_StudentSubjectCombi().split(
                    ' ')[i] + ' '
            elif student.get_AllSubjectGrades().split(' ')[i] > 'D':
                WeakSubjects += student.get_StudentSubjectCombi().split(
                    ' ')[i] + ' '
        student_seating_arrangement.set_StrongSubjects(StrongSubjects[:-1])
        student_seating_arrangement.set_WeakSubjects(WeakSubjects[:-1])
        execute_sql(student_seating_arrangement.update_record())

    reset_seating_arrangement()

    return render_template("display_all_records.html",
                           classes=classes_oop,
                           students=students_oop,
                           records=records_oop,
                           subjects=subjects_oop)
Ejemplo n.º 10
0
def read_file(file_name):
    with open(file_name) as f:
        line = f.readline()
        line = f.readline()
        while line:
            lst = line.split(';')
            ClassName = lst[0]
            TotalStudents = lst[1]
            StudentRegNo = lst[2]
            StudentName = lst[3]
            StudentGender = lst[4]
            StudentSubjectCombi = lst[5]
            SubjectName = lst[6]
            Description = lst[7]
            SubjectGrade = lst[8]
            SeatArrName = lst[9]
            CommentIDs = lst[10]
            CommentID = lst[11]
            CommentText = lst[12]
            CommentDatetime = lst[13][:-1]  #because of "\n" at the back
            c1 = Class(ClassName, TotalStudents)
            st1 = Student(StudentName,
                          StudentRegNo,
                          ClassName,
                          StudentSubjectCombi,
                          StudentGender,
                          AllSubjectGrades='')
            sr1 = StudentRecords(StudentName, SubjectGrade, SubjectName)
            sb1 = Subject(SubjectName, Description)
            sa1 = SeatingArrangement(StudentName,
                                     CannotSeatNextTo='',
                                     SeatInFront=False,
                                     WeakSubjects='',
                                     StrongSubjects='',
                                     ClassLst='',
                                     SeatByGrades='',
                                     RowNo=0,
                                     ColumnNo=0)
            user1 = User(UserName='******', Password='')
            currentuser1 = CurrentUser(UserName='******')
            comment = Comment(SeatArrName,
                              int(CommentID),
                              CommentText,
                              CommentDatetime,
                              UserName="******")
            ssr1 = SavedSeatArr(UserName='******',
                                SeatArrName=SeatArrName,
                                SeatArrSeq="",
                                RowNo=0,
                                ColumnNo=0,
                                CommentIDs=CommentIDs)
            #TODO Username does not have to be class specific as CP will add in student info by themselves, most important is to do a back-end  validation to check if username is taken alr or not
            #TODO set user to an example user first, by right upon creating an account on login, user info will be created and added into User table
            #TODO set current user to nth by default until someone logins, then current user will become that username, must always reset current user
            execute_sql(c1.create_new_record())
            execute_sql(st1.create_new_record())
            execute_sql(sr1.create_new_record())
            execute_sql(sb1.create_new_record())
            execute_sql(sa1.create_new_record())
            execute_sql(user1.create_new_record())
            execute_sql(currentuser1.create_new_record())
            execute_sql(comment.create_new_record())
            execute_sql(ssr1.create_new_record())
            line = f.readline()
    f.close()
def create_table():
    execute_sql(Class.create_table())
    execute_sql(Student.create_table())
    execute_sql(StudentRecords.create_table())
    execute_sql(Subject.create_table())
    execute_sql(SeatingArrangement.create_table())
def create_student_record():

    if request.method == 'POST':
        error = False
        if request.form['ClassName'].strip() == "":
            error = "Invalid Class, Please write something for Class..."

        elif len(request.form['ClassName'].strip()) != 2:
            error = "Invalid Class, Class should consist of one number and one letter (e.g. 6M)"

        elif len(request.form['ClassName'].strip()) == 2:
            if not (request.form['ClassName'].strip()[0].isdigit() and request.form['ClassName'][1].strip().isalpha() and
                    ord(request.form['ClassName'][1].strip()) >= 65 and ord(request.form['ClassName'][1].strip()) <= 90 ):
                error = "Invalid Class, Class should consist of one number and one capital letter (e.g. 6M)"
                return render_template('create_student_record.html', SubjectAbbrev=SubjectAbbrev,
                                    SubjectFull=SubjectFull, error=error)

        existing_students = execute_sql("SELECT * FROM Student WHERE ClassName = '{}'".format(request.form['ClassName'].strip()))
        existing_regno = list(map(lambda tuple: tuple[1], existing_students))

        if request.form['StudentName'].strip() == "":
            error = "Invalid Name, Please write something for Name..."
            return render_template('create_student_record.html', SubjectAbbrev=SubjectAbbrev,
                                    SubjectFull=SubjectFull, error=error)

        elif hasNumbers(request.form['StudentName']):
            error = "Invalid Name, Name should not contain numbers"
            return render_template('create_student_record.html', SubjectAbbrev=SubjectAbbrev,
                                    SubjectFull=SubjectFull, error=error)

        elif request.form['StudentRegNo'].strip() == "":
            error = "Invalid Register No., Please write something for Register No..."
            return render_template('create_student_record.html', SubjectAbbrev=SubjectAbbrev,
                                    SubjectFull=SubjectFull, error=error)

        elif not request.form['StudentRegNo'].strip().isdigit():
            error = "Invalid Register No., Register No. should only consist of numbers"
            return render_template('create_student_record.html', SubjectAbbrev=SubjectAbbrev,
                                    SubjectFull=SubjectFull, error=error)

        if request.form['StudentRegNo'].strip().isdigit():
            if int(request.form['StudentRegNo'].strip()) in existing_regno:
                error = "Register No. has already been used, please key in another register no."
                return render_template('create_student_record.html', SubjectAbbrev=SubjectAbbrev,
                                    SubjectFull=SubjectFull, error=error)

        if request.form['StudentGender'].strip() == "":
            error = "Invalid Gender, Please write something for Gender..."

        elif request.form['StudentGender'].strip() not in ["F","M"]:
            error = "Invalid Gender, Gender should only be M or F"

        elif request.form['StudentSubjectCombi'].strip() == "":
            error = "Invalid Subject Combination, Please write something for Subject Combination..."

        for SubCom in list(request.form['StudentSubjectCombi'].split(' ')):
            if SubCom[:3:] not in SubjectAbbrev:
                error = 'Invalid Subject Combination, An impossible subject was entered'

        for SubGrade in (list(filter(lambda x: x != ' ', request.form['AllSubjectGrades'].strip()))):
            if SubGrade not in ['A', 'B', 'C', 'D', 'E', 'S', 'U']:
                print(SubGrade, 'Subgrade')
                error = 'Invalid Grades, An impossible grade was entered'

        if request.form['AllSubjectGrades'].strip() == "":
            error = "Invalid Grades, Please write something for Grades..."

        elif len(request.form['StudentSubjectCombi'].strip().split(" ")) != len(request.form['AllSubjectGrades'].strip().split(" ")):
            error = "Number of grades should correspond to number of subjects keyed in"

        if error != False:
            return render_template('create_student_record.html', SubjectAbbrev=SubjectAbbrev,
                                    SubjectFull=SubjectFull, error=error)

        #Create Class object
        new_class = Class(request.form.get('ClassName').strip(),'')
        execute_sql(new_class.create_new_record())

        #Create Student object
        new_student_details = Student(request.form.get('StudentName').strip(), request.form.get('StudentRegNo').strip(), request.form.get('ClassName').strip(),request.form.get('StudentSubjectCombi').strip(), request.form.get('StudentGender').strip(), request.form.get('AllSubjectGrades').strip())
        execute_sql(new_student_details.create_new_record())

        #Create StudentRecords object
        StudentSubjectCombi = request.form.get('StudentSubjectCombi').strip().split(' ')
        AllSubjectGrades = request.form.get('AllSubjectGrades').strip().split(' ')
        for i in range(len(StudentSubjectCombi)):
            new_student_record = StudentRecords(request.form.get('StudentName').strip(), AllSubjectGrades[i], StudentSubjectCombi[i])
            execute_sql(new_student_record.create_new_record())

        subject_lst = execute_sql('SELECT * FROM Subject')
        subject_lst = list(map(lambda tuple: tuple[0],subject_lst))
        for subject in StudentSubjectCombi:
            if subject not in subject_lst:
                new_subject = Subject(subject, subject_description(subject))
                execute_sql(new_subject.create_new_record())

        #Create Seating Arrangement object
        new_seating_arrangement = SeatingArrangement(request.form.get('StudentName').strip(), CannotSeatNextTo = '', SeatInFront = False, WeakSubjects = '', StrongSubjects = '', ClassLst= '', SeatByGrades='', RowNo=0, ColumnNo=0)
        execute_sql(new_seating_arrangement.create_new_record())

        return redirect(url_for('display_all_student_records'))

    else:
        return render_template('create_student_record.html', SubjectAbbrev=SubjectAbbrev,
           SubjectFull=SubjectFull)