Python開發程式:學員管理系統(mysql),pythonmysql

來源:互聯網
上載者:User

Python開發程式:學員管理系統(mysql),pythonmysql

主題:學員管理系統

需求:

使用者角色,講師\學員, 使用者登陸後根據角色不同,能做的事情不同,分別如下

講師視圖:

  •   管理班級,可建立班級,根據學員qq號把學員加入班級
  •   可建立指定班級的上課紀錄,注意一節上課紀錄對應多條學員的上課紀錄, 即每節課都有整班學員上, 為了紀錄每位學員的學習成績,需在建立每節上課紀錄是,同時為這個班的每位學員建立一條上課紀錄
  •   為學員批改成績, 一條一條的手動修改成績

學員視圖:

  • 提交作業
  • 查看作業成績
  • 一個學員可以同時屬於多個班級,就像報了Linux的同時也可以報名Python一樣, 所以提交作業時需先選擇班級,再選擇具體上課的節數
  • 附加:學員可以查看自己的班級成績排名

 

表結構:

根據需求先畫表結構

 

程式目錄結構:

 

表結構執行個體代碼:

from sqlalchemy import String,Column,Integer,ForeignKey,DATE,Tablefrom sqlalchemy.ext.declarative import declarative_basefrom sqlalchemy.orm import relationshipfrom conf.settings import engine############建立資料表結構######################3Base = declarative_base()# 班級與學生的對應關係表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")),                          )# 班級與學生的對應關係表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):    '''班級和課節對應表'''    __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):    "上課記錄"    __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,狀態:【%s】,成績:【%s】\33[0m"%(self.class_m2m_lessons,self.students,self.status,self.score)class Teacher(Base):    "講師"    __tablename__ = "teacher"    teacher_id = Column(Integer, primary_key=True)    teacher_name = Column(String(32), nullable=False, unique=True)   #唯一    classes = relationship("Class", secondary=teacher_m2m_class, backref="teachers")    def __repr__(self):        return "講師:【%s】"%self.teacher_nameclass Class(Base):    "班級"    __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 "班級名:【%s】"%self.class_nameclass Student(Base):    "學生"    __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 "學生名:【%s】"%self.stu_nameclass Lesson(Base):    "課節"    __tablename__ = "lesson"    lesson_id = Column(Integer, primary_key=True)    lesson_name = Column(String(32), nullable=False, unique=True)    def __repr__(self):        return "節次名:【%s】"%self.lesson_nameBase.metadata.create_all(engine)

 

建立學習記錄以及修改學產生績:

def add_studyrecord(self):        '''添加學習記錄'''        class_name = input("\033[34;0m請輸入要添加學習記錄的班級名:\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:  # 輸入的班級名存在且在屬於當前老師管理            lesson_name = input("\033[34;0m請輸入添加學習記錄的課節名(lesson):\033[0m")            lesson_obj = self.session.query(Lesson).filter_by(lesson_name=lesson_name).first()            if lesson_obj:                                       # 輸入的lesson名字存在                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:                                            # 班級對應的課lesson表資料存在                    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:                                                    # 上課記錄為建立                        for student_obj in class_obj.students:                            status = input("輸入學生 %s 的上課狀態(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系統錯誤:當前上課記錄已經建立\33[0m")                else:                     print("\33[31;1m系統錯誤:當前班級的lesson課節未建立\33[0m")            else:                print("\33[31;1m系統錯誤:lesson未建立\33[0m")        else:            print("\33[31;1m輸入錯誤:班級不存在或沒有許可權管理此班級\33[0m")    def modify_scores(self):        '''修改成績'''        class_name = input("\033[34;0m請輸入學習記錄的班級名:\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:  # 輸入的班級名存在且在屬於當前老師管理            lesson_name = input("\033[34;0m請輸入學習記錄的課節名(lesson):\033[0m")            lesson_obj = self.session.query(Lesson).filter_by(lesson_name=lesson_name).first()            if lesson_obj:  # 輸入的lesson名字存在                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:  # 班級對應的課lesson表資料存在                    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輸入要修改成績的學生名:[Q 退出]\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:                            # 上課記錄存在                                score = input("\033[34;0m輸入修改後的成績\33[0m")                                study_record_obj.score= score                                self.session.commit()                    else:                        print("\33[31;1m系統錯誤:當前上課記錄已經建立\33[0m")                else:                    print("\33[31;1m系統錯誤:當前班級的lesson課節未建立\33[0m")            else:                print("\33[31;1m系統錯誤:lesson未建立\33[0m")        else:            print("\33[31;1m輸入錯誤:班級不存在或沒有許可權管理此班級\33[0m")

 

上面的兩段代碼實現了主要的功能,其他的功能都是套路,都一樣。。。。

 

完整代碼地址--》》https://coding.net/u/James_Lian/p/Whaterver/git/tree/master

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.