def monthlyStatistics():
   originButton = flightUtil.Button('Select','') 
   destinationButton = flightUtil.Button('Select','')
   airlineButton = flightUtil.Button('Select','')
   monthButton = flightUtil.Button('Select','')
   yearButton = flightUtil.Button('Select','Select')

   try:
      als = flightUtil.executeQueryAndReturn(flightUtil.airlineQuery)
      als = (('Select',''),)+als
      cities = flightUtil.executeQueryAndReturn(flightUtil.cityQuery)

      if request.method=='POST':
         originButton.setValue(request.form['selectedOrigin'])
         destinationButton.setValue(request.form['selectedDestination'])
         airlineButton.setValue(request.form['selectedAirline']) 
         yearButton.setValue(request.form['selectedYear'])
         yearButton.setLabel(request.form['selectedYear'])
         monthButton.setValue(request.form['selectedMonth'])
         #monthButton.setLabel(flightUtil.months[monthButton.getValue()])
         destinationButton.setLabel(\
            flightUtil.executeQueryAndReturn(\
               flightUtil.cityNameQuery%(destinationButton.getValue()))[0][0])
         originButton.setLabel(\
            flightUtil.executeQueryAndReturn(\
               flightUtil.cityNameQuery%(originButton.getValue()))[0][0])
         if airlineButton.getValue()!='':
            airlineButton.setLabel(flightUtil.executeQueryAndReturn(\
                      flightUtil.airlineNameQuery%(airlineButton.getValue()))[0][0])
         else :
            airlineButton.setLabel('Select')
   except Exception, e:
      print 'Error occured'+str(e)
      return render_template('error.html',error=str(e))
def priceHistory():
   slotFrom='Select'
   slotTo='Select'
   origin='Select'
   originValue=''
   destination='Select'
   destinationValue=''
   date=''
   metrics=['MIN','AVG','MAX'] 
   metric='Select'
   history=[]
   try:
      cities = flightUtil.executeQueryAndReturn(flightUtil.cityQuery)
      if request.method=='POST':
         slotFrom=request.form['selectedSlotFrom']
         slotTo=request.form['selectedSlotTo']
         origin=request.form['selectedOrigin']
         destination=request.form['selectedDestination']
         date=request.form['datepicker']
         metric=request.form['selectedMetric']
         query = flightUtil.metricPriceHistoryQuery%(\
                                             metric,\
                                             slotFrom,\
                                             slotTo,\
                                             origin,\
                                             destination,\
                                             origin+'%'+flightUtil.flightQuerySufix(date)+'%')
         print query
         history=flightUtil.executeQueryAndReturn(query)
         print history
         priceHistory = [(hist[0],hist[1]) for hist in history]
         print priceHistory
         history =''
         p = ''
         for i in range(len(priceHistory)):
            p = priceHistory[i][1]
            dt = str(priceHistory[i][0])
            print dt
            dt = dt.split('/')
            history += dt[2]+' '+dt[1]+' '+dt[0]+' '
            history += str(p)+':'

         history += (flightUtil.currentDate())+' '
         history += str(p)
         print history
         originValue = origin
         destinationValue = destination
         destination = flightUtil.executeQueryAndReturn(\
                          flightUtil.cityNameQuery%(destination))[0][0] 
         origin = flightUtil.executeQueryAndReturn(\
                     flightUtil.cityNameQuery%(origin))[0][0]
   except Exception, e:
      print 'Error occured'+str(e)
      return render_template('error.html',error=str(e))
def flight(name):
   typ = flightUtil.flightType(name)
   query = ''
   if typ=="direct":
      query = flightUtil.directFlightPriceHistoryQuery
   elif typ=="oneStop":
      query = flightUtil.oneStopFlightPriceHistoryQuery
   elif typ=="twoStop":
      query = flightUtil.twoStopFlightPriceHistoryQuery

   priceHistory = flightUtil.executeQueryAndReturn(
                     query%name)
   print priceHistory
   priceHistory = [(price[0],price[1]) for price in priceHistory]
   history =''
   p = ''
   print priceHistory
   for i in range(len(priceHistory)):
      p = priceHistory[i][0]
      dt = str(priceHistory[i][1])
      print dt
      dt = dt.split('/')
      history += dt[0]+' '+dt[1]+' '+dt[2]+' '
      history += str(priceHistory[i][0])+';'
   history += (flightUtil.currentDate())+' '
   history += str(p)
   print history
   return history
 def flightView(query,pattern,orig,dest):
    fltLst = []
    nowTime = time.strftime("%H:%M")
    currentDay = time.strftime("%d")
    currentMonth = time.strftime("%m")
    flightDate = dt
    todayDate = time.strftime('%m/%d/%Y')  
    fltHst = \
       flightUtil.executeQueryAndReturn( \
          query%(pattern,orig,dest))
    for flt in fltHst:
       if flightDate!=todayDate or nowTime < flt[1][:5]: 
          fltLst.append(flightUtil.flightDescription(flt[0],flt[1]))
    return fltLst
def flightHistory():
   airlineButton = flightUtil.Button('Select','') 
   originButton = flightUtil.Button('Select','')
   destinationButton = flightUtil.Button('Select','')
   stopButton = flightUtil.Button('Select','')
   oneStop = []
   twoStop = []
   direct = []
   fltLst = []
   dt = ''
   try:
      cities = flightUtil.executeQueryAndReturn(flightUtil.cityQuery)
      als = flightUtil.executeQueryAndReturn(flightUtil.airlineQuery)
      als = (('Select',''),)+als
      if request.method=='POST':
         originButton.setValue(request.form['selectedOrigin'])
         destinationButton.setValue(request.form['selectedDestination'])
         dt = request.form['datepicker']
         airlineButton.setValue(request.form['selectedAirline'])
         stopButton.setValue(request.form['selectedStop'])
         stopButton.setLabel(request.form['selectedStop'])
         pattern = flightUtil.flightQueryPattern(\
                               airlineButton.getValue(),\
                               originButton.getValue(),\
                               dt)
         print pattern
         def flightView(query,pattern,orig,dest):
            fltLst = []
            nowTime = time.strftime("%H:%M")
            currentDay = time.strftime("%d")
            currentMonth = time.strftime("%m")
            flightDate = dt
            todayDate = time.strftime('%m/%d/%Y')  
            fltHst = \
               flightUtil.executeQueryAndReturn( \
                  query%(pattern,orig,dest))
            for flt in fltHst:
               if flightDate!=todayDate or nowTime < flt[1][:5]: 
                  fltLst.append(flightUtil.flightDescription(flt[0],flt[1]))
            return fltLst
         twoStop = flightView(\
            flightUtil.twoStopFlightsQuery,pattern,originButton.getValue(),destinationButton.getValue())
         oneStop = flightView(\
            flightUtil.oneStopFlightsQuery,pattern,originButton.getValue(),destinationButton.getValue())
         direct  = flightView(\
            flightUtil.directFlightsQuery,pattern,originButton.getValue(),destinationButton.getValue())
         destinationButton.setLabel(\
            flightUtil.executeQueryAndReturn(\
               flightUtil.cityNameQuery%(destinationButton.getValue()))[0][0]) 
         originButton.setLabel(\
            flightUtil.executeQueryAndReturn(\
               flightUtil.cityNameQuery%(originButton.getValue()))[0][0])
         if airlineButton.getValue()!='':
            airlineButton.setLabel(flightUtil.executeQueryAndReturn(\
                      flightUtil.airlineNameQuery%(airlineButton.getValue()))[0][0])
         else :
           airlineButton.setLabel('Select')
         if stopButton.getValue()=='':
            stopButton.setLabel('Select')
   except Exception, e:
      print 'Error occured'+str(e)
      return render_template('error.html',error=str(e))