def addDrug(): # 判断用户是否登录 user_id = session.get('user_id') if user_id: user = User.query.filter(User.id == user_id).first() if user: if request.method == 'GET': return render_template('addDrug.html') else: # 当前时间 nowDate = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) num = request.form.get('num') name = request.form.get('name') type = request.form.get('type') count = request.form.get('count') price = request.form.get('price') desc = request.form.get('desc') try: count = int(count) except Exception: raise ValueError('count value is error!') # 查找数据库类别表 drugType = DrugType.query.filter(DrugType.name == type).first() if drugType: drugTypeId = drugType.id for index in range(0, count): drug = Drug(num=num, name=name, desc=desc, stockDate=nowDate, stockPrice=price, drugTypeId=drugTypeId) drug.drugType = drugType db.session.add(drug) db.session.flush() # 主要是这里,写入数据库,但是不提交 else: drugType = DrugType(name=type) db.session.add(drugType) db.session.commit() drugTypeId = drugType.id for index in range(0, count): drug = Drug(num=num, name=name, desc=desc, stockDate=nowDate, stockPrice=price, drugTypeId=drugTypeId) drug.drugType = drugType db.session.add(drug) db.session.flush() # 主要是这里,写入数据库,但是不提交 db.session.commit() return redirect(url_for('home')) return redirect(url_for('login'))
def addStock(drugNum): # 判断用户是否登录 user_id = session.get('user_id') if user_id: user = User.query.filter(User.id == user_id).first() if user: # 判断是否是POST if request.method == 'GET': drug = Drug.query.filter(Drug.num == drugNum).first() return render_template('addStock.html', drug=drug) else: num = request.form.get('num') name = request.form.get('name') type = request.form.get('type') count = request.form.get('count') price = request.form.get('price') desc = request.form.get('desc') try: count = int(count) except Exception: raise ValueError('count value is error!') # 查找数据库类别表 drugType = DrugType.query.filter(DrugType.name == type).first() if drugType: drugTypeId = drugType.id for index in range(0, count): drug = Drug(num=num, name=name, desc=desc, stockPrice=price, drugTypeId=drugTypeId) drug.drugType = drugType db.session.add(drug) db.session.flush() # 主要是这里,写入数据库,但是不提交 db.session.commit() return redirect(url_for('addStockHome')) return redirect(url_for('login'))