def cal_page(): """课表导出页面视图函数""" from everyclass.server.calendar import ics_generator from everyclass.server.db.dao import get_classes_for_student, get_my_semesters, check_if_stu_exist from everyclass.server.db.model import Semester # 如果请求中包含 id 就写入 session if request.values.get('id'): if not check_if_stu_exist(request.values.get('id')): flash("你输入的学号不存在") return redirect(url_for("main.main")) session['stu_id'] = request.values.get('id') # 如果 session 中有 stu_id 就生成 ics 并返回页面,没有就跳转回首页 if session.get('stu_id', None): # 获得学生姓名和他的合法学期 my_available_semesters, student_name = get_my_semesters( session['stu_id']) semester = Semester.get() student_classes = get_classes_for_student(session['stu_id'], semester) ics_generator.generate(session['stu_id'], student_name, student_classes, semester.to_str(simplify=True), semester.to_tuple()) return render_template('ics.html', student_id=session['stu_id'], semester=semester.to_str(simplify=True)) else: return redirect(url_for('main.main'))
def get_students_in_class(class_id: str): """ 获得一门课程的全部学生 :param class_id: 班级 ID :return: 若有学生,返回课程名称、课程时间(day、time)、任课教师、学生列表(包含姓名、学号、学院、专业、班级), 否则引出 exception """ from everyclass.server.db.model import Semester from everyclass.server.exceptions import NoStudentException, NoClassException mysql_query = "SELECT students,clsname,day,time,teacher FROM {} WHERE id=%s" \ .format('ec_classes_' + Semester.get().to_db_code()) db = get_connection() cursor = db.cursor() cursor.execute(mysql_query, (class_id, )) result = cursor.fetchall() if not result: cursor.close() raise NoClassException(class_id) else: students = json.loads(result[0][0]) students_info = list() class_name = result[0][1] class_day = result[0][2] class_time = result[0][3] class_teacher = result[0][4] if not students: cursor.close() raise NoStudentException students_in_sql = '' for each_student in students: students_in_sql += "'" + each_student + "'," students_in_sql = '(' + students_in_sql[0:len(students_in_sql) - 1] + ')' print(students_in_sql) mysql_query = "SELECT xh, name, faculty, class_name FROM ec_students WHERE xh IN {}".format( students_in_sql) cursor.execute(mysql_query) result = cursor.fetchall() if result: # 信息包含姓名、学号、学院、专业、班级 for each in result: students_info.append([each[1], each[0], each[2], each[3]]) cursor.close() return class_name, class_day, class_time, class_teacher, students_info
def get_students_in_class(class_id): """ 获得一门课程的全部学生,若有学生,返回课程名称、课程时间(day、time)、任课教师、学生列表(包含姓名、学号、学院、专业、班级), 否则引出 exception :param class_id: :return: """ from everyclass.server.db.model import Semester from everyclass.server.exceptions import NoStudentException, NoClassException mysql_query = "SELECT students,clsname,day,time,teacher FROM {} WHERE id=%s" \ .format('ec_classes_' + Semester.get().to_db_code()) db = get_local_conn() cursor = db.cursor() cursor.execute(mysql_query, (class_id, )) result = cursor.fetchall() if not result: cursor.close() raise NoClassException(class_id) else: students = json.loads(result[0][0]) students_info = list() class_name = result[0][1] class_day = result[0][2] class_time = result[0][3] class_teacher = result[0][4] if not students: cursor.close() raise NoStudentException for each_student in students: mysql_query = "SELECT name FROM ec_students WHERE xh=%s" cursor.execute(mysql_query, (each_student, )) result = cursor.fetchall() if result: # 信息包含姓名、学号、学院、专业、班级 students_info.append([ result[0][0], each_student, faculty_lookup(each_student), class_lookup(each_student) ]) cursor.close() return class_name, class_day, class_time, class_teacher, students_info
def cal_page(): """课表导出页面视图函数""" from everyclass.server.db.dao import check_if_stu_exist from everyclass.server.db.model import Semester # 如果请求中包含 id 就写入 session if request.values.get('id'): if not check_if_stu_exist(request.values.get('id')): flash("你输入的学号不存在") return redirect(url_for("main.main")) session['stu_id'] = request.values.get('id') # 如果 session 中有 stu_id 就生成 ics 并返回页面,没有就跳转回首页 if session.get('stu_id', None): # 获得学生姓名和他的合法学期 semester = Semester.get() return render_template('ics.html', student_id=session['stu_id'], semester=semester.to_str(simplify=True)) else: return redirect(url_for('main.main'))