We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
error 404 when searching for a package name with underscores
when I try to search for a package name which contains underscores I get a 404 error

example: https://pypi.io/search/?q=test_test

error 404 when searching for a package name with underscores
when I try to search for a package name which contains underscores I get a 404 error

example: https://pypi.io/search/?q=test_test


--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `warehouse/utils/paginate.py`
Content:
```
1 # Licensed under the Apache License, Version 2.0 (the "License");
2 # you may not use this file except in compliance with the License.
3 # You may obtain a copy of the License at
4 #
5 # http://www.apache.org/licenses/LICENSE-2.0
6 #
7 # Unless required by applicable law or agreed to in writing, software
8 # distributed under the License is distributed on an "AS IS" BASIS,
9 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10 # See the License for the specific language governing permissions and
11 # limitations under the License.
12 
13 from paginate import Page
14 
15 
16 class _ElasticsearchWrapper:
17 
18     max_results = 10000
19 
20     def __init__(self, query):
21         self.query = query
22         self.results = None
23         self.best_guess = None
24 
25     def __getitem__(self, range):
26         # If we're asking for a range that extends past our maximum results,
27         # then we need to clamp the start of our slice to our maximum results
28         # size, and make sure that the end of our slice >= to that to ensure a
29         # consistent slice.
30         if range.start > self.max_results:
31             range = slice(
32                 self.max_results,
33                 max(range.stop, self.max_results),
34                 range.step,
35             )
36 
37         # If we're being asked for a range that extends past our maximum result
38         # then we'll clamp it to the maximum result size and stop there.
39         if range.stop > self.max_results:
40             range = slice(range.start, self.max_results, range.step)
41 
42         if self.results is not None:
43             raise RuntimeError("Cannot reslice after having already sliced.")
44         self.results = self.query[range].execute()
45 
46         if hasattr(self.results, "suggest"):
47             suggestion = self.results.suggest.name_suggestion[0]
48             if suggestion.options:
49                 self.best_guess = suggestion.options[0]
50 
51         return list(self.results)
52 
53     def __len__(self):
54         if self.results is None:
55             raise RuntimeError("Cannot get length until a slice.")
56         return min(self.results.hits.total, self.max_results)
57 
58 
59 def ElasticsearchPage(*args, **kwargs):  # noqa
60     kwargs.setdefault("wrapper_class", _ElasticsearchWrapper)
61     return Page(*args, **kwargs)
62 
63 
64 def paginate_url_factory(request, query_arg="page"):
65     def make_url(page):
66         query_seq = [
67             (k, v)
68             for k, vs in request.GET.dict_of_lists().items()
69             for v in vs
70             if k != query_arg
71         ]
72         query_seq += [(query_arg, page)]
73         return request.current_route_path(_query=query_seq)
74     return make_url
75
```

Path: `warehouse/views.py`
Content:
```
1 # Licensed under the Apache License, Version 2.0 (the "License");
2 # you may not use this file except in compliance with the License.
3 # You may obtain a copy of the License at
4 #
5 # http://www.apache.org/licenses/LICENSE-2.0
6 #
7 # Unless required by applicable law or agreed to in writing, software
8 # distributed under the License is distributed on an "AS IS" BASIS,
9 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10 # See the License for the specific language governing permissions and
11 # limitations under the License.
12 
13 import collections
14 
15 from pyramid.httpexceptions import (
16     HTTPException, HTTPSeeOther, HTTPMovedPermanently, HTTPNotFound,
17 )
18 from pyramid.view import (
19     notfound_view_config, forbidden_view_config, view_config,
20 )
21 from sqlalchemy import func
22 from sqlalchemy.orm import aliased, joinedload
23 
24 from warehouse.accounts import REDIRECT_FIELD_NAME
25 from warehouse.accounts.models import User
26 from warehouse.cache.origin import origin_cache
27 from warehouse.cache.http import cache_control
28 from warehouse.classifiers.models import Classifier
29 from warehouse.packaging.models import Project, Release, File
30 from warehouse.utils.row_counter import RowCount
31 from warehouse.utils.paginate import ElasticsearchPage, paginate_url_factory
32 
33 
34 @view_config(context=HTTPException)
35 @notfound_view_config(append_slash=HTTPMovedPermanently)
36 def httpexception_view(exc, request):
37     return exc
38 
39 
40 @forbidden_view_config()
41 def forbidden(exc, request):
42     # If the forbidden error is because the user isn't logged in, then we'll
43     # redirect them to the log in page.
44     if request.authenticated_userid is None:
45         url = request.route_url(
46             "accounts.login",
47             _query={REDIRECT_FIELD_NAME: request.path_qs},
48         )
49         return HTTPSeeOther(url)
50 
51     # If we've reached here, then the user is logged in and they are genuinely
52     # not allowed to access this page.
53     # TODO: Style the forbidden page.
54     return exc
55 
56 
57 @view_config(
58     route_name="robots.txt",
59     renderer="robots.txt",
60     decorator=[
61         cache_control(1 * 24 * 60 * 60),         # 1 day
62         origin_cache(
63             1 * 24 * 60 * 60,                    # 1 day
64             stale_while_revalidate=6 * 60 * 60,  # 6 hours
65             stale_if_error=1 * 24 * 60 * 60,     # 1 day
66         ),
67     ],
68 )
69 def robotstxt(request):
70     request.response.content_type = "text/plain"
71     return {}
72 
73 
74 @view_config(
75     route_name="index",
76     renderer="index.html",
77     decorator=[
78         origin_cache(
79             1 * 60 * 60,                      # 1 hour
80             stale_while_revalidate=10 * 60,   # 10 minutes
81             stale_if_error=1 * 24 * 60 * 60,  # 1 day
82             keys=["all-projects"],
83         ),
84     ]
85 )
86 def index(request):
87     project_names = [
88         r[0] for r in (
89             request.db.query(File.name)
90                    .group_by(File.name)
91                    .order_by(func.sum(File.downloads).desc())
92                    .limit(5)
93                    .all())
94     ]
95     release_a = aliased(
96         Release,
97         request.db.query(Release)
98                   .distinct(Release.name)
99                   .filter(Release.name.in_(project_names))
100                   .order_by(Release.name, Release._pypi_ordering.desc())
101                   .subquery(),
102     )
103     top_projects = (
104         request.db.query(release_a)
105                .options(joinedload(release_a.project),
106                         joinedload(release_a.uploader))
107                .order_by(func.array_idx(project_names, release_a.name))
108                .all()
109     )
110 
111     latest_releases = (
112         request.db.query(Release)
113                   .options(joinedload(Release.project),
114                            joinedload(Release.uploader))
115                   .order_by(Release.created.desc())
116                   .limit(5)
117                   .all()
118     )
119 
120     counts = dict(
121         request.db.query(RowCount.table_name, RowCount.count)
122                   .filter(
123                       RowCount.table_name.in_([
124                           Project.__tablename__,
125                           Release.__tablename__,
126                           File.__tablename__,
127                           User.__tablename__,
128                       ]))
129                   .all()
130     )
131 
132     return {
133         "latest_releases": latest_releases,
134         "top_projects": top_projects,
135         "num_projects": counts.get(Project.__tablename__, 0),
136         "num_releases": counts.get(Release.__tablename__, 0),
137         "num_files": counts.get(File.__tablename__, 0),
138         "num_users": counts.get(User.__tablename__, 0),
139     }
140 
141 
142 @view_config(
143     route_name="search",
144     renderer="search/results.html",
145     decorator=[
146         origin_cache(
147             1 * 60 * 60,                      # 1 hour
148             stale_while_revalidate=10 * 60,   # 10 minutes
149             stale_if_error=1 * 24 * 60 * 60,  # 1 day
150             keys=["all-projects"],
151         )
152     ],
153 )
154 def search(request):
155     if request.params.get("q"):
156         query = request.es.query(
157             "multi_match",
158             query=request.params["q"],
159             fields=[
160                 "name^2", "version", "author", "author_email", "maintainer",
161                 "maintainer_email", "home_page", "license", "summary",
162                 "description", "keywords", "platform", "download_url",
163             ],
164         ).suggest(
165             name="name_suggestion",
166             text=request.params["q"],
167             term={"field": "name"}
168         )
169     else:
170         query = request.es.query()
171 
172     if request.params.get("o"):
173         query = query.sort(request.params["o"])
174 
175     if request.params.getall("c"):
176         query = query.filter('terms', classifiers=request.params.getall("c"))
177 
178     page_num = int(request.params.get("page", 1))
179     page = ElasticsearchPage(
180         query,
181         page=page_num,
182         url_maker=paginate_url_factory(request),
183     )
184 
185     if page_num > page.page_count:
186         raise HTTPNotFound
187 
188     available_filters = collections.defaultdict(list)
189 
190     for cls in request.db.query(Classifier).order_by(Classifier.classifier):
191         first, *_ = cls.classifier.split(' :: ')
192         available_filters[first].append(cls.classifier)
193 
194     return {
195         "page": page,
196         "term": request.params.get("q", ''),
197         "order": request.params.get("o", ''),
198         "available_filters": sorted(available_filters.items()),
199         "applied_filters": request.params.getall("c"),
200     }
201 
202 
203 @view_config(
204     route_name="includes.current-user-indicator",
205     renderer="includes/current-user-indicator.html",
206     uses_session=True,
207 )
208 def current_user_indicator(request):
209     return {}
210 
211 
212 @view_config(route_name="health", renderer="string")
213 def health(request):
214     # This will ensure that we can access the database and run queries against
215     # it without doing anything that will take a lock or block other queries.
216     request.db.execute("SELECT 1")
217 
218     # Nothing will actually check this, but it's a little nicer to have
219     # something to return besides an empty body.
220     return "OK"
221
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/warehouse/utils/paginate.py b/warehouse/utils/paginate.py
--- a/warehouse/utils/paginate.py
+++ b/warehouse/utils/paginate.py
@@ -44,9 +44,10 @@
         self.results = self.query[range].execute()
 
         if hasattr(self.results, "suggest"):
-            suggestion = self.results.suggest.name_suggestion[0]
-            if suggestion.options:
-                self.best_guess = suggestion.options[0]
+            if self.results.suggest.name_suggestion:
+                suggestion = self.results.suggest.name_suggestion[0]
+                if suggestion.options:
+                    self.best_guess = suggestion.options[0]
 
         return list(self.results)
 
diff --git a/warehouse/views.py b/warehouse/views.py
--- a/warehouse/views.py
+++ b/warehouse/views.py
@@ -182,7 +182,7 @@
         url_maker=paginate_url_factory(request),
     )
 
-    if page_num > page.page_count:
+    if page.page_count and page_num > page.page_count:
         raise HTTPNotFound
 
     available_filters = collections.defaultdict(list)

{"golden_diff": "diff --git a/warehouse/utils/paginate.py b/warehouse/utils/paginate.py\n--- a/warehouse/utils/paginate.py\n+++ b/warehouse/utils/paginate.py\n@@ -44,9 +44,10 @@\n         self.results = self.query[range].execute()\n \n         if hasattr(self.results, \"suggest\"):\n-            suggestion = self.results.suggest.name_suggestion[0]\n-            if suggestion.options:\n-                self.best_guess = suggestion.options[0]\n+            if self.results.suggest.name_suggestion:\n+                suggestion = self.results.suggest.name_suggestion[0]\n+                if suggestion.options:\n+                    self.best_guess = suggestion.options[0]\n \n         return list(self.results)\n \ndiff --git a/warehouse/views.py b/warehouse/views.py\n--- a/warehouse/views.py\n+++ b/warehouse/views.py\n@@ -182,7 +182,7 @@\n         url_maker=paginate_url_factory(request),\n     )\n \n-    if page_num > page.page_count:\n+    if page.page_count and page_num > page.page_count:\n         raise HTTPNotFound\n \n     available_filters = collections.defaultdict(list)\n", "issue": "error 404 when searching for a package name with underscores\nwhen I try to search for a package name which contains underscores I get a 404 error\n\nexample: https://pypi.io/search/?q=test_test\n\nerror 404 when searching for a package name with underscores\nwhen I try to search for a package name which contains underscores I get a 404 error\n\nexample: https://pypi.io/search/?q=test_test\n\n", "before_files": [{"content": "# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#\n# http://www.apache.org/licenses/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\n\nfrom paginate import Page\n\n\nclass _ElasticsearchWrapper:\n\n    max_results = 10000\n\n    def __init__(self, query):\n        self.query = query\n        self.results = None\n        self.best_guess = None\n\n    def __getitem__(self, range):\n        # If we're asking for a range that extends past our maximum results,\n        # then we need to clamp the start of our slice to our maximum results\n        # size, and make sure that the end of our slice >= to that to ensure a\n        # consistent slice.\n        if range.start > self.max_results:\n            range = slice(\n                self.max_results,\n                max(range.stop, self.max_results),\n                range.step,\n            )\n\n        # If we're being asked for a range that extends past our maximum result\n        # then we'll clamp it to the maximum result size and stop there.\n        if range.stop > self.max_results:\n            range = slice(range.start, self.max_results, range.step)\n\n        if self.results is not None:\n            raise RuntimeError(\"Cannot reslice after having already sliced.\")\n        self.results = self.query[range].execute()\n\n        if hasattr(self.results, \"suggest\"):\n            suggestion = self.results.suggest.name_suggestion[0]\n            if suggestion.options:\n                self.best_guess = suggestion.options[0]\n\n        return list(self.results)\n\n    def __len__(self):\n        if self.results is None:\n            raise RuntimeError(\"Cannot get length until a slice.\")\n        return min(self.results.hits.total, self.max_results)\n\n\ndef ElasticsearchPage(*args, **kwargs):  # noqa\n    kwargs.setdefault(\"wrapper_class\", _ElasticsearchWrapper)\n    return Page(*args, **kwargs)\n\n\ndef paginate_url_factory(request, query_arg=\"page\"):\n    def make_url(page):\n        query_seq = [\n            (k, v)\n            for k, vs in request.GET.dict_of_lists().items()\n            for v in vs\n            if k != query_arg\n        ]\n        query_seq += [(query_arg, page)]\n        return request.current_route_path(_query=query_seq)\n    return make_url\n", "path": "warehouse/utils/paginate.py"}, {"content": "# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#\n# http://www.apache.org/licenses/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\n\nimport collections\n\nfrom pyramid.httpexceptions import (\n    HTTPException, HTTPSeeOther, HTTPMovedPermanently, HTTPNotFound,\n)\nfrom pyramid.view import (\n    notfound_view_config, forbidden_view_config, view_config,\n)\nfrom sqlalchemy import func\nfrom sqlalchemy.orm import aliased, joinedload\n\nfrom warehouse.accounts import REDIRECT_FIELD_NAME\nfrom warehouse.accounts.models import User\nfrom warehouse.cache.origin import origin_cache\nfrom warehouse.cache.http import cache_control\nfrom warehouse.classifiers.models import Classifier\nfrom warehouse.packaging.models import Project, Release, File\nfrom warehouse.utils.row_counter import RowCount\nfrom warehouse.utils.paginate import ElasticsearchPage, paginate_url_factory\n\n\n@view_config(context=HTTPException)\n@notfound_view_config(append_slash=HTTPMovedPermanently)\ndef httpexception_view(exc, request):\n    return exc\n\n\n@forbidden_view_config()\ndef forbidden(exc, request):\n    # If the forbidden error is because the user isn't logged in, then we'll\n    # redirect them to the log in page.\n    if request.authenticated_userid is None:\n        url = request.route_url(\n            \"accounts.login\",\n            _query={REDIRECT_FIELD_NAME: request.path_qs},\n        )\n        return HTTPSeeOther(url)\n\n    # If we've reached here, then the user is logged in and they are genuinely\n    # not allowed to access this page.\n    # TODO: Style the forbidden page.\n    return exc\n\n\n@view_config(\n    route_name=\"robots.txt\",\n    renderer=\"robots.txt\",\n    decorator=[\n        cache_control(1 * 24 * 60 * 60),         # 1 day\n        origin_cache(\n            1 * 24 * 60 * 60,                    # 1 day\n            stale_while_revalidate=6 * 60 * 60,  # 6 hours\n            stale_if_error=1 * 24 * 60 * 60,     # 1 day\n        ),\n    ],\n)\ndef robotstxt(request):\n    request.response.content_type = \"text/plain\"\n    return {}\n\n\n@view_config(\n    route_name=\"index\",\n    renderer=\"index.html\",\n    decorator=[\n        origin_cache(\n            1 * 60 * 60,                      # 1 hour\n            stale_while_revalidate=10 * 60,   # 10 minutes\n            stale_if_error=1 * 24 * 60 * 60,  # 1 day\n            keys=[\"all-projects\"],\n        ),\n    ]\n)\ndef index(request):\n    project_names = [\n        r[0] for r in (\n            request.db.query(File.name)\n                   .group_by(File.name)\n                   .order_by(func.sum(File.downloads).desc())\n                   .limit(5)\n                   .all())\n    ]\n    release_a = aliased(\n        Release,\n        request.db.query(Release)\n                  .distinct(Release.name)\n                  .filter(Release.name.in_(project_names))\n                  .order_by(Release.name, Release._pypi_ordering.desc())\n                  .subquery(),\n    )\n    top_projects = (\n        request.db.query(release_a)\n               .options(joinedload(release_a.project),\n                        joinedload(release_a.uploader))\n               .order_by(func.array_idx(project_names, release_a.name))\n               .all()\n    )\n\n    latest_releases = (\n        request.db.query(Release)\n                  .options(joinedload(Release.project),\n                           joinedload(Release.uploader))\n                  .order_by(Release.created.desc())\n                  .limit(5)\n                  .all()\n    )\n\n    counts = dict(\n        request.db.query(RowCount.table_name, RowCount.count)\n                  .filter(\n                      RowCount.table_name.in_([\n                          Project.__tablename__,\n                          Release.__tablename__,\n                          File.__tablename__,\n                          User.__tablename__,\n                      ]))\n                  .all()\n    )\n\n    return {\n        \"latest_releases\": latest_releases,\n        \"top_projects\": top_projects,\n        \"num_projects\": counts.get(Project.__tablename__, 0),\n        \"num_releases\": counts.get(Release.__tablename__, 0),\n        \"num_files\": counts.get(File.__tablename__, 0),\n        \"num_users\": counts.get(User.__tablename__, 0),\n    }\n\n\n@view_config(\n    route_name=\"search\",\n    renderer=\"search/results.html\",\n    decorator=[\n        origin_cache(\n            1 * 60 * 60,                      # 1 hour\n            stale_while_revalidate=10 * 60,   # 10 minutes\n            stale_if_error=1 * 24 * 60 * 60,  # 1 day\n            keys=[\"all-projects\"],\n        )\n    ],\n)\ndef search(request):\n    if request.params.get(\"q\"):\n        query = request.es.query(\n            \"multi_match\",\n            query=request.params[\"q\"],\n            fields=[\n                \"name^2\", \"version\", \"author\", \"author_email\", \"maintainer\",\n                \"maintainer_email\", \"home_page\", \"license\", \"summary\",\n                \"description\", \"keywords\", \"platform\", \"download_url\",\n            ],\n        ).suggest(\n            name=\"name_suggestion\",\n            text=request.params[\"q\"],\n            term={\"field\": \"name\"}\n        )\n    else:\n        query = request.es.query()\n\n    if request.params.get(\"o\"):\n        query = query.sort(request.params[\"o\"])\n\n    if request.params.getall(\"c\"):\n        query = query.filter('terms', classifiers=request.params.getall(\"c\"))\n\n    page_num = int(request.params.get(\"page\", 1))\n    page = ElasticsearchPage(\n        query,\n        page=page_num,\n        url_maker=paginate_url_factory(request),\n    )\n\n    if page_num > page.page_count:\n        raise HTTPNotFound\n\n    available_filters = collections.defaultdict(list)\n\n    for cls in request.db.query(Classifier).order_by(Classifier.classifier):\n        first, *_ = cls.classifier.split(' :: ')\n        available_filters[first].append(cls.classifier)\n\n    return {\n        \"page\": page,\n        \"term\": request.params.get(\"q\", ''),\n        \"order\": request.params.get(\"o\", ''),\n        \"available_filters\": sorted(available_filters.items()),\n        \"applied_filters\": request.params.getall(\"c\"),\n    }\n\n\n@view_config(\n    route_name=\"includes.current-user-indicator\",\n    renderer=\"includes/current-user-indicator.html\",\n    uses_session=True,\n)\ndef current_user_indicator(request):\n    return {}\n\n\n@view_config(route_name=\"health\", renderer=\"string\")\ndef health(request):\n    # This will ensure that we can access the database and run queries against\n    # it without doing anything that will take a lock or block other queries.\n    request.db.execute(\"SELECT 1\")\n\n    # Nothing will actually check this, but it's a little nicer to have\n    # something to return besides an empty body.\n    return \"OK\"\n", "path": "warehouse/views.py"}], "after_files": [{"content": "# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#\n# http://www.apache.org/licenses/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\n\nfrom paginate import Page\n\n\nclass _ElasticsearchWrapper:\n\n    max_results = 10000\n\n    def __init__(self, query):\n        self.query = query\n        self.results = None\n        self.best_guess = None\n\n    def __getitem__(self, range):\n        # If we're asking for a range that extends past our maximum results,\n        # then we need to clamp the start of our slice to our maximum results\n        # size, and make sure that the end of our slice >= to that to ensure a\n        # consistent slice.\n        if range.start > self.max_results:\n            range = slice(\n                self.max_results,\n                max(range.stop, self.max_results),\n                range.step,\n            )\n\n        # If we're being asked for a range that extends past our maximum result\n        # then we'll clamp it to the maximum result size and stop there.\n        if range.stop > self.max_results:\n            range = slice(range.start, self.max_results, range.step)\n\n        if self.results is not None:\n            raise RuntimeError(\"Cannot reslice after having already sliced.\")\n        self.results = self.query[range].execute()\n\n        if hasattr(self.results, \"suggest\"):\n            if self.results.suggest.name_suggestion:\n                suggestion = self.results.suggest.name_suggestion[0]\n                if suggestion.options:\n                    self.best_guess = suggestion.options[0]\n\n        return list(self.results)\n\n    def __len__(self):\n        if self.results is None:\n            raise RuntimeError(\"Cannot get length until a slice.\")\n        return min(self.results.hits.total, self.max_results)\n\n\ndef ElasticsearchPage(*args, **kwargs):  # noqa\n    kwargs.setdefault(\"wrapper_class\", _ElasticsearchWrapper)\n    return Page(*args, **kwargs)\n\n\ndef paginate_url_factory(request, query_arg=\"page\"):\n    def make_url(page):\n        query_seq = [\n            (k, v)\n            for k, vs in request.GET.dict_of_lists().items()\n            for v in vs\n            if k != query_arg\n        ]\n        query_seq += [(query_arg, page)]\n        return request.current_route_path(_query=query_seq)\n    return make_url\n", "path": "warehouse/utils/paginate.py"}, {"content": "# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#\n# http://www.apache.org/licenses/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\n\nimport collections\n\nfrom pyramid.httpexceptions import (\n    HTTPException, HTTPSeeOther, HTTPMovedPermanently, HTTPNotFound,\n)\nfrom pyramid.view import (\n    notfound_view_config, forbidden_view_config, view_config,\n)\nfrom sqlalchemy import func\nfrom sqlalchemy.orm import aliased, joinedload\n\nfrom warehouse.accounts import REDIRECT_FIELD_NAME\nfrom warehouse.accounts.models import User\nfrom warehouse.cache.origin import origin_cache\nfrom warehouse.cache.http import cache_control\nfrom warehouse.classifiers.models import Classifier\nfrom warehouse.packaging.models import Project, Release, File\nfrom warehouse.utils.row_counter import RowCount\nfrom warehouse.utils.paginate import ElasticsearchPage, paginate_url_factory\n\n\n@view_config(context=HTTPException)\n@notfound_view_config(append_slash=HTTPMovedPermanently)\ndef httpexception_view(exc, request):\n    return exc\n\n\n@forbidden_view_config()\ndef forbidden(exc, request):\n    # If the forbidden error is because the user isn't logged in, then we'll\n    # redirect them to the log in page.\n    if request.authenticated_userid is None:\n        url = request.route_url(\n            \"accounts.login\",\n            _query={REDIRECT_FIELD_NAME: request.path_qs},\n        )\n        return HTTPSeeOther(url)\n\n    # If we've reached here, then the user is logged in and they are genuinely\n    # not allowed to access this page.\n    # TODO: Style the forbidden page.\n    return exc\n\n\n@view_config(\n    route_name=\"robots.txt\",\n    renderer=\"robots.txt\",\n    decorator=[\n        cache_control(1 * 24 * 60 * 60),         # 1 day\n        origin_cache(\n            1 * 24 * 60 * 60,                    # 1 day\n            stale_while_revalidate=6 * 60 * 60,  # 6 hours\n            stale_if_error=1 * 24 * 60 * 60,     # 1 day\n        ),\n    ],\n)\ndef robotstxt(request):\n    request.response.content_type = \"text/plain\"\n    return {}\n\n\n@view_config(\n    route_name=\"index\",\n    renderer=\"index.html\",\n    decorator=[\n        origin_cache(\n            1 * 60 * 60,                      # 1 hour\n            stale_while_revalidate=10 * 60,   # 10 minutes\n            stale_if_error=1 * 24 * 60 * 60,  # 1 day\n            keys=[\"all-projects\"],\n        ),\n    ]\n)\ndef index(request):\n    project_names = [\n        r[0] for r in (\n            request.db.query(File.name)\n                   .group_by(File.name)\n                   .order_by(func.sum(File.downloads).desc())\n                   .limit(5)\n                   .all())\n    ]\n    release_a = aliased(\n        Release,\n        request.db.query(Release)\n                  .distinct(Release.name)\n                  .filter(Release.name.in_(project_names))\n                  .order_by(Release.name, Release._pypi_ordering.desc())\n                  .subquery(),\n    )\n    top_projects = (\n        request.db.query(release_a)\n               .options(joinedload(release_a.project),\n                        joinedload(release_a.uploader))\n               .order_by(func.array_idx(project_names, release_a.name))\n               .all()\n    )\n\n    latest_releases = (\n        request.db.query(Release)\n                  .options(joinedload(Release.project),\n                           joinedload(Release.uploader))\n                  .order_by(Release.created.desc())\n                  .limit(5)\n                  .all()\n    )\n\n    counts = dict(\n        request.db.query(RowCount.table_name, RowCount.count)\n                  .filter(\n                      RowCount.table_name.in_([\n                          Project.__tablename__,\n                          Release.__tablename__,\n                          File.__tablename__,\n                          User.__tablename__,\n                      ]))\n                  .all()\n    )\n\n    return {\n        \"latest_releases\": latest_releases,\n        \"top_projects\": top_projects,\n        \"num_projects\": counts.get(Project.__tablename__, 0),\n        \"num_releases\": counts.get(Release.__tablename__, 0),\n        \"num_files\": counts.get(File.__tablename__, 0),\n        \"num_users\": counts.get(User.__tablename__, 0),\n    }\n\n\n@view_config(\n    route_name=\"search\",\n    renderer=\"search/results.html\",\n    decorator=[\n        origin_cache(\n            1 * 60 * 60,                      # 1 hour\n            stale_while_revalidate=10 * 60,   # 10 minutes\n            stale_if_error=1 * 24 * 60 * 60,  # 1 day\n            keys=[\"all-projects\"],\n        )\n    ],\n)\ndef search(request):\n    if request.params.get(\"q\"):\n        query = request.es.query(\n            \"multi_match\",\n            query=request.params[\"q\"],\n            fields=[\n                \"name^2\", \"version\", \"author\", \"author_email\", \"maintainer\",\n                \"maintainer_email\", \"home_page\", \"license\", \"summary\",\n                \"description\", \"keywords\", \"platform\", \"download_url\",\n            ],\n        ).suggest(\n            name=\"name_suggestion\",\n            text=request.params[\"q\"],\n            term={\"field\": \"name\"}\n        )\n    else:\n        query = request.es.query()\n\n    if request.params.get(\"o\"):\n        query = query.sort(request.params[\"o\"])\n\n    if request.params.getall(\"c\"):\n        query = query.filter('terms', classifiers=request.params.getall(\"c\"))\n\n    page_num = int(request.params.get(\"page\", 1))\n    page = ElasticsearchPage(\n        query,\n        page=page_num,\n        url_maker=paginate_url_factory(request),\n    )\n\n    if page.page_count and page_num > page.page_count:\n        raise HTTPNotFound\n\n    available_filters = collections.defaultdict(list)\n\n    for cls in request.db.query(Classifier).order_by(Classifier.classifier):\n        first, *_ = cls.classifier.split(' :: ')\n        available_filters[first].append(cls.classifier)\n\n    return {\n        \"page\": page,\n        \"term\": request.params.get(\"q\", ''),\n        \"order\": request.params.get(\"o\", ''),\n        \"available_filters\": sorted(available_filters.items()),\n        \"applied_filters\": request.params.getall(\"c\"),\n    }\n\n\n@view_config(\n    route_name=\"includes.current-user-indicator\",\n    renderer=\"includes/current-user-indicator.html\",\n    uses_session=True,\n)\ndef current_user_indicator(request):\n    return {}\n\n\n@view_config(route_name=\"health\", renderer=\"string\")\ndef health(request):\n    # This will ensure that we can access the database and run queries against\n    # it without doing anything that will take a lock or block other queries.\n    request.db.execute(\"SELECT 1\")\n\n    # Nothing will actually check this, but it's a little nicer to have\n    # something to return besides an empty body.\n    return \"OK\"\n", "path": "warehouse/views.py"}]}