def test_snapshot_person(self): fake_stdout = StringIO() # make fake Person who doesn't care if people know where he is zuckerberg = Person.create_dummy( first_name="mark", location_confirmed=True, location_display_name='Palo Alto', latitude=0, longitude=0) self.assertEquals( zuckerberg.get_public_location_or_default(), 'Palo Alto') # ...and make a fake Person who REALLY cares about his location being private munroe = Person.create_dummy(first_name="randall", location_confirmed=False, location_display_name='Cambridge', latitude=0, longitude=0) self.assertEquals(munroe.get_public_location_or_default(), 'Inaccessible Island') # Creating dummy tags, tags_persons and tagtypes # Dummy TagTypes tagtype_understands = TagType(name="understands") tagtype_understands.save() tagtype_can_mentor = TagType(name="can_mentor") tagtype_can_mentor.save() # Dummy Tags tag_facebook_development = Tag( text="Facebook development", tag_type=tagtype_understands) tag_facebook_development.save() tag_something_interesting = Tag( text="Something interesting", tag_type=tagtype_can_mentor) tag_something_interesting.save() # Dummy Links link_zuckerberg = Link_Person_Tag( person=zuckerberg, tag=tag_facebook_development) link_zuckerberg.save() link_munroe = Link_Person_Tag( person=munroe, tag=tag_something_interesting) link_munroe.save() command = mysite.customs.management.commands.snapshot_public_data.Command( ) command.handle(output=fake_stdout) # now, delete fake people zuckerberg.delete() munroe.delete() # ...and tags, tagtypes, and links too tag_facebook_development.delete() tag_something_interesting.delete() tagtype_understands.delete() tagtype_can_mentor.delete() link_zuckerberg.delete() link_munroe.delete() # and delete any User objects too django.contrib.auth.models.User.objects.all().delete() mysite.profile.models.Tag.objects.all().delete() mysite.profile.models.TagType.objects.all().delete() mysite.profile.models.Link_Person_Tag.objects.all().delete() # go go reincarnation gadget for obj in django.core.serializers.deserialize('json', fake_stdout.getvalue()): obj.save() # did we snapshot/save ANY Persons? self.assertTrue(Person.objects.all()) # did our fake Persons get saved? new_zuckerberg = mysite.profile.models.Person.objects.get( user__first_name="mark") new_munroe = mysite.profile.models.Person.objects.get( user__first_name="randall") # check that location_confirmed was saved accurately self.assertEquals(new_zuckerberg.location_confirmed, True) self.assertEquals(new_munroe.location_confirmed, False) # check that location_display_name is appropriate self.assertEquals(new_zuckerberg.location_display_name, 'Palo Alto') self.assertEquals(new_munroe.location_display_name, 'Inaccessible Island') # Check that Zuckerburg has a real lat/long self.assertNotEqual(mysite.profile.models.DEFAULT_LATITUDE, new_zuckerberg.latitude) self.assertNotEqual(mysite.profile.models.DEFAULT_LONGITUDE, new_zuckerberg.longitude) # Check that Randall has no lat/long self.assertEquals(mysite.profile.models.DEFAULT_LATITUDE, new_munroe.latitude) self.assertEquals(mysite.profile.models.DEFAULT_LONGITUDE, new_munroe.longitude) # check that we display both as appropriate self.assertEquals( new_zuckerberg.get_public_location_or_default(), 'Palo Alto') self.assertEquals( new_munroe.get_public_location_or_default(), 'Inaccessible Island') # get tags linked to our two dummy users... new_link_zuckerberg = mysite.profile.models.Link_Person_Tag.objects.get( id=new_zuckerberg.user_id) new_link_munroe = mysite.profile.models.Link_Person_Tag.objects.get( id=new_munroe.user_id) new_tag_facebook_development = mysite.profile.models.Tag.objects.get( link_person_tag__person=new_zuckerberg) new_tag_something_interesting = mysite.profile.models.Tag.objects.get( link_person_tag__person=new_munroe) # ...and tagtypes for the tags new_tagtype_understands = mysite.profile.models.TagType.objects.get( tag__tag_type=new_tag_facebook_development.tag_type) new_tagtype_can_mentor = mysite.profile.models.TagType.objects.get( tag__tag_type=new_tag_something_interesting.tag_type) # finally, check values self.assertEquals(new_link_zuckerberg.person, new_zuckerberg) self.assertEquals(new_link_munroe.person, new_munroe) self.assertEquals(new_tag_facebook_development.text, 'Facebook development') self.assertEquals(new_tag_something_interesting.text, 'Something interesting') self.assertEquals(new_tagtype_understands.name, 'understands') self.assertEquals(new_tagtype_can_mentor.name, 'can_mentor')
def people(request): """Display a list of people.""" data = {} # pull in q from GET query = request.GET.get('q', '') data['raw_query'] = query parsed_query = mysite.profile.controllers.parse_string_query(query) data.update(parsed_query) if parsed_query['query_type'] != 'project': # Figure out which projects happen to match that projects_that_match_q_exactly = [] for word in [parsed_query['q']]: # This is now tokenized smartly. name_matches = Project.objects.filter(name__iexact=word) for project in name_matches: if project.cached_contributor_count: # only add those projects that have people in them projects_that_match_q_exactly.append(project) data['projects_that_match_q_exactly'] = projects_that_match_q_exactly # Get the list of people to display. if parsed_query['q'].strip(): # "everybody" means everyone matching this query everybody, extra_data = query2results(parsed_query) data.update(extra_data) else: everybody = Person.objects.all().order_by('user__username') # filter by query, if it is set data['people'] = everybody get_relevant_person_data = lambda p: ( {'name': p.get_full_name_or_username(), 'location': p.get_public_location_or_default(), }) person_id2data = dict([(person.pk, get_relevant_person_data(person)) for person in everybody]) data['person_id2data_as_json'] = simplejson.dumps(person_id2data) data['test_js'] = request.GET.get('test', None) data['num_of_persons_with_locations'] = len(person_id2data) if request.GET.get('center', False): data['center_json'] = mysite.base.controllers.cached_geocoding_in_json( request.GET.get('center', '')) # the below is true when we fail to geocode the center that we got from GET if data['center_json'] == 'null': data['geocode_failed'] = True; data['center_name'] = request.GET.get('center', '') data['center_name_json'] = simplejson.dumps(request.GET.get('center', '')) data['show_everybody_javascript_boolean'] = simplejson.dumps(not data.get('center_json', False)) data['person_id2lat_long_as_json'] = simplejson.dumps( dict( (person_id, simplejson.loads(mysite.base.controllers.cached_geocoding_in_json(person_id2data[person_id]['location']))) for person_id in person_id2data)) suggestion_count = 6 cache_timespan = 86400 * 7 #if settings.DEBUG: # cache_timespan = 0 key_name = 'most_popular_projects_last_flushed_on_20100325' popular_projects = cache.get(key_name) if popular_projects is None: projects = Project.objects.all() popular_projects = sorted(projects, key=lambda proj: len(proj.get_contributors())*(-1))[:suggestion_count] #extract just the names from the projects popular_projects = [project.name for project in popular_projects] # cache it for a week cache.set(key_name, popular_projects, cache_timespan) key_name = 'most_popular_tags' popular_tags = cache.get(key_name) if popular_tags is None: # to get the most popular tags: # get all tags # order them by the number of people that list them # remove duplicates tags = Tag.objects.all() #lowercase them all and then remove duplicates tags_with_no_duplicates = list(set(map(lambda tag: tag.name.lower(), tags))) #take the popular ones popular_tags = sorted(tags_with_no_duplicates, key=lambda tag_name: len(Tag.get_people_by_tag_name(tag_name))*(-1))[:suggestion_count] # cache it for a week cache.set(key_name, popular_tags, cache_timespan) # Populate matching_project_suggestions # (unless query is icanhelp, in which case it's not relevant enough) if data['query_type'] == "icanhelp": data['matching_project_suggestions'] = [] else: data['matching_project_suggestions'] = Project.objects.filter( cached_contributor_count__gt=0, name__icontains=data['q']).filter( ~Q(name__iexact=data['q'])).order_by( '-cached_contributor_count') if data['people']: data['a_few_matching_project_suggestions'] = data['matching_project_suggestions'][:3] # What kind of people are these? if data['q']: if data['query_type'] == 'project': data['this_query_summary'] = 'who have contributed to ' data['query_is_a_project_name'] = True elif data['query_type'] == 'icanhelp': data['this_query_summary'] = 'willing to contribute to the project ' data['query_is_a_project_name'] = True elif data['query_type'] == 'all_tags': data['this_query_summary'] = 'who have listed' data['this_query_post_summary'] = ' on their profiles' elif data['query_type'] == 'understands_not': data['this_query_summary'] = 'tagged with understands_not = ' elif data['query_type'] == 'understands': data['this_query_summary'] = 'who understand ' elif data['query_type'] == 'studying': data['this_query_summary'] = 'who are currently studying ' else: long_name = mysite.profile.models.TagType.short_name2long_name[data['query_type']] data['this_query_summary'] = 'who ' + long_name if data['query_type'] == 'icanhelp' and not data['people']: data['total_query_summary'] = "No one has yet listed himself/herself as willing to contribute to the project <strong>%s</strong>." % data['q'] try: data['queried_project'] = Project.objects.get(name=data['q']) except Project.DoesNotExist: data['queried_project'] = None # If this is a project, determine how many people are listed as willing to # contribute to that project. if data['query_type'] == 'project' and data['queried_project']: data['icanhelp_count'] = data['queried_project'].people_who_wanna_help.all().count() if data['query_type'] == 'icanhelp' and not data['queried_project']: data['total_query_summary'] = "Sorry, we couldn't find a project named <strong>%s</strong>." % data['q'] data['suggestions'] = [ dict(display_name='projects', values=popular_projects, query_prefix='project:'), dict(display_name='profile tags', values=popular_tags, query_prefix='')] return (request, 'profile/search_people.html', data)
def people(request): """Display a list of people.""" data = {} # pull in q from GET query = request.GET.get('q', '') # Store the raw query in the template data data['raw_query'] = query # Parse the query, and store that in the template. parsed_query = mysite.profile.controllers.parse_string_query(query) data.update(parsed_query) # Get the list of people to display. if parsed_query['q'].strip(): try: # "everybody" means everyone matching this query everybody, extra_data = query2results(parsed_query) data.update(extra_data) except mysite.base.controllers.HaystackIsDown: data['haystack_is_down'] = True return (request, 'profile/search_people.html', data) # Note that if the search engine is down, nothing in this function # below this point will be incorporated into the template context. else: everybody = Person.objects.all().order_by('user__username') # filter by query, if it is set data['people'] = everybody cache_timespan = 86400 * 7 #if settings.DEBUG: # cache_timespan = 0 key_name = 'most_popular_projects_last_flushed_on_20100325' popular_projects = cache.get(key_name) if popular_projects is None: projects = Project.objects.all() popular_projects = sorted(projects, key=lambda proj: len(proj.get_contributors())*(-1))[:SUGGESTION_COUNT] #extract just the names from the projects popular_projects = [project.name for project in popular_projects] # cache it for a week cache.set(key_name, popular_projects, cache_timespan) key_name = 'most_popular_tags' popular_tags = cache.get(key_name) if popular_tags is None: # to get the most popular tags: # get all tags # order them by the number of people that list them # remove duplicates tags = Tag.objects.all() #lowercase them all and then remove duplicates tags_with_no_duplicates = list(set(map(lambda tag: tag.name.lower(), tags))) #take the popular ones popular_tags = sorted(tags_with_no_duplicates, key=lambda tag_name: len(Tag.get_people_by_tag_name(tag_name))*(-1))[:SUGGESTION_COUNT] # cache it for a week cache.set(key_name, popular_tags, cache_timespan) # Populate matching_project_suggestions # (unless query is icanhelp, in which case it's not relevant enough) if data['query_type'] == "icanhelp": data['matching_project_suggestions'] = [] else: mps1 = Project.objects.filter( cached_contributor_count__gt=0, name__icontains=data['q']).filter( ~Q(name__iexact=data['q'])).order_by( '-cached_contributor_count') mps2 = Project.objects.filter( cached_contributor_count__gt=0, display_name__icontains=data['q']).filter( ~Q(name__iexact=data['q'])).order_by( '-cached_contributor_count') data['matching_project_suggestions'] = mps1 | mps2 if data['people']: data['a_few_matching_project_suggestions'] = data['matching_project_suggestions'][:3] # What kind of people are these? if data['q']: data.update( mysite.profile.controllers.query_type2query_summary(data)) try: data['queried_project'] = Project.objects.get(name=data['q']) except Project.DoesNotExist: try: # See if what they have typed in matches a display name data['queried_project'] = Project.objects.get(display_name=data['q']) except Project.DoesNotExist: data['queried_project'] = None data['multiple_projects_matching'] = False except Project.MultipleObjectsReturned: data['queried_project'] = None data['multiple_projects_matching'] = True # If this is a project, determine how many people are listed as willing to # contribute to that project. if data['query_type'] == 'project' and data['queried_project']: data['icanhelp_count'] = data['queried_project'].people_who_wanna_help.all().count() if data['query_type'] == 'icanhelp' and not data['queried_project']: if data['multiple_projects_matching']: data['total_query_summary'] = "Your query <strong>%s</strong> matched multiple projects, which is incompatible with this search type." % data['q'] else: data['total_query_summary'] = "Sorry, we couldn't find a project named <strong>%s</strong>." % data['q'] data['suggestions'] = [ dict(display_name='projects', values=popular_projects, query_prefix='project:'), dict(display_name='profile tags', values=popular_tags, query_prefix='')] return (request, 'profile/search_people.html', data)
def people(request): """Display a list of people.""" data = {} # pull in q from GET query = request.GET.get('q', '') # Store the raw query in the template data data['raw_query'] = query # Parse the query, and store that in the template. parsed_query = mysite.profile.controllers.parse_string_query(query) data.update(parsed_query) # Get the list of people to display. if parsed_query['q'].strip(): try: # "everybody" means everyone matching this query everybody, extra_data = query2results(parsed_query) data.update(extra_data) except mysite.base.controllers.HaystackIsDown: data['haystack_is_down'] = True return (request, 'profile/search_people.html', data) # Note that if the search engine is down, nothing in this function # below this point will be incorporated into the template context. else: everybody = Person.objects.all().order_by('user__username') # filter by query, if it is set data['people'] = everybody cache_timespan = 86400 * 7 #if settings.DEBUG: # cache_timespan = 0 key_name = 'most_popular_projects_last_flushed_on_20100325' popular_projects = cache.get(key_name) if popular_projects is None: projects = Project.objects.all() popular_projects = sorted( projects, key=lambda proj: len(proj.get_contributors()) * (-1))[:SUGGESTION_COUNT] #extract just the names from the projects popular_projects = [project.name for project in popular_projects] # cache it for a week cache.set(key_name, popular_projects, cache_timespan) key_name = 'most_popular_tags' popular_tags = cache.get(key_name) if popular_tags is None: # to get the most popular tags: # get all tags # order them by the number of people that list them # remove duplicates tags = Tag.objects.all() #lowercase them all and then remove duplicates tags_with_no_duplicates = list( set(map(lambda tag: tag.name.lower(), tags))) #take the popular ones popular_tags = sorted( tags_with_no_duplicates, key=lambda tag_name: len(Tag.get_people_by_tag_name(tag_name)) * (-1))[:SUGGESTION_COUNT] # cache it for a week cache.set(key_name, popular_tags, cache_timespan) # Populate matching_project_suggestions # (unless query is icanhelp, in which case it's not relevant enough) if data['query_type'] == "icanhelp": data['matching_project_suggestions'] = [] else: mps1 = Project.objects.filter( cached_contributor_count__gt=0, name__icontains=data['q']).filter(~Q( name__iexact=data['q'])).order_by('-cached_contributor_count') mps2 = Project.objects.filter( cached_contributor_count__gt=0, display_name__icontains=data['q']).filter(~Q( name__iexact=data['q'])).order_by('-cached_contributor_count') data['matching_project_suggestions'] = mps1 | mps2 if data['people']: data['a_few_matching_project_suggestions'] = data[ 'matching_project_suggestions'][:3] # What kind of people are these? if data['q']: data.update(mysite.profile.controllers.query_type2query_summary(data)) try: data['queried_project'] = Project.objects.get(name=data['q']) except Project.DoesNotExist: try: # See if what they have typed in matches a display name data['queried_project'] = Project.objects.get( display_name=data['q']) except Project.DoesNotExist: data['queried_project'] = None data['multiple_projects_matching'] = False except Project.MultipleObjectsReturned: data['queried_project'] = None data['multiple_projects_matching'] = True # If this is a project, determine how many people are listed as willing to # contribute to that project. if data['query_type'] == 'project' and data['queried_project']: data['icanhelp_count'] = data[ 'queried_project'].people_who_wanna_help.all().count() if data['query_type'] == 'icanhelp' and not data['queried_project']: if data['multiple_projects_matching']: data[ 'total_query_summary'] = "Your query <strong>%s</strong> matched multiple projects, which is incompatible with this search type." % data[ 'q'] else: data[ 'total_query_summary'] = "Sorry, we couldn't find a project named <strong>%s</strong>." % data[ 'q'] data['suggestions'] = [ dict(display_name='projects', values=popular_projects, query_prefix='project:'), dict(display_name='profile tags', values=popular_tags, query_prefix='') ] return (request, 'profile/search_people.html', data)