2016-04-22 135 views
1

我有非常简单的应用程序,其中匿名用户张贴建议和主持人指定接收到的建议(状态是:0,1,2,3)的状态。瓶:记录没有立即更新

我使用烧瓶管理员给管理员定制列表视图(我添加了4个按钮的每个状态)

from flask import Flask,request,url_for,redirect 
from Admin.Admin import admin,init_login 
from Model import db,suggestion,User 
import geoip2.webservice 

app = Flask(__name__) 
app.config.from_pyfile('Config.cfg') 
db.init_app(app) 
app.config['SQLALCHEMY_POOL_SIZE'] = 100 
app.config['SQLALCHEMY_POOL_RECYCLE'] = 100 



init_login(app) 
admin.init_app(app) 
IPclient = geoip2.webservice.Client("XXX") 

def UpdateRecord(ID,status): 
      try: 
        Record = suggestion.query.filter_by(id=ID).first() 
        Record.Flag=status 
        db.session.commit() 
      except Exception, r: 
        raise r 
        #db.session.flush() 
        #Record = suggestion.query.filter_by(id=id).first_or_404() 
        #Record.Flag=1 
        #db.session.commit() 


@app.route('/') 
def index(): 

    return redirect("sometime ... ") 



@app.route("/XXXX/Favorite/<int:id>") 
def favorite(id): 
    UpdateRecord(id,1) 
    return redirect(url_for("admin.index")+"Favsuggestion") 



@app.route("/XXXX/Archive/<int:id>") 
def archive(id): 
    UpdateRecord(id,2) 
    return redirect(url_for("admin.index")+"Arcsuggestion") 



@app.route("/WSC/Published/<int:id>") 
def published(id): 
    UpdateRecord(id,3) 
    return redirect(url_for("admin.index")+"Pubsuggestion") 


@app.route("/suggest",methods=["GET","POST"]) 
def suggest(): 
    try: 
     db.session.flush() 
     suggestionString=request.form.get('suggestionString') 
     IpAddress=request.remote_addr 
     IPresponse = IPclient.city(IpAddress) 

     Country=IPresponse.country.name 
     City=IPresponse.city.name 
     if len(suggestionString)>0: 
      suggestRecord = suggestion(suggestionString,Country,City,IpAddress) 
      db.session.add(suggestRecord) 
      db.session.commit() 
    except Exception,e:return str(e) 
    return redirect("http://site/thankyou") 





@app.teardown_appcontext 
def shutdown_session(response_or_exc): 
    try: 
     if response_or_exc is None: 
      db.session.commit() 
    finally: 
     db.session.remove() 
    return response_or_exc 

这里是我的配置:

# -*- coding: utf-8 -*- 
#NOTE:Replace bands with DB name 
DEBUG = False 
SECRET_KEY='key' 
SQLALCHEMY_DATABASE_URI='mysql://user:[email protected]/sugeestion?charset=utf8' 

的问题是,当主持人通过点击按钮来更改建议状态页面重新加载但状态不会改变几秒后我刷新然后建议状态将被改变

更新 - 2 2016年5月: 我检查了日志,我发现2个错误发生有关DB

OperationalError: (_mysql_exceptions.OperationalError) (2006, 'MySQL server has gone away') [SQL: u'SELECT `User`.id AS `User_id`, `User`.`Username` AS `User_Username`, `User`.`Password` AS `User_Password`, `User`.`Permission` AS `User_Permission` \\nFROM `User` \\nWHERE `User`.id = %s'] [parameters: (1,)], referer: http://TheWebSite 

StatementError: (sqlalchemy.exc.InvalidRequestError) Can't reconnect until invalid transaction is rolled back [SQL: u'SELECT count(%s) AS count_1 \\nFROM `suggestion` \\nWHERE `suggestion`.`Flag` = %s'] [parameters: [immutabledict({})]], referer: http://TheWebSite 

回答

0

好,

数据一致性:

我们聘请了顾问,我转而要求我们需要在每个自定义的adm中的get_query的开头添加db.commit()在我们完全没有怀疑的ModelView类中。

对于错误,我们仍然没有修复它,我们正在玩参数,但迄今为止没有任何工作。