#NAME: NISPAND MEHTA
#ID_NUMBER: 1001163146
#BATCH TIME: 3.30 to 5.30 p.m.
"""Links Referred::

http://stackoverflow.com/questions/3215140/google-app-engine-appcfg-py-rollback
https://cloud.google.com/appengine/docs/python/googlecloudstorageclient/
http://nealbuerger.com/2013/12/google-app-engine-import-csv-to-datastore/
https://www.daniweb.com/software-development/python/threads/228057/insert-file-data-in-mysql-using-python
http://stackoverflow.com/questions/15064376/python-read-a-csv-and-place-values-in-mysql-database
"""
import cgi
import os
import csv
import cloudstorage as gcs


# Google apliclient (Google App Engine specific) libraries
from google.appengine.api import app_identity
from StringIO import StringIO

from bottle import route, request, response, template, HTTPResponse

import MySQLdb

# Google apliclient (Google App Engine specific) libraries

from bottle import Bottle
import time

bottle = Bottle()
conobj = MySQLdb.connect(unix_socket='/cloudsql/csecloud-971:cloudsql',user='root')
cursor = conobj.cursor()
sql = "USE myclouddata"
cursor.execute(sql)
print "database selected"
my_default_retry_params = gcs.RetryParams(initial_delay=0.2,
                                          max_delay=5.0,
                                          backoff_factor=2,
                                          max_retry_period=15)
# All requests to GCS using the GCS client within current GAE request and
# current thread will use this retry params as default. If a default is not
# set via this mechanism, the library's built-in default will be used.
# Any GCS client function can also be given a more specific retry params
# that overrides the default.
# Note: the built-in default is good enough for most cases. We override
# retry_params here only for demo purposes.
gcs.set_default_retry_params(my_default_retry_params)
#set bucket name and connect it to default bucket
bucket_name = os.environ.get('csecloud-971.appspot.com',
                                 app_identity.get_default_gcs_bucket_name())
bucket = '/' + bucket_name
filename = bucket + '/all_month.csv'
#Route to get CSV file from User
#It will return a template defined in views folder
@bottle.route('/route_to_upload_file')
def route_to_upload_file():
    return template('get_file',name="hello")

@bottle.route('/web_interface')
def web_interface():
    return  template('Query_page',name="hello")

@bottle.route('/process_query')
def process_query():
    mag = request.GET.get('mag')
    par1 = request.GET.get('par1')
    par2 = request.GET.get('par2')
    location = request.GET.get('loc')
    print mag
    print par1
    print par2
    print location
    def_query = "SELECT * from earthquake WHERE "
    if location == None:
        data = fun_for_mag_only(def_query,mag,par1)

    elif mag == None :
        fun_for_location_only(def_query,location)

    else:
        fun_for_macandloc(def_query,mag,par1,par2,location)

    ans = " "
    for x in data:
        ans = ans + str(x[0]) + "|" + str(x[1]) + "|"+ str(x[2])+"|"+ str(x[3])+"|"+ str(x[4])+"|"+ str(x[5])+"|"+ str(x[6])+"|"+ str(x[7])+"|"+ str(x[8])+"|"+ str(x[9])+"|"+ str(x[10])+"|"+ str(x[11])+"|"+ str(x[12])+"|"+ str(x[13])+"|"+ str(x[14])
        ans = ans + "============================================================================================================================================="

    return template('Display_data', details = ans)

def fun_for_mag_only(query,mag,par1):
    if par1 == 'grt':
        query = query + " mag > "+ mag
        print query
    elif par1 == 'less':
        query = query + " mag < " + mag
        print query
    elif par1 == 'equal':
        query = query + "mag = " + mag
        print query
    elif par1 == 'lte':