def test_create_flex_interest(self): EventManager.create(category='Category1', duration='40', expiration='180', username='******', building_name='building_1', note='test_note') EventManager.create(category='Category2', duration='40', expiration='180', username='******', building_name='building_1', note='test_note') EventManager.create(category='Category3', duration='40', expiration='180', username='******', building_name='building_1', note='test_note') EventManager.create(category='Category4', duration='40', expiration='180', username='******', building_name='building_2', note='test_note') EventManager.create(category='Category5', duration='40', expiration='180', username='******', building_name='building_2', note='test_note') self.assertEqual(3, len(Event.get_interests_by_building('building_1'))) self.assertEqual(2, len(Event.get_interests_by_building('building_2')))
def match_all(cls): """ Match between list of all valid interests and activities in the datastore. Match is made in groups of listed categorized by interest and activity category """ match_result_list = {} for category in cls._get_categories(): if category[0] == '': continue interest_list = Event.get_active_interests_by_category(category[0]) activity_list = Event.get_active_activities_by_category( category[0]) cls.match_interests_with_activities(interest_list, activity_list, match_result_list) return match_result_list
def test_create_specific_interest(self): EventManager.create(category='Category1', duration='40', expiration='180', username='******', building_name='building_1', min_number_of_people_to_join='1', max_number_of_people_to_join='2', note='note1') EventManager.create(category='Category2', duration='40', expiration='180', username='******', building_name='building_1', min_number_of_people_to_join='1', max_number_of_people_to_join='2', note='note2') EventManager.create(category='Category3', duration='40', expiration='180', username='******', building_name='building_1', min_number_of_people_to_join='1', max_number_of_people_to_join='2', note='note2') EventManager.create(category='Category4', duration='40', expiration='180', username='******', building_name='building_2', min_number_of_people_to_join='1', max_number_of_people_to_join='2', note='note3') EventManager.create(category='Category5', duration='40', expiration='180', username='******', building_name='building_2', min_number_of_people_to_join='1', max_number_of_people_to_join='2', note='note4') self.assertEqual(3, len(Event.get_activities_by_building('building_1'))) self.assertEqual(2, len(Event.get_activities_by_building('building_2')))
def match_activity_with_interests(cls, activity_key): """ Match between An activity and the list of valid interests in the data store for a building """ activity = ndb.Key(urlsafe=activity_key).get() interest_list = Event.get_active_interests_by_category_and_building( activity.category, activity.building_name) return cls.match_interests_with_activities(interest_list, [activity], {})
def match_interest_with_activities(cls, interest_key): """ Match between An interest and the list of valid activities in the store for a building """ interest = ndb.Key(urlsafe=interest_key).get() activity_list = Event.get_active_activities_by_category_and_building( interest.category, interest.building_name) return cls.match_interests_with_activities([interest], activity_list, {})
def number_of_events(self): #Get number of FORMING and events from User activity created in the last 5 minutes now = datetime.now() five_minutes_ago = now - timedelta(minutes=self.CONFIG_TIME) #For each activity, find it, and events = Event.get_latest_forming_activities(self._user.username, self._user.building, five_minutes_ago) return len(events)
def get(self): """ Returns a simple HTML for contact form """ user_info = models.User.get_by_id(long(self.request.get('user_id'))) self.view.activities = Event.query( Event.username == user_info.username, Event.type == Event.EVENT_TYPE_SPECIFIC_INTEREST).fetch() params = {"exception": self.request.get('exception')} return self.render_template('activity_list.html', **params)
def get(self): params = {} cursorStr = str(self.request.get('cursor')) user_info = models.User.get_by_id(long(self.user_id)) building_name = user_info.building self.view.building = building_name cursor = Cursor(urlsafe=cursorStr.lstrip('Cursor(').rstrip(')')) events, next_cursor, more = Event.query(Event.building_name == building_name).order(-Event.date_entered).fetch_page(15, start_cursor=cursor) event_attributes_list = dict() for event in events: event_attribute = event_attributes(event.key.urlsafe(),self.username) event_attributes_list[event.key.urlsafe()] = event_attribute self.view.events = events self.view.event_attributes_list = event_attributes_list self.view.cursor = next_cursor self.view.more = more if cursorStr is not None and cursorStr != "": return self.render_template('event_list.html', **params) else: return self.render_template('stat.html', **params)
def create(cls,**kwargs): ''' Creates an Interest based on the arguments passed in kwargs Once an interest is created is added to matchmaker queue :param cls: :param kwargs: :return: ''' eventThrottler = EventThrottler(kwargs['username']) # set the # of events to 3 instead of 5, and at join_flex_interest (for consistency). Throttler at 10 minutes if eventThrottler.number_of_cached_events() > 3: return False, "You have too many concurrent events.", None event = Event(category = kwargs['category'], date_entered = datetime.utcnow(), type = Event.EVENT_TYPE_FLEX_INTEREST, username = kwargs['username'], building_name = kwargs['building_name'], ) if 'expiration' in kwargs and kwargs['expiration'] != "": event.expiration = kwargs['expiration'] if 'start_time' in kwargs and kwargs['start_time'] != "": event.start_time = kwargs['start_time'] if 'duration' in kwargs and kwargs['duration'] != "": event.duration = kwargs['duration'] if 'min_number_of_people_to_join' in kwargs and kwargs['min_number_of_people_to_join'] != 'None' and kwargs['min_number_of_people_to_join'] != "": event.min_number_of_people_to_join = kwargs['min_number_of_people_to_join'] event.type = Event.EVENT_TYPE_SPECIFIC_INTEREST if 'max_number_of_people_to_join' in kwargs and kwargs['max_number_of_people_to_join'] != 'None' and kwargs['max_number_of_people_to_join'] != "": event.max_number_of_people_to_join = kwargs['max_number_of_people_to_join'] if 'note' in kwargs and kwargs['note'] != "": event.note = kwargs['note'] if 'meeting_place' in kwargs and kwargs['meeting_place'] != "": event.meeting_place = kwargs['meeting_place'] if 'activity_location' in kwargs and kwargs['activity_location'] != "": event.activity_location = kwargs['activity_location'] event.put() if os.environ.get('ENV_TYPE') is None: if event.type == Event.EVENT_TYPE_FLEX_INTEREST: task = Task(url='/match_maker/',method='GET',params={'interest': event.key.urlsafe()}) else: task = Task(url='/match_maker/',method='GET',params={'activity': event.key.urlsafe()}) task.add('matchmaker') logging.info('event created') logging.info('match maker task queued') eventThrottler.increment_activity_count() return True, 'success', event
def test_match_making(self): interest_list = [] activity_list = [] #Create some interests interest_list.append( EventManager.create(category='Play date', duration='40', expiration='180', username='******', building_name='building1', note='test_node')[2]) interest_list.append( EventManager.create(category='Play date', duration='40', expiration='180', username='******', building_name='building1', note='test_node')[2]) interest_list.append( EventManager.create(category='Play date', duration='20', expiration='180', username='******', building_name='building1', note='test_node')[2]) #Create some activities activity_list.append( EventManager.create(category='Play date', duration='40', expiration='180', username='******', building_name='building1', ip='127.0.0.1', min_number_of_people_to_join='1', max_number_of_people_to_join='1', note='meet me at shadyside')[2]) activity_list.append( EventManager.create(category='Stroller walk', duration='40', expiration='180', username='******', building_name='building1', ip='127.0.0.1', min_number_of_people_to_join='1', max_number_of_people_to_join='1', note='meet me at shadyside')[2]) #Assert if interests and activities are created fine self.assertEqual( 3, len(Event.get_active_interests_by_category('Play date'))) self.assertEqual( 1, len(Event.get_active_activities_by_category('Stroller walk'))) self.assertEqual( 1, len(Event.get_active_activities_by_category('Stroller walk'))) match_list = MatchMaker.match_all() self.assertEqual(2, len(match_list)) self.assertEqual(2, len(match_list['user1'])) self.assertEqual(1, len(match_list['user2'])) interest = EventManager.create(category='Stroller walk', duration='40', expiration='180', username='******', building_name='building1', note='test_node')[2] interest_list.append(interest) match_list = MatchMaker.match_interest_with_activities( interest.key.urlsafe()) self.assertEqual(1, len(match_list)) self.assertEqual(1, len(match_list['user1'])) activity = EventManager.create(category='Stroller walk', duration='40', expiration='180', username='******', building_name='building1', ip='127.0.0.1', min_number_of_people_to_join='1', max_number_of_people_to_join='1', note='meet me at shadyside')[2] match_list = MatchMaker.match_activity_with_interests( activity.key.urlsafe()) self.assertEqual(1, len(match_list)) activity = EventManager.create(category='Play date', duration='20', expiration='180', username='******', building_name='building1', ip='127.0.0.1', min_number_of_people_to_join='1', max_number_of_people_to_join='1', note='meet me at shadyside')[2] match_list = MatchMaker.match_activity_with_interests( activity.key.urlsafe()) self.assertEqual(2, len(match_list)) self.assertEqual(2, len(match_list['user1']))