def initLocations(self): self.home_id = str(Utils.execute_id("""INSERT INTO Locations(latitude, longitude, address, place) VALUES(%s, %s, %s, %s)""", (self.home[0],self.home[1],self.home[2],self.home[3]))) self.work_id = str(Utils.execute_id("""INSERT INTO Locations(latitude, longitude, address, place) VALUES(%s, %s, %s, %s)""", (self.work[0],self.work[1],self.work[2],self.work[3]))) self.extra_id = str(Utils.execute_id("""INSERT INTO Locations(latitude, longitude, address, place) VALUES(%s, %s, %s, %s)""", (self.extra[0],self.extra[1],self.extra[2],self.extra[3]))) for i in self.routes: self.route_ids.append(str(Utils.execute_id("""INSERT INTO Locations(latitude, longitude, address, place) VALUES(%s, %s, %s, %s)""", (i[0], i[1],i[2],i[3])))) return self.home_id
def add_data2(o,occurances): location_id = Utils.execute_id("""INSERT INTO Locations(address) VALUES('The place I am')""",()) user_id = Utils.query("""SELECT user_id FROM User_Sessions WHERE session_token = %s;""", (o.loginResult['token'])) event_id= Utils.execute_id("""INSERT INTO Events(event_type,user_id,name,location_id,locked,deleted) VALUES ('cycle',%s,'test cycle',%s,TRUE,FALSE);""", (user_id[0]["user_id"], location_id)) Utils.execute("""INSERT INTO Cyclical_Events(event_id, cycle_type, occurances) VALUES(%s,'weekly',%s)""", (event_id, json.JSONEncoder().encode(occurances))) return {"location_id" : location_id, "event_id" : event_id }
def getLocationID(self, location): loc_id = Utils.query("SELECT location_id FROM Locations WHERE place = %s", (location[3])) if len(loc_id) > 0: return loc_id[0]["location_id"] else: return Utils.execute_id("""INSERT INTO Locations(latitude, longitude, address, place) VALUES(%s, %s, %s, %s)""", (location[0], location[1], location[2], location[3]))
def add(self): body = json.loads( cherrypy.request.body.read() ) check = Utils.arg_check(body, ['token', 'latitude', 'longitude']) if (check[0]): return check[1] user_check = Utils.validate_user(body["token"]) if(user_check[0]): return user_check[1] user_id = user_check[1] base = "http://maps.googleapis.com/maps/api/geocode/json?" params = "latlng={lat},{lon}&sensor={sen}".format( lat=body['latitude'], lon=body['longitude'], sen=True ) url = "{base}{params}".format(base=base, params=params) response = requests.get(url) if (response): content = response.json() if (content and content['results'] and content['results'][0] and content['results'][0]['formatted_address']): savedLocations = Utils.query("""SELECT * FROM Locations WHERE address = %s""", (content['results'][0]['formatted_address'])) #print savedLocations if (len(savedLocations) == 1): location_id = savedLocations[0]["location_id"] elif (len(savedLocations) > 1): return json.JSONEncoder().encode( Utils.status_more( 34, "Inconsistet database" ) ) else: location_id = Utils.execute_id("""INSERT INTO Locations(latitude, longitude, address, place) VALUES(%s, %s, %s, %s)""", (body["latitude"], body["longitude"], content['results'][0]['formatted_address'], "I don't know")) if (location_id != -1): previousUserLocation = Utils.query("""SELECT location_id FROM Users_Locations WHERE user_id = %s ORDER BY time DESC LIMIT 1""", (user_id)) is_route = False if previousUserLocation: previousLocation = Utils.query("""SELECT * FROM Locations WHERE location_id = %s""", (previousUserLocation[0]["location_id"])) if len(previousLocation) == 1 and self.checkDistance(previousLocation[0]["latitude"], previousLocation[0]["longitude"],body["latitude"],body["longitude"]) == 1: is_route = True Utils.execute("""INSERT INTO Users_Locations(user_id, location_id, time, is_route) VALUES(%s, %s, %s, %s)""", (user_id, location_id, datetime.now(), is_route)) return json.JSONEncoder().encode( Utils.status_more( 0, "OK" ) ) return json.JSONEncoder().encode( Utils.status_more( 35, "Could not save to database" ) ) return json.JSONEncoder().encode( Utils.status_more( 33, "Could not retrieve location information" ) )