Student Management System (mysql)

Keywords: Python Session Linux

Theme: Student Management System

Demand:

User roles, lecturers, trainees, users can do different things according to their roles after login, as follows

Lecturer View:

  • Management class, can create a class, according to the qq number of students to join the class
  • A class record can be created for a given class. Note that one class record corresponds to several students'class records. That is to say, each class has a whole class. In order to record each student's academic performance, it is necessary to create a class record for each class. At the same time, it is necessary to create a class record for each student in this class.
  • Manual revision of grades one by one for students

Student View:

  • Submit homework
  • Check homework results
  • A student can belong to more than one class at the same time, just like registering for Linux or Python at the same time, so when submitting homework, you need to select the class first, and then choose the number of specific classes.
  • Additional: Students can view their own class rankings

Table structure:

Draw table structure first according to requirement

 

Program directory structure:

 

Table structure instance code:

 

from sqlalchemy import String,Column,Integer,ForeignKey,DATE,Table
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship
from conf.settings import engine
 
 
############Create a data table structure######################3
Base = declarative_base()
 
# Table of Corresponding Relations between Classes and Students
teacher_m2m_class = Table("teacher_m2m_class",Base.metadata,
                          Column("teacher_id", Integer, ForeignKey("teacher.teacher_id")),
                          Column("class_id", Integer, ForeignKey("class.class_id")),
                          )
 
# Table of Corresponding Relations between Classes and Students
class_m2m_student = Table("class_m2m_student",Base.metadata,
                          Column("class_id",Integer,ForeignKey("class.class_id")),
                          Column("stu_id", Integer, ForeignKey("student.stu_id")),
                          )
 
class Class_m2m_Lesson(Base):
    '''Class and Session Correspondence Table'''
    __tablename__ = "class_m2m_lesson"
    id =  Column(Integer, primary_key=True)
    class_id = Column(Integer,ForeignKey("class.class_id"))
    lesson_id = Column(Integer, ForeignKey("lesson.lesson_id"))
 
    classes = relationship("Class",backref="class_m2m_lessons")
    lessons = relationship("Lesson", backref="class_m2m_lessons")
 
    def __repr__(self):
        return "%s %s" % (self.classes,self.lessons)
 
class Study_record(Base):
    "Class record"
    __tablename__ = "study_record"
    id = Column(Integer,primary_key=True)
    class_m2m_lesson_id = Column(Integer,ForeignKey("class_m2m_lesson.id"))
    stu_id = Column(Integer, ForeignKey("student.stu_id"))
    status = Column(String(32),nullable=False)
    score = Column(Integer,nullable=True)
 
    class_m2m_lessons = relationship("Class_m2m_Lesson",backref="my_study_record")
    students = relationship("Student", backref="my_study_record")
 
    def __repr__(self):
       return  "\033[35;0m%s,%s,Status:%s],Results:%s]\33[0m"%(self.class_m2m_lessons,self.students,self.status,self.score)
 
class Teacher(Base):
    "lecturer"
    __tablename__ = "teacher"
    teacher_id = Column(Integer, primary_key=True)
    teacher_name = Column(String(32), nullable=False, unique=True)   #Only
 
    classes = relationship("Class", secondary=teacher_m2m_class, backref="teachers")
 
    def __repr__(self):
        return "Lecturer:%s]"%self.teacher_name
 
class Class(Base):
    "class"
    __tablename__ ="class"
    class_id = Column(Integer, primary_key=True)
    class_name = Column(String(32), nullable=False,unique=True)
    course =  Column(String(32), nullable=False)
 
    students = relationship("Student",secondary=class_m2m_student,backref="classes")
 
    def __repr__(self):
        return "Class name: [%s]"%self.class_name
 
class Student(Base):
    "Student"
    __tablename__ ="student"
    stu_id = Column(Integer, primary_key=True)
    stu_name = Column(String(32), nullable=False, unique=True)
    QQ =  Column(Integer(), nullable=False)
 
    def __repr__(self):
        return "Student name: [%s]"%self.stu_name
 
class Lesson(Base):
    "Class day"
    __tablename__ = "lesson"
    lesson_id = Column(Integer, primary_key=True)
    lesson_name = Column(String(32), nullable=False, unique=True)
 
    def __repr__(self):
        return "Section Name: [%s]"%self.lesson_name
 
 
Base.metadata.create_all(engine)
Create a table structure
def add_studyrecord(self):
        '''Adding Learning Records'''
        class_name = input("\033[34;0m Please enter the class name to add the learning record:\033[0m")
        class_obj = self.session.query(Class).filter_by(class_name=class_name).first()
 
        if class_obj and class_obj.teachers[0] == self.teacher_obj:  # Class name input exists and belongs to current teacher management
            lesson_name = input("\033[34;0m Please enter the name of the class that adds the learning record. lesson):\033[0m")
            lesson_obj = self.session.query(Lesson).filter_by(lesson_name=lesson_name).first()
            if lesson_obj:                                       # Input lesson Name exists
                class_m2m_lesson_obj = self.session.query(Class_m2m_Lesson).filter(Class_m2m_Lesson.class_id == class_obj.class_id). \
                    filter(Class_m2m_Lesson.lesson_id == lesson_obj.lesson_id).first()
                if class_m2m_lesson_obj:                                            # Class Corresponding Lessons lesson Table Data Existence
 
                    study_record_obj = self.session.query(Study_record).filter_by(class_m2m_lesson_id=class_m2m_lesson_obj.id).first()
                    if not study_record_obj:                                                    # Class Records for Creation
                        for student_obj in class_obj.students:
                            status = input("Input student %s Class status( yes/no): "%student_obj.stu_name)
                            study_record_new = Study_record(class_m2m_lesson_id=class_m2m_lesson_obj.id,
                                                            stu_id=student_obj.stu_id,
                                                            status=status)
                            self.session.add(study_record_new)
                            self.session.commit()
                    else:
                        print("\33[31;1m System error: Current class record has been created\33[0m")
                else:
                     print("\33[31;1m System Error: Current Class lesson Lesson not created\33[0m")
            else:
                print("\33[31;1m System error: lesson Not created\33[0m")
        else:
            print("\33[31;1m Input error: Class does not exist or does not have permission to manage this class\33[0m")
 
 
    def modify_scores(self):
        '''Revision of results'''
        class_name = input("\033[34;0m Please enter the class name of the learning record.:\033[0m")
        class_obj = self.session.query(Class).filter_by(class_name=class_name).first()
 
        if class_obj and class_obj.teachers[0] == self.teacher_obj:  # Class name input exists and belongs to current teacher management
            lesson_name = input("\033[34;0m Please enter the class name of the learning record. lesson):\033[0m")
            lesson_obj = self.session.query(Lesson).filter_by(lesson_name=lesson_name).first()
 
            if lesson_obj:  # Input lesson Name exists
                class_m2m_lesson_obj = self.session.query(Class_m2m_Lesson).filter(
                    Class_m2m_Lesson.class_id == class_obj.class_id). \
                    filter(Class_m2m_Lesson.lesson_id == lesson_obj.lesson_id).first()
 
                if class_m2m_lesson_obj:  # Class Corresponding Lessons lesson Table Data Existence
                    while True:
                        study_record_objs = self.session.query(Study_record).filter(
                                Study_record.class_m2m_lesson_id==class_m2m_lesson_obj.id).all()
                        for obj in  study_record_objs:
                            print(obj)
 
                        student_name = input("\033[34;0m Enter the name of the student whose grade is to be modified:[Q Sign out]\33[0m")
                        if student_name == "q" or student_name == "Q":break
                        student_obj = self.session.query(Student).filter_by(stu_name=student_name).first()
                        if student_obj:
                            study_record_obj = self.session.query(Study_record).filter(
                                Study_record.class_m2m_lesson_id==class_m2m_lesson_obj.id).filter(
                                Study_record.stu_id == student_obj.stu_id).first()
 
                            if study_record_obj:                            # Class Records Exist
                                score = input("\033[34;0m Enter revised results\33[0m")
                                study_record_obj.score= score
                                self.session.commit()
 
                    else:
                        print("\33[31;1m System error: Current class record has been created\33[0m")
                else:
                    print("\33[31;1m System Error: Current Class lesson Lesson not created\33[0m")
            else:
                print("\33[31;1m System error: lesson Not created\33[0m")
        else:
            print("\33[31;1m Input error: Class does not exist or does not have permission to manage this class\33[0m")
Creating Learning Records and Revising Students'Achievements

The above two pieces of code achieve the main functions, the other functions are routines, are the same...

Posted by virtualdevl on Sun, 27 Jan 2019 11:45:15 -0800