def test_hosework(self): s = Students(id='04160909', perm=Permission.WRITE) t = Teamnumbers(id='041609', fk_sid=s.id) c = Courses(id='01', name='英语') c2 = Courses(id='02', name='数学') c3 = Courses(id='03', name='语文') h = Houseworks(createdate=datetime.now(), title='英语作业1', content='完成单元1测试', fk_cid=c.id, fk_tid=t.id, state=States.NOW) h3 = Houseworks(createdate=datetime.now(), title='语文作业1', content='完成单元1测试', fk_cid=c3.id, fk_tid=t.id, state=States.NOW) h4 = Houseworks(createdate=datetime.now(), title='数学作业1', content='完成单元1测试', fk_cid=c2.id, fk_tid=t.id, state=States.NOW) h5 = Houseworks(createdate=datetime.now(), title='数学作业2', content='完成单元1测试', fk_cid=c2.id, fk_tid=t.id, state=States.NOW) h6 = Houseworks(createdate=datetime.now(), title='数学作业3', content='完成单元1测试', fk_cid=c2.id, fk_tid=t.id, state=States.NOW) h7 = Houseworks(createdate=datetime.now(), title='英语作业2', content='完成单元2测试', fk_cid=c.id, fk_tid=t.id, state=States.NOW) s2 = Students(id='04160809', perm=Permission.ADMIN) t2 = Teamnumbers(id='041608', fk_sid=s2.id) h2 = Houseworks(createdate=datetime.now(), title='英语作业1', content='完成单元1测试', fk_cid=c.id, fk_tid=t2.id, state=States.NOW) db.session.add_all( [s, t, c, h, s2, t2, h2, c2, c3, h3, h4, h5, h6, h7]) db.session.commit() self.assertEqual(h.fk_tid, t.id) print(h.to_json())
def post(self, *args, **kwargs): # 第一种方式 stu = Students() stu.s_name = u'小四' session.add(stu) session.commit() # 第二种方式: 批量添加 stu_list = [] for i in range(5): stu = Students() stu.s_name = '小明_%s' % i stu_list.append(stu) session.add_all(stu_list) session.commit() self.write('创建学生信息成功')
def index(): dept_form = DepartmentForm() if dept_form.validate_on_submit(): dept = Departments(dept_name=dept_form.dept_name.data, building=dept_form.building.data, budget=dept_form.budget.data) db.session.add(dept) db.session.commit() return redirect(url_for('index')) departments = Departments.query.all() form = StudentForm() form.dept_name.choices = [(i, i) for i in [ii.dept_name for ii in departments]] if form.validate_on_submit(): student = Students(first_name=form.first_name.data, last_name=form.last_name.data, email=form.email.data, dept_name=form.dept_name.data) db.session.add(student) db.session.commit() return redirect(url_for('index')) students = Students.query.all() return render_template('index.html', title='Home', dept_form=dept_form, form=form, departments=departments, students=students)
def add_student(id): form = AddStudentForm() h1 = 'Add A Student' account = Accounts.query.get(id) # if for validates if form.validate_on_submit(): teacher = form.teacher_ID.data instrument = form.instrument.data # add formatting so names are capitalized student = Students(account_ID=id, teacher_ID=teacher.id, first_name=form.first_name.data.capitalize(), last_name=form.last_name.data.capitalize(), instrument=instrument.instrument, notes=form.notes.data) db.session.add(student) db.session.commit() flash('Student added!') return redirect(url_for('main.view_account', id=id)) else: print("not valid") return render_template('add_student.html', title='Add Student', account=account, h1=h1, form=form)
def student_register(): if request.method == 'GET': return render_template('StudentRegister.html') elif request.method == 'POST': username = request.form.get('username') password = request.form.get('password') students = Students() students.s_name = username students.password = password db.session.add(students) db.session.commit() return 'Register Success'
def create_stu_all(): # 批量创建,add_all() names = ['小王', '老王', '厂长', '莉哥', '温婉'] stu_list = [] for name in names: stu = Students() stu.s_name = name # stu.save() stu_list.append(stu) # db.session.add(stu) db.session.add_all(stu_list) db.session.commit() return '批量创建成功'
def delete_student(): form = DeleteForm() if request.method == 'POST' and form.validate(): student = Students(idNum=form.id_number.data) if student.exist(): student.delete() flash("Deleted student with id number '{}'".format( form.id_number.data), category='success') return redirect(url_for('students')) else: flash("Student Does not exist.", category='danger') return redirect(url_for('delete_student')) else: return render_template('delete_student.html', form=form)
def edit_student(student_id): form = StudentForm() db = Students(idNum=student_id) student = db.search('idNum', student_id) if request.method == 'POST' and form.validate(): newId = Students(idNum=form.id_number.data) if(newId.exist() and student_id != form.id_number.data): flash('ID number already taken please choose another ID NUMBER', category='danger') return redirect(url_for('edit_student', student_id=db.idNum)) else: db.update(form.firstname.data, form.lastname.data, form.gender.data, form.id_number.data, form.course.data) flash('Successfully Edited Student', category='success') return redirect(url_for('students')) else: for attribute in student: form.firstname.data = attribute[0] form.lastname.data = attribute[1] form.gender.data = attribute[2] form.id_number.data = attribute[3] form.course.data = attribute[4] return render_template('edit_student.html', form=form)
def add_student(): form = StudentForm() if request.method == 'POST' and form.validate(): student = Students(form.firstname.data.title(), form.lastname.data.title(), form.gender.data, form.id_number.data, form.course.data) if student.exist() is False: student.add() flash("Student Added Successfully with ID number '{}'".format( form.id_number.data), category='success') return redirect(url_for('students')) else: flash("Student Already Exist!", category='danger') return redirect(url_for('add_student')) else: return render_template('add_student.html', form=form)
def register(): if current_user.is_authenticated: return redirect(url_for('index')) register_form = RegisterForm() if register_form.validate_on_submit(): student = Students(student_id=register_form.student_id.data, student_name=register_form.student_name.data, email=register_form.email.data) student.set_password(register_form.password.data) db.session.add(student) db.session.commit() flash('Account Registered. Redirecting to login.') return redirect(url_for('login')) return render_template('signup.html', title="Sign Up Page", form=register_form)
def register(): id = request.values.get('id', '') name = request.values.get('name', '') email = request.values.get('email', '') password = request.values.get('password', '') check_id = Students.query.filter_by(id=id).first() jdata = {'code': -1, 'token': ''} if check_id is None and name is not '' and email is not '' and len( password) >= 8: user = Students() user.id = id user.name = name user.email = email user.password = password db.session.add(user) db.session.commit() jdata['code'] = 0 return jsonify(jdata)
def delete(student_id): student = Students(idNum=student_id) student.delete() flash(f"Deleted student with id number '{student_id}'", category='success') return redirect(url_for('students'))
def create_stu(): # 实现创建, add() stu = Students() stu.s_name = '小明1' stu.save() return '创建学生信息成功'