Python-Bottle, form在main app.py中太大



我是新的编程,我正在做一个Webbapp与瓶子。我现在正在做请求部分,但我的导师告诉我这部分很乱。我如何改进或使@route更清洁?我的代码:

"

from bottle import run, template, route, request
from PlanetDestination import price
from database import dataInput
#Global variables will be used for other routes
formDateTime = ""  
formPlanet = ""
formSeat = ""
formFood = ""
@route('/information', method=['GET', 'POST'])
def passangers():
if request.method == 'POST':  
global formDateTime
global formPlanet
global formSeat
global formFood
#calling variables from Frontend
firstName = request.forms.get('firstName')  
lastName = request.forms.get('lastName')
birthD = request.forms.get('birthD')
adress = request.forms.get('adress')
email = request.forms.get('email')
phone = request.forms.get('phone')
payment = request.forms.get('payment')
ticketP = price(formPlanet, formSeat)# calling the function to generate final prices 
data = (firstName, lastName, birthD, adress, email, phone, payment, formDateTime, 
formPlanet, formSeat, formFood, ticketP)
dataInput(data) #Sends the data to sqlite database
formDateTime = ""
formPlanet = ""
redirect ('/confirmation')
else:
return template('templatePersonalData')

缓存选项:这里有一个问题,一旦你的web服务器变成异步,这将无法工作,由于竞争条件。我的建议不是缓存,而是从数据库中提取。

#Global variables will be used for other routes
cache = {}
@route('/information', method=['GET', 'POST'])
def passangers():
if request.method == 'POST':  
global cache
form = ['firstName', 'lastName', 'birthD','address','email','phone','payment', 'formDateTime', 'formPlanet', 'formSeat', 'formFood']
#calling variables from Frontend
for f in form:
cache[f] = request.forms.get(f, '')
cache['ticketP'] = price(cache['formPlanet'], cache['formSeat'])# calling the function to generate final prices 
dataInput(cache) #Sends the data to sqlite database
redirect ('/confirmation')
else:
return template('templatePersonalData')

最新更新