示例#1
0
def edit_department(id):
    qry = db.session.query(Department).filter(
        Department.id == id
    )  #When you use the contains word then strings with that word will be returned which may not be the case for male, it would also return female coz the word male is in female
    department = qry.first()
    if department:
        form = DepartmentForm(
            formdata=request.form,
            obj=department)  #You can also pass None to the formdata parameter
        if request.method == 'POST' and form.validate():
            department.name = form.name.data
            db.session.add(department)
            db.session.commit()
    return render_template('departments/edit_department.html',
                           department=department,
                           form=form)
示例#2
0
def delete_department(id):
    qry = db.session.query(Department).filter(Department.id == id)
    department = qry.first()
    if department:
        form = DepartmentForm(formdata=request.form, obj=department)
        """ This will delete the member objects associated with the department from the members' database"""
        for member in department.department_member:  #This displays the individual member objects associated with the department object
            qry = db.session.query(DepMember).filter(DepMember.id == member.id)
            member = qry.first()
            db.session.delete(member)
            db.session.commit()
        if request.method == 'POST' and form.validate():
            db.session.delete(department)
            db.session.commit()
            #pass a redirect after deleting the department object
        return render_template('departments/delete.html',
                               form=form,
                               department=department)
    else:
        return 'Error deleting #{id}'.format(id=id)