def match(user_id, friends=False, is_filter=False, exclude_ids=None, user_ids=None): """ :param user_id: :param friends: :param is_filter: :param exclude_ids: :param user_ids: use if you need limit friends list :return: """ user = FacebookCustomUserActive.objects.get(pk=user_id) likes = list( FacebookLike.objects.filter(user_id=user.id).values_list( 'facebook_id', flat=True)) stop_words = StopWords.objects.all().values_list('word', flat=True) query = ElasticSearchMatchEngineManager.prepare_query(user, stop_words) fields = ["goals", "offers", "interests"] exclude_user_ids = ['members.facebookcustomuseractive.%s' % user_id] neo = NeoFourJ() if exclude_ids is None: exclude_ids = neo.get_my_thumbed_up_ids(user_id) friends_list = [] if not friends: for f in exclude_ids: exclude_user_ids.append('members.facebookcustomuseractive.%s' % f) else: # All my friends # # fids = Friend.objects.all_my_friends(user_id) fids = neo.get_my_friends_ids(user_id) for f in fids: friends_list.append('members.facebookcustomuseractive.%s' % f) if user_ids is not None: for user_id in user_ids: friends_list.append('members.facebookcustomuseractive.%s' % user_id) response = ElasticSearchMatchEngineManager. \ query_builder(user, query, fields, exclude_user_ids, stop_words, is_filter=is_filter, friends_list=friends_list, friends=friends, likes=likes) return response['hits']['hits']
class NeoFriendsResourceTestCase(JWTResourceTestCase): def setUp(self): super(NeoFriendsResourceTestCase, self).setUp() self.neo = NeoFourJ() self.neo.graph.delete_all() self.user = FacebookCustomUser.objects.create_user(username='******', password='******', first_name='Andrii', last_name='So') self.user1 = FacebookCustomUser.objects.create_user(username='******', password='******', first_name='Tata', last_name='MCJ') self.user2 = FacebookCustomUser.objects.create_user(username='******', password='******', first_name='Ti', last_name='Bao') self.n1 = self.neo.create_person(self.neo.person(self.user)) self.n2 = self.neo.create_person(self.neo.person(self.user1)) self.n3 = self.neo.create_person(self.neo.person(self.user2)) self.neo.add_to_friends(self.n1, self.n2) self.neo.add_to_friends(self.n2, self.n1) def test_get_list_unauthorzied(self): self.assertHttpUnauthorized( self.api_client.get('/api/v2/friends/', format='json')) def test_get_my_friends(self): resp = self.api_client.get('/api/v2/friends/', format='json', authentication=self.get_credentials()) deserialized_resp = self.deserialize(resp) self.assertEqual( deserialized_resp['objects'], [{ u'resource_uri': u'/api/v2/friends/{}/'.format(self.n2._id), u'user_id': self.user1.id, u'id': self.n2._id, u'name': u'Tata MCJ' }]) def test_add_to_friends(self): self.data = {'user_id': self.user2.id} resp = self.api_client.post('/api/v2/friends/', format='json', data=self.data, authentication=self.get_credentials()) self.assertHttpCreated(resp) deserialized_resp = self.deserialize(resp) self.assertEqual(deserialized_resp['user_id'], self.user2.id) def test_pass_friend(self): self.data = {'user_id': self.user2.id, 'action': 'pass'} resp = self.api_client.post('/api/v2/friends/', format='json', data=self.data, authentication=self.get_credentials()) self.assertHttpCreated(resp) deserialized_resp = self.deserialize(resp) self.assertEqual(deserialized_resp['user_id'], self.user2.id) def test_remove_from_friends(self): resp = self.api_client.delete('/api/v2/friends/', format='json', authentication=self.get_credentials(), data=json.dumps( {'user_id': self.user1.id})) self.assertHttpAccepted(resp) self.assertEqual(self.neo.get_my_friends_ids(self.user.id), []) def test_disconnect_friend(self): self.data = {'user_id': self.user1.id, 'action': 'disconnect'} resp = self.api_client.post('/api/v2/friends/', format='json', authentication=self.get_credentials(), data=self.data) self.assertHttpCreated(resp) deserialized_resp = self.deserialize(resp) self.assertEqual(deserialized_resp['user_id'], self.user1.id) self.assertFalse( self.neo.check_friendship_rel(self.user.id, self.user1.id))
class FriendUtilsTestCase(TestCase): def setUp(self): self.user = FacebookCustomUser.objects.create_user(username='******', password='******', first_name='Ani', last_name='Lendel') self.user1 = FacebookCustomUser.objects.create_user(username='******', password='******', first_name='Sneja', last_name='Yerson') self.user2 = FacebookCustomUser.objects.create_user(username='******', password='******', first_name='Alena', last_name='Winner') self.neo = NeoFourJ() self.neo.graph.delete_all() def test_create_person(self): self.neo.create_person(self.neo.person(self.user)) neo_user = self.neo.get_person(self.user) self.assertEqual(neo_user.properties['user_id'], self.user.id) self.assertEqual( neo_user.properties['name'], u'{} {}'.format(self.user.first_name, self.user.last_name)) def test_add_to_friend(self): n1 = self.neo.create_person(self.neo.person(self.user)) n2 = self.neo.create_person(self.neo.person(self.user1)) self.neo.add_to_friends(n1, n2) self.neo.add_to_friends(n2, n1) friends = self.neo.get_my_friends(self.user.id) self.assertEqual(friends.one['node_name'], n2.properties['name']) def test_passes_friend(self): n1 = self.neo.create_person(self.neo.person(self.user)) n2 = self.neo.create_person(self.neo.person(self.user1)) self.neo.pass_friend(n1, n2) friends = self.neo.get_my_passes(self.user.id) self.assertEqual(friends.one['node_name'], n2.properties['name']) def test_check_friends(self): n1 = self.neo.create_person(self.neo.person(self.user)) n2 = self.neo.create_person(self.neo.person(self.user1)) self.neo.add_to_friends(n1, n2) self.neo.add_to_friends(n2, n1) friends = self.neo.check_friendship(self.user.id, self.user1.id) self.assertEqual(friends.one['n.name'], n2.properties['name']) def test_get_or_create(self): self.neo.create_person(self.neo.person(self.user)) p, c = self.neo.get_or_create_node(self.user) self.assertFalse(c) self.assertEqual(p['user_id'], self.user.id) def test_get_my_friends_ids(self): n1 = self.neo.create_person(self.neo.person(self.user)) n2 = self.neo.create_person(self.neo.person(self.user1)) n3 = self.neo.create_person(self.neo.person(self.user2)) self.neo.add_to_friends(n1, n2) self.neo.add_to_friends(n2, n1) self.neo.add_to_friends(n2, n3) self.neo.add_to_friends(n3, n2) self.neo.add_to_friends(n1, n3) self.neo.add_to_friends(n3, n1) user_ids = self.neo.get_my_friends_ids(self.user.id) self.assertEqual(sorted(user_ids), sorted([self.user1.id, self.user2.id])) def test_get_my_friends_icontains_name(self): n1 = self.neo.create_person(self.neo.person(self.user)) n2 = self.neo.create_person(self.neo.person(self.user1)) n3 = self.neo.create_person(self.neo.person(self.user2)) self.neo.add_to_friends(n1, n2) self.neo.add_to_friends(n2, n1) self.neo.add_to_friends(n2, n3) self.neo.add_to_friends(n3, n2) self.neo.add_to_friends(n1, n3) self.neo.add_to_friends(n3, n1) user_id = self.neo.get_my_friends_icontains_name(self.user.id, 'sne') self.assertIn(self.user1.id, user_id) user_id = self.neo.get_my_friends_icontains_name(self.user.id, 'ens') self.assertEqual(user_id, list()) user_id = self.neo.get_my_friends_icontains_name(self.user.id, '') self.assertEqual(user_id, list()) user_id = self.neo.get_my_friends_icontains_name(self.user.id, 'Alena') self.assertIn(self.user2.id, user_id) def test_remove_from_friends(self): n1 = self.neo.create_person(self.neo.person(self.user)) n2 = self.neo.create_person(self.neo.person(self.user1)) self.neo.add_to_friends(n1, n2) self.neo.add_to_friends(n2, n1) self.neo.remove_from_friends(self.user.id, self.user1.id) ids = self.neo.get_my_friends_ids(self.user.id) self.assertEqual(ids, []) def test_check_friendsip(self): n1 = self.neo.create_person(self.neo.person(self.user)) n2 = self.neo.create_person(self.neo.person(self.user1)) self.neo.add_to_friends(n1, n2) self.assertTrue( self.neo.check_friendship_rel(self.user.id, self.user1.id)) def test_check_friendship_negative(self): n1 = self.neo.create_person(self.neo.person(self.user)) n2 = self.neo.create_person(self.neo.person(self.user1)) self.neo.add_to_friends(n2, n1) self.assertFalse( self.neo.check_friendship_rel(self.user.id, self.user1.id)) def test_get_new_friends(self): n1 = self.neo.create_person(self.neo.person(self.user)) n2 = self.neo.create_person(self.neo.person(self.user1)) n3 = self.neo.create_person(self.neo.person(self.user2)) self.neo.add_to_friends(n1, n2) self.neo.add_to_friends(n2, n1) self.neo.add_to_friends(n1, n3) self.neo.add_to_friends(n3, n1) self.neo.add_to_friends(n3, n2) self.neo.add_to_friends(n2, n3) count = self.neo.get_new_friends_count(n1['user_id']) self.assertEqual(count, 2) def test_update_rel_seen(self): n1 = self.neo.create_person(self.neo.person(self.user)) n2 = self.neo.create_person(self.neo.person(self.user1)) self.neo.add_to_friends(n1, n2) self.neo.add_to_friends(n2, n1) self.assertEqual(self.neo.get_new_friends_count(n1['user_id']), 1) self.neo.update_rel_seen(n1['user_id'], n2['user_id']) self.assertEqual(self.neo.get_new_friends_count(n1['user_id']), 0) self.assertEqual(self.neo.get_new_friends_count(n2['user_id']), 1) def test_mutual_friends(self): n1 = self.neo.create_person(self.neo.person(self.user)) n2 = self.neo.create_person(self.neo.person(self.user1)) n3 = self.neo.create_person(self.neo.person(self.user2)) self.neo.add_to_friends(n1, n2) self.neo.add_to_friends(n2, n1) self.neo.add_to_friends(n1, n3) self.neo.add_to_friends(n3, n1) self.neo.add_to_friends(n3, n2) self.neo.add_to_friends(n2, n3) mutual_friends = self.neo.get_mutual_friends(self.user.id, self.user2.id) self.assertEqual(mutual_friends, [self.user1.id])