2016-04-22 169 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類中。

對於錯誤,我們仍然沒有修復它,我們正在玩參數,但迄今爲止沒有任何工作。