Search code examples
pythonbottle

Python-Bottle, form is too big in the main app.py


I'm new to programming and I am doing a Webbapp with bottle. I'm doing the request part right now but my tutor told me is quite messy. How can I improve or make @route cleaner? My code:

'''

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')

Solution

  • Option for cache with caveat: There is a problem here, as soon as your webserver becomes async this will not work due to race conditions. My suggestion is not cache, and instead pull from a database.

    #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')