Ejemplo n.º 1
0
	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" ) )
Ejemplo n.º 2
0
  def request(self): 
    body = json.JSONDecoder().decode( cherrypy.request.body.read() )
    check = Utils.arg_check(body, ["token","buddy"])

    if (check[0]): 
      return check[1]

    # Find the user ID of the person making the request.
    user_check = Utils.validate_user(body["token"])
    if(user_check[0]):
      return user_check[1]
    user_id = user_check[1]

    matching_users = Utils.query("""SELECT * FROM Users WHERE username=%s OR email=%s""", 
              (body["buddy"],body["buddy"]))

    if len(matching_users) == 0:
      return json.JSONEncoder().encode( Utils.status_more( 112, "Could not locate user" ) )
    
    matching_user = matching_users[0]
    
    if matching_user["user_id"] == user_id:
      return json.JSONEncoder().encode( Utils.status_more( 115, "Cannot be buddies with yourself" ) )

    in_buddies = Utils.query("""SELECT * FROM Buddies WHERE (requester_id = %s AND requestee_id = %s) OR (requester_id = %s AND requestee_id = %s)""", (user_id,matching_user["user_id"],matching_user["user_id"],user_id))

    if any(in_buddies):
      return json.JSONEncoder().encode( Utils.status_more( 115, "Cannot request buddy when you are already buddies or there is already a pending request between you." ) )

    try:
        Utils.execute("""INSERT INTO Buddies(requester_id, requestee_id, approved) VALUE(%s,%s,0)""",
          (user_id,matching_user["user_id"]))
    except Exception:
        return json.JSONEncoder().encode({"status": Utils.status(3981,"Could not request buddy")})

    return json.JSONEncoder().encode(Utils.status_more(0, "OK"))
Ejemplo n.º 3
0
  def accept(self): 
    body = json.JSONDecoder().decode( cherrypy.request.body.read() )
    check = Utils.arg_check(body, ["token","buddy_id"])

    if (check[0]): 
      return check[1]

    # Find the user ID of the person making the request.
    user_check = Utils.validate_user(body["token"])
    if(user_check[0]):
      return user_check[1]
    user_id = user_check[1]

    try:
        Utils.execute("""UPDATE Buddies
                         SET approved=1
                         WHERE requester_id=%s AND requestee_id=%s""",
                         (body["buddy_id"],user_id))
    except Exception:
        return json.JSONEncoder().encode({"status": Utils.status(3981,"Could not approve buddy")})

    return json.JSONEncoder().encode(Utils.status_more(0, "OK"))