Ejemplo n.º 1
0
def is_local(uid,latlng):
  userprefs = UserPrefs.get_by_key_name(uid)
  if not userprefs:
    return False
  return userprefs.location_lat == latlng[0] and userprefs.location_lng == latlng[1]
Ejemplo n.º 2
0
  def get(self):
    user = self.current_user
    
    if user:
      userprefs = get_userprefs(user["id"])
      if not userprefs.acknowledged_terms:
        self.redirect('/profile?terms=1')
        return
    else:
      userprefs = None
    
    locations = dict()
    friends_count = 0
    friends_count_2 = 0
    if user:
      graph = facebook.GraphAPI(version=2.1,access_token=user["access_token"])

      friends = get_friends(graph)
      friends[:] = [friend for friend in friends if is_app_user(str(friend['id']))]
      for profile in friends:
        friends_count += 1
        friend_prefs = UserPrefs.get_by_key_name(str(profile['id']))
        location_name = friend_prefs.location_name
        location_lng = friend_prefs.location_lng
        location_lat = friend_prefs.location_lat
        location_id = (location_lat,location_lng)
        if (0,0) != location_id:
          if location_id not in locations:
            locations[location_id] = dict()
            locations[location_id]['name'] = location_name
            locations[location_id]['count'] = 1
            locations[location_id]['count_2'] = 0
            locations[location_id]['longitude'] = location_lng
            locations[location_id]['latitude'] = location_lat
          else:
            locations[location_id]['count'] += 1
        
        friend_user = User.get_by_key_name(str(profile['id']))
        if friend_user.offline_token_created + timedelta(2*365/12) < datetime.utcnow():
          logging.info('Friend ' + str(profile['id']) + ' access token expired') 
          continue
        graph_friend = facebook.GraphAPI(version=2.1,access_token=friend_user.offline_token)

        friends_2 = get_friends(graph_friend)
        friends_2[:] = [friend for friend in friends_2 if is_app_user(str(friend['id']))]
        friends_1_2_with_out_mutual = remove_profile_by_id(friends_2, [user['id']])
 
        for profile_2 in friends_1_2_with_out_mutual:
          # ignore mutual friend
          # FIXME inefficient, assumes 1st degree list is shorter than 2nd
          if any(f['id'] == profile_2['id'] for f in friends):
            continue
          # ignore "me"
          if str(profile_2['id']) == str(user['id']):
            continue
          friends_count_2 += 1
          friend_prefs_2 = UserPrefs.get_by_key_name(str(profile_2['id']))
          location_name = friend_prefs_2.location_name
          location_lng = friend_prefs_2.location_lng
          location_lat = friend_prefs_2.location_lat
          location_id = (location_lat,location_lng)
          if location_id not in locations:
            locations[location_id] = dict()
            locations[location_id]['name'] = location_name
            locations[location_id]['count'] = 0
            locations[location_id]['count_2'] = 1
            locations[location_id]['longitude'] = location_lng
            locations[location_id]['latitude'] = location_lat
          else:
            locations[location_id]['count_2'] += 1
    
    locations_list = sorted(locations.items(), key=lambda l: l[1]['name'])

    template = template_env.get_template('home.html')
    context = {
      'facebook_app_id': FACEBOOK_APP_ID,
      'user': user,
      'userprefs': userprefs,
      'locations': locations_list,
      'friends_count': friends_count,
      'friends_count_2': friends_count_2,
      'google_maps_api_key': GOOGLE_MAPS_API_KEY,
      'markers': json.encode(locations_list),
    }
    self.response.out.write(template.render(context))
Ejemplo n.º 3
0
def is_app_user(uid):
  if User.get_by_key_name(uid) and UserPrefs.get_by_key_name(uid):
    return True
  else:
    return False