def edit_student_requirements_done(current_student_requirements_done): requirements = db.session.query(ormStudentRequirementsDone).filter( ormStudentRequirementsDone.ukey == current_student_requirements_done).one() student_list = db.session.query(ormStudent).all() requirement_list = db.session.query(ormRequirement).all() form = studentRequirementsDoneForm(request.form) form.requirement_name.choices = [(requirement.requirement_name, requirement.requirement_name) for requirement in requirement_list] form.student_recordbook.choices = [(student.student_recordbook, student.student_recordbook) for student in student_list] if request.method == 'GET': form.requirement_name.data = requirements.requirement_name form.student_recordbook.data = requirements.student_recordbook return render_template( 'requirement/edit_student_requirements_done.html', form=form, form_name="Edit requirement") else: if form.validate(): requirements.requirement_name = form.requirement_name.data requirements.student_recordbook = form.student_recordbook.data try: db.session.commit() return redirect('/student_requirements_done') except: form.requirement_name.errors = [ 'Requirement with this name already exists!' ] return render_template( 'requirement/edit_student_requirements_done.html', form=form, form_name="Edit requirement") else: return render_template('requirement/requirement_edit_page.html', form=form, form_name="Edit requirement")
def studentRequirementsDone(): def select_data(ormStudent, ormStudentRequirementsDone, ormRequirement): res = db.session.query(ormStudentRequirementsDone.ukey, ormStudent.student_recordbook, ormRequirement.requirement_name) \ .select_from(ormStudent) \ .join(ormStudentRequirementsDone) \ .join(ormRequirement) \ .all() return res def insert_data(data): db.session.add(data) def update_data(obj, class_name): mapped_values = {} for item in class_name.__dict__.items(): field_name = item[0] field_type = item[1] is_column = isinstance(field_type, InstrumentedAttribute) if is_column: mapped_values[field_name] = getattr(obj, field_name) db.session.query(class_name).filter_by( ukey=obj.ukey).update(mapped_values) def save(): db.session.commit() student_list = db.session.query(ormStudent).all() requirement_list = db.session.query(ormRequirement).all() data = select_data(ormStudent, ormStudentRequirementsDone, ormRequirement) student_requirement_done = [ StudentRequirementDTO(i[0], i[1], i[2]) for i in data ] form = studentRequirementsDoneForm(request.form) form.requirement_name.choices = [(requirement.requirement_name, requirement.requirement_name) for requirement in requirement_list] form.student_recordbook.choices = [(student.student_recordbook, student.student_recordbook) for student in student_list] if request.method == 'POST': print(form.ukey.data) if form.ukey.data == '': student_requirement = ormStudentRequirementsDone( randint(1, 50), form.student_recordbook.data, form.requirement_name.data) insert_data(student_requirement) else: student_requirement = ormStudentRequirementsDone( randint(1, 50), form.student_recordbook.data, form.requirement_name.data) update_data(student_requirement, ormStudentRequirementsDone) save() return redirect('/student_requirements_done') return render_template('student_requirements_done.html', student_requirement_done=student_requirement_done, form=form)