def test_admin_creates_keys(self): self.be_admin_user() get_response = self.testapp.get('/admin') self.assertEqual(get_response.status_int, 200) form = get_response.forms["private-key"] self.assertEqual(form.method, 'POST') form['oauth_key'] = 'oauth' form['api_key'] = 'api' post_response = form.submit() self.assertEqual(post_response.status_int, 302) self.assertEqual(post_response.headers['Location'], 'http://localhost:80/admin#tab-private-keys') self.assertEqual(PrivateKey.get_oauth(), 'oauth') self.assertEqual(PrivateKey.get_api(), 'api')
def _query_search(self, query, page): """Call the Custom Search API with the given query and get the given page of results. Return a tuple. The first element is the list of search results for the requested page. Each result is a map that can be passed to the search result template. The second element is the total count of search results. The count will always be the total count of all results starting from the beginning, regardless of the page being requested. For each search result, this looks up the actual package in the data store and returns its live metadata. If the URL cannot be parsed or the package cannot be found in the datastore, it is not included in the results. Arguments: query: The search query to perform. page: The page of results to return. If out of bounds, returns an empty collection of results. """ global _search_service if page < 1 or page > MAX_RESULTS / RESULTS_PER_PAGE: handlers.http_error(400, "Page \"%d\" is out of bounds." % page) start = (page - 1) * RESULTS_PER_PAGE + 1 results = [] if _mock_resource: resource = _mock_resource else: if not _search_service: _search_service = build("customsearch", "v1", developerKey=PrivateKey.get_api()) resource = _search_service.cse().list(q=query, cx=CUSTOM_SEARCH_ID, num=RESULTS_PER_PAGE, start=start).execute() if "items" in resource: for item in resource["items"]: result = self._get_result(item) if result: results.append(result) count = int(resource["searchInformation"]["totalResults"]) return results, count
def requires_api_key(fn, *args, **kwargs): """A decorator for actions that require a Google API key to be set.""" if PrivateKey.get_api() is None: http_error(500, 'No private Google API key set.') return fn(*args, **kwargs)