def test_filter_with_different_word_form_of_keywords2(self): """ Searching Crowd for "hiking" yields no results (but search for "hike" does) - ICE-1311 """ Goal.objects.create(user=self.user, goal=self.subject) Goal.objects.create(user=self.user1, goal=self.subject) Goal.objects.create(user=self.user1, goal=Subject.objects.create(description='hike')) Goal.objects.create(user=self.user2, goal=self.subject) FilterState.objects.create(user=self.user, min_age=18, max_age=99, keyword='hiking', distance=16516) update_index.Command().handle(interactive=False) match_users = MatchQuerySet.all(self.user.id, is_filter=True) self.assertEqual(len(match_users), 1) self.assertEqual(match_users[0].first_name, 'Sasa') self.assertEqual(match_users[0].goals, [{ u'learning django': 1, u'hike': 0 }])
def get_object_list(self, request): fs = FilterState.objects.filter(user=request.user.id) cache_match_users = None filter_updated_sha = None if fs: try: attrs = [fs[0].gender, fs[0].min_age, fs[0].max_age, fs[0].distance, fs[0].distance_unit, fs[0].order_criteria, fs[0].keyword] filter_updated = '.'.join(map(str, attrs)) filter_updated_sha = hashlib.sha1(filter_updated).hexdigest() # Concatenate all filters value instead!!! # m.1312.10000mi.sim # filter_updated = time.mktime(fs[0].updated.timetuple()) cache_match_users = cache.get('%s_%s' % (request.user.id, filter_updated_sha)) except AttributeError: pass if cache_match_users: match_users = cache_match_users else: match_users = MatchQuerySet.all(request.user.id, is_filter=True) cache.set('%s_%s' % (request.user.id, filter_updated_sha), match_users) if fs: if fs[0].order_criteria == 'match_score': return sorted(match_users, key=lambda x: -x.score) elif fs[0].order_criteria == 'mutual_friends': return sorted(match_users, key=lambda x: -x.friends_score) elif fs[0].order_criteria == 'date': return sorted(match_users, key=lambda x: x.last_login, reverse=True) return match_users
def test_order_mutual_friends(self): Goal.objects.get_or_create(user=self.user, goal=self.subject) Goal.objects.get_or_create(user=self.user1, goal=self.subject5) Goal.objects.get_or_create(user=self.user2, goal=self.subject5) Goal.objects.get_or_create(user=self.user3, goal=self.subject5) Goal.objects.get_or_create(user=self.user4, goal=self.subject5) Goal.objects.get_or_create(user=self.user5, goal=self.subject5) FilterState.objects.create(user=self.user, min_age=18, max_age=99, distance=16000, order_criteria='mutual_friends') self.neo.create_friendship(self.user, self.user1) self.neo.create_friendship(self.user, self.user3) self.neo.create_friendship(self.user5, self.user1) self.neo.create_friendship(self.user5, self.user3) self.neo.create_friendship(self.user4, self.user1) update_index.Command().handle(interactive=False) match_users = MatchQuerySet.all(self.user.id, is_filter=True) self.assertEqual(len(match_users), 3) resp = self.api_client.get('/api/v1/matchfeed/', authentication=self.get_credentials(), format='json') self.assertValidJSONResponse(resp) data = self.deserialize(resp)['objects'] self.assertEqual(data[0]['friends_score'], 2) self.assertEqual(data[1]['friends_score'], 1) self.assertEqual(data[2]['friends_score'], 0)
def test_simple_match_query_set(self): Goal.objects.create(user=self.user, goal=self.subject) Offer.objects.create(user=self.user1, offer=self.subject5) update_index.Command().handle(interactive=False) users = MatchQuerySet.all(self.user.id) self.assertEqual(len(users), 1) self.assertEqual(users[0].first_name, 'Sasa')
def test_filter_gender_female(self): Goal.objects.create(user=self.user, goal=self.subject) Goal.objects.create(user=self.user4, goal=self.subject5) FilterState.objects.create(user=self.user, gender='f', max_age=99) update_index.Command().handle(interactive=False) match_users = MatchQuerySet.all(self.user.id, is_filter=True) self.assertEqual(len(match_users), 1) self.assertEqual(match_users[0].first_name, 'Tati') self.assertEqual(match_users[0].gender, 'f')
def test_simple_match_score_offers(self): Offer.objects.create(user=self.user, offer=self.subject) Offer.objects.create(user=self.user, offer=self.subject2) Offer.objects.create(user=self.user1, offer=self.subject5) Offer.objects.create(user=self.user1, offer=self.subject6) Offer.objects.create(user=self.user1, offer=self.subject7) update_index.Command().handle(interactive=False) users = MatchQuerySet.all(self.user.id) self.assertEqual(users[0].score, 2)
def test_filter_gender_male(self): Goal.objects.create(user=self.user, goal=self.subject) Goal.objects.create(user=self.user1, goal=self.subject5) Goal.objects.create(user=self.user3, goal=self.subject5) FilterState.objects.create(user=self.user, gender='m', distance=16516) update_index.Command().handle(interactive=False) match_users = MatchQuerySet.all(self.user.id, is_filter=True) self.assertEqual(len(match_users), 1) self.assertEqual(match_users[0].first_name, 'Sasa') self.assertEqual(match_users[0].gender, 'm')
def test_simple_top_interests_less_than_3(self): Goal.objects.create(user=self.user, goal=self.subject11) Goal.objects.create(user=self.user1, goal=self.subject10) update_index.Command().handle(interactive=False) match_users = MatchQuerySet.all(self.user.id) self.assertEqual(len(match_users), 1) self.assertEqual(match_users[0].top_interests[0], { u'kiteboarding': 1, u'foxes': 0 })
def test_simple_top_interests(self): Goal.objects.create(user=self.user, goal=self.subject) Goal.objects.create(user=self.user1, goal=self.subject15) update_index.Command().handle(interactive=False) match_users = MatchQuerySet.all(self.user.id) self.assertEqual(len(match_users), 1) self.assertEqual(match_users[0].top_interests[0], { u'coding': 0, u'django': 1, u'python': 0 })
def test_filter_distance(self): Goal.objects.create(user=self.user, goal=self.subject) Goal.objects.create(user=self.user1, goal=self.subject5) Goal.objects.create(user=self.user1, goal=self.subject2) Goal.objects.create(user=self.user3, goal=self.subject5) FilterState.objects.create(user=self.user, min_age=18, max_age=99, distance=1) update_index.Command().handle(interactive=False) match_users = MatchQuerySet.all(self.user.id, is_filter=True) self.assertEqual(len(match_users), 0)
def get_object_list(self, request): now = time.time() rsvp = request.GET.get('rsvp') event_id = request.GET.get('event_id') try: interest_id = int(request.GET.get('interest_id')) except (ValueError, TypeError): try: interest_id = int(request.GET.get('interest')) except (ValueError, TypeError): interest_id = None if rsvp is None and event_id is None: logger.error('rsvp and event_id is required') raise BadRequest('rsvp and event_id is required') if rsvp not in ('yes', 'no', 'maybe'): logger.error('rsvp is incorrect rsvp: {}'.format(rsvp)) raise BadRequest('Please use correct rsvp: yes, no or maybe') if interest_id: user_ids = Interest.objects.filter( interest_id=interest_id ).values_list('user_id', flat=True) attendees_ids = Membership.objects.filter( rsvp=rsvp, event_id=event_id, is_organizer=False, user_id__in=user_ids ).values_list('user_id', flat=True) else: attendees_ids = Membership.objects.filter( rsvp=rsvp, event_id=event_id, is_organizer=False ).values_list('user_id', flat=True) if rsvp == 'yes': attendees_ids = list(attendees_ids) match_users = MatchQuerySet.all(request.user.id, is_filter=False, exclude_ids=()) attendees = [] matched_attendees_ids = [] for match_user in match_users: if match_user.id in attendees_ids: attendees.append(match_user) matched_attendees_ids.append(match_user.id) non_match_attendees = set(attendees_ids) - set(matched_attendees_ids) for non_match_attendee in non_match_attendees: try: non_match_user = NonMatchUser(request.user.id, non_match_attendee) attendees.append(non_match_user) except FacebookCustomUserActive.DoesNotExist as er: logger.error(er) logger.info('/api/v2/attendees/: {}'.format(time.time() - now)) return sorted(attendees, key=lambda x: (-x.score, -x.connected))
def test_exclude_friends(self): Goal.objects.create(user=self.user, goal=self.subject) Goal.objects.create(user=self.user1, goal=self.subject5) Goal.objects.create(user=self.user3, goal=self.subject5) FilterState.objects.create(user=self.user, min_age=18, max_age=99, distance=10000) self.neo.create_friendship(self.user, self.user3) update_index.Command().handle(interactive=False) match_users = MatchQuerySet.all(self.user.id) self.assertEqual(len(match_users), 1) self.assertEqual(match_users[0].first_name, 'Sasa')
def test_filter_two_keywords(self): Goal.objects.create(user=self.user, goal=self.subject) Goal.objects.create(user=self.user1, goal=self.subject5) Goal.objects.create(user=self.user1, goal=self.subject2) Goal.objects.create(user=self.user3, goal=self.subject5) FilterState.objects.create(user=self.user, min_age=18, max_age=99, keyword='python,ruby', distance=16516) update_index.Command().handle(interactive=False) match_users = MatchQuerySet.all(self.user.id, is_filter=True) self.assertEqual(len(match_users), 1) self.assertEqual(match_users[0].first_name, 'Sasa') self.assertEqual(match_users[0].goals, [{ u'learn python': 0, u'teach django': 1 }])
def test_top_interests(self): Goal.objects.create(user=self.user, goal=self.subject12) Goal.objects.create(user=self.user, goal=self.subject) Goal.objects.create(user=self.user, goal=self.subject2) Offer.objects.create(user=self.user, offer=self.subject3) Goal.objects.create(user=self.user1, goal=self.subject13) Goal.objects.create(user=self.user1, goal=self.subject5) Goal.objects.create(user=self.user1, goal=self.subject6) Offer.objects.create(user=self.user1, offer=self.subject7) update_index.Command().handle(interactive=False) match_users = MatchQuerySet.all(self.user.id) self.assertEqual(len(match_users), 1) self.assertEqual(match_users[0].top_interests, [{ u'child': 0, u'django': 1, u'erlang': 1, u'python': 1 }])
def refresh_cache(user_id=None): fs = FilterState.objects.filter(user=user_id) cache_match_users = None filter_updated_sha = None if fs: try: attrs = [fs[0].gender, fs[0].min_age, fs[0].max_age, fs[0].distance, fs[0].distance_unit, fs[0].order_criteria, fs[0].keyword] filter_updated = '.'.join(map(str, attrs)) filter_updated_sha = hashlib.sha1(filter_updated).hexdigest() # Concatenate all filters value instead!!! # m.1312.10000mi.sim # filter_updated = time.mktime(fs[0].updated.timetuple()) cache_match_users = cache.get('%s_%s' % (user_id, filter_updated_sha)) except AttributeError: pass if cache_match_users: pass else: match_users = MatchQuerySet.all(user_id, is_filter=True) cache.set('%s_%s' % (user_id, filter_updated_sha), match_users)
def test_simple_top_interests_less_than_1(self): Goal.objects.create(user=self.user, goal=self.subject) Goal.objects.create(user=self.user1, goal=self.subject10) update_index.Command().handle(interactive=False) match_users = MatchQuerySet.all(self.user.id) self.assertEqual(len(match_users), 0)
def test_highlight_simple_match_offer_query_set(self): Offer.objects.create(user=self.user, offer=self.subject) Offer.objects.create(user=self.user1, offer=self.subject5) update_index.Command().handle(interactive=False) users = MatchQuerySet.all(self.user.id) self.assertEqual(users[0].offers, [{u'teach django': 1}])
def test_simple_phrase(self): s = Subject.objects.create(description='foreign language') Goal.objects.get_or_create(user=self.user, goal=s) Goal.objects.get_or_create(user=self.user1, goal=s) update_index.Command().handle(interactive=False) match_users = MatchQuerySet.all(self.user.id, is_filter=True)
def test_highlight_simple_match_interest_query_set(self): Interest.objects.create(user=self.user, interest=self.i_subject) Interest.objects.create(user=self.user1, interest=self.i_subject1) update_index.Command().handle(interactive=False) users = MatchQuerySet.all(self.user.id) self.assertEqual(users[0].interests, [{u'learn django': 1}])