def create(): data = dict( title="錄入員工" ) if request.method == "POST": res = dict(code=0) form = EmployeeAddForm() if form.validate(): emp_id = save_employee(form) session = ORM.db() try: for v in form.hobby.data: hobby = Hobby( employee_id=emp_id, hobby_key=int(v), createdAt=dt(), updatedAt=dt() ) session.add(hobby) except Exception as e: session.rollback() else: session.commit() finally: session.close() res["code"] = 1 else: res = form.errors res["code"] = 0 return jsonify(res) else: return render_template("create.html", data=data)
def save_game(form): session = ORM.db() try: game = Game(name=form.name.data, email=form.email.data, createdAt=dt(), updatedAt=dt()) session.add(game) except Exception as e: session.rollback() else: session.commit() finally: session.close() return
def save_message(form): session = ORM.db() try: message = Message(game_id=form.id.data, title=form.name.data, content=form.info.data, createdAt=dt(), updatedAt=dt()) session.add(message) except Exception as e: session.rollback() else: session.commit() finally: session.close() return
def update_emp(form): session = ORM.db() try: # 修改員工訊息 emp = session.query(Employee).filter_by(id=int(form.id.data)).first() emp.name = form.name.data emp.job = int(form.job.data) emp.sex = int(form.sex.data) emp.edu = int(form.edu.data) emp.birth = form.birth.data emp.email = form.email.data emp.phone = form.phone.data emp.info = form.info.data emp.face = form.face.data emp.updatedAt = dt() # 2.修改愛好 hob = session.query(Hobby).filter_by( employee_id=int(form.id.data)).all() old_hob_keys = [v.hobby_key for v in hob] new_hob_keys = [int(v) for v in form.hobby.data] # 2-1.新增愛好添加到數據庫中 for v in new_hob_keys: if v not in old_hob_keys: ch = Hobby(employee_id=emp.id, hobby_key=v, createdAt=dt(), updatedAt=dt()) session.add(ch) # 2-2.取消爱好從數據庫中中删除 for v in old_hob_keys: if v not in new_hob_keys: ch = session.query(Hobby).filter( and_(Hobby.employee_id == emp.id, Hobby.hobby_key == v)).first() session.delete(ch) session.add(emp) except Exception as e: session.rollback() else: session.commit() finally: session.close()
def save_subgame(form): session = ORM.db() try: subgame = Subgame( game_id=form.id.data, name=form.name.data, url=form.url.data, img=form.img.data, createdAt=dt(), updatedAt=dt() ) session.add(subgame) except Exception as e: session.rollback() else: session.commit() finally: session.close() return
def save_employee(form): session = ORM.db() try: employee = Employee( name=form.name.data, job=int(form.job.data), sex=int(form.sex.data), edu=int(form.edu.data), birth=form.birth.data, email=form.email.data, phone=form.phone.data, info=form.info.data, face=form.face.data, createdAt=dt(), updatedAt=dt() ) session.add(employee) except Exception as e: session.rollback() else: session.commit() finally: session.close() return employee.id
def update_mes(form): session = ORM.db() try: # 修改員工訊息 emp = session.query(Message).filter_by(id=int(form.id.data)).first() #emp.game_id = form.game_id.data emp.content = form.info.data emp.title = form.name.data emp.updatedAt = dt() except Exception as e: session.rollback() else: session.commit() finally: session.close()
def save_menu(form): session = ORM.db() try: menu = Menu(game_id=form.id.data, item=form.name.data, url=form.url.data, createdAt=dt()) session.add(menu) except Exception as e: session.rollback() else: session.commit() finally: session.close() return