Repository: google/personfinder

Issue:
Bring back support of "query" parameter for /results
We renamed "query" to "query_name" a while ago, but some third parties directly link to a URL with "query". So we should support both.

Example:
http://google.org/personfinder/japan/results?role=seek&lang=ja&query=mike


Discussion:

Resolving patch:
diff --git a/app/results.py b/app/results.py
--- a/app/results.py
+++ b/app/results.py
@@ -96,11 +96,12 @@ def search(self, query_dict):
                 results = indexing.search(self.repo,
                                           TextQuery(query_txt), MAX_RESULTS)
 
+        query_name = self.get_query_value()
         for result in results:
             result.view_url = self.get_url('/view',
                                            id=result.record_id,
                                            role=self.params.role,
-                                           query_name=self.params.query_name,
+                                           query_name=query_name,
                                            query_location=
                                                self.params.query_location,
                                            given_name=self.params.given_name,
@@ -119,6 +120,12 @@ def reject_query(self, query):
         return self.redirect(
             '/query', role=self.params.role, error='error', query=query.query)
 
+    def get_query_value(self):
+        # The query_name parameter used to be called simply "query", and we
+        # continue to accept it because some third-parties have links that
+        # depend on it.
+        return self.params.query_name or self.params.query
+
     def get_results_url(self, query_name, query_location):
         params = {
             'query_name': query_name,
@@ -185,17 +192,18 @@ def get(self):
             if results:
                 # Perhaps the person you wanted to report has already been
                 # reported?
-                return self.render('results.html',
-                                   results=results,
-                                   num_results=len(results),
-                                   has_possible_duplicates=
-                                       has_possible_duplicates(results),
-                                   results_url=results_url,
-                                   create_url=create_url,
-                                   third_party_search_engines=
-                                       third_party_search_engines,
-                                   query_name=self.params.query_name,
-                                   query_location=self.params.query_location,)
+                return self.render(
+                    'results.html',
+                    results=results,
+                    num_results=len(results),
+                    has_possible_duplicates=
+                        has_possible_duplicates(results),
+                    results_url=results_url,
+                    create_url=create_url,
+                    third_party_search_engines=
+                        third_party_search_engines,
+                    query_name=self.get_query_value(),
+                    query_location=self.params.query_location,)
             else:
                 if self.env.ui == 'small':
                     # show a link to a create page.
@@ -206,10 +214,13 @@ def get(self):
                     return self.redirect(create_url)
 
         if self.params.role == 'seek':
-            query_dict = {'name': self.params.query_name,
+            # The query_name parameter was previously called "query", and we
+            # continue to support it for third-parties that link directly to
+            # search results pages.
+            query_name = self.get_query_value()
+            query_dict = {'name': query_name,
                           'location': self.params.query_location}
-            self.params.query = " ".join(q for q in query_dict.values() if q)
-            query = TextQuery(self.params.query)
+            query = TextQuery(" ".join(q for q in query_dict.values() if q))
             results_based_on_input = True
 
             # If a query looks like a phone number, show the user a result
@@ -244,7 +255,7 @@ def get(self):
             else:
                 # Look for prefix matches.
                 results = self.search(query_dict)
-                results_url = self.get_results_url(self.params.query_name,
+                results_url = self.get_results_url(query_name,
                                                    self.params.query_location)
                 third_party_query_type = ''
 
@@ -252,14 +263,13 @@ def get(self):
                 # Check if there have results match for name
                 if not results:
                     if self.params.query_location:
-                        query_dict = {'name': self.params.query_name,
-                                      'location': ''}
+                        query_dict = {'name': query_name, 'location': ''}
                         results = self.search(query_dict)
                         # search result not based on the user input
                         results_based_on_input = False
 
             concatenated_query = (
-                ('%s %s' % (self.params.query_name, self.params.query_location))
+                ('%s %s' % (query_name, self.params.query_location))
                     .strip())
 
             # Show the (possibly empty) matches.
@@ -272,7 +282,7 @@ def get(self):
                                create_url=create_url,
                                third_party_search_engines=
                                    third_party_search_engines,
-                               query_name=self.params.query_name,
+                               query_name=query_name,
                                query=concatenated_query,
                                third_party_query_type=third_party_query_type,
                                results_based_on_input=results_based_on_input)


Test patch:
diff --git a/tests/server_test_cases/person_note_tests.py b/tests/server_test_cases/person_note_tests.py
--- a/tests/server_test_cases/person_note_tests.py
+++ b/tests/server_test_cases/person_note_tests.py
@@ -620,6 +620,25 @@ def assert_params(url=None):
         assert 'http://www.twitter.com/_test_account2' in self.s.doc.content
         assert 'http://www.foo.com/_test_account3' in self.s.doc.content
 
+    def test_seeking_someone_with_query_param(self):
+        """Visit the results page with the query (rather than query_name) param.
+        """
+        person = Person(
+            key_name='haiti:test.google.com/person.111',
+            repo='haiti',
+            author_name='_test_author_name',
+            author_email='test@example.com',
+            given_name='_test_given_name',
+            family_name='_test_family_name',
+            full_name='_test_given_name _test_family_name',
+            entry_date=datetime.datetime.utcnow(),
+            text='_test A note body')
+        person.update_index(['old', 'new'])
+        person.put()
+        self.go('/haiti/results?role=seek&query=_test_given_name')
+        link = self.s.doc.cssselect_one('a.result-link')
+        assert 'query_name=_test_given_name' in link.get('href')
+
     def test_seeking_someone_regular_by_full_text_search(self):
         config.set(enable_full_text_search = True)
         self.run_test_seeking_someone_regular()