jeudi 3 septembre 2015

Storing user location coordinates and other user specific data in session in flask

I want to store user's location coordinates in the session
I am using Flask Micro Framework.

    from flask import session as sess
    geolocator = GoogleV3()
    @app.route('/location', methods = ['POST'])
    def location():
          latitude = request.json['latitude']
          longitude = request.json['longitude']
          address = geolocator.reverse(str(latitude)+ "," + str(longitude),exactly_one = True)
          sess['lat'] = float(latitude)
          sess['long'] = float(longitude)

This is the code that I have written to store the location in session
I don't think this is the correct way because I am not able to retrive the stored data and this data is different for every user and I am using this coords for further calculation that is to find garage.

Below is the code for the same.

    def find_garage():
          garageList = session.query(Garage)
          user_coords = (float(sess['lat']), float(sess['long']))
          for garage in garageList:
                d = haversine(user_coords,(float(garage.locationX),float(garage.locationY)))
                if d <= 0.5:
                    sess['garagename'] = garage.name
                    sess['garagecontact'] = garage.contactnumber
                    send_email("myemail@example.com","Location:",str(sess['garagename']))
                    break

I am running this find_garage()method once the user submits a post request about the type of service needed.But I am not able to retrieve the garagename and also lat longstored in session and I get the KeyError
Maybe this is due to refresh that happens.
Please Note:I am not using any other line of code for using session.




Aucun commentaire:

Enregistrer un commentaire