Skip to content

Commit 581e20e

Browse files
committed
helper function to render and hash simple detail for a specific project
1 parent 0fa28d4 commit 581e20e

File tree

2 files changed

+51
-11
lines changed

2 files changed

+51
-11
lines changed

warehouse/legacy/api/simple.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,20 @@ def simple_index(request):
4949
return {"projects": projects}
5050

5151

52+
def _simple_detail(project, request):
53+
# Get all of the files for this project.
54+
files = sorted(
55+
request.db.query(File)
56+
.options(joinedload(File.release))
57+
.join(Release)
58+
.filter(Release.project == project)
59+
.all(),
60+
key=lambda f: (parse(f.release.version), f.filename),
61+
)
62+
63+
return {"project": project, "files": files}
64+
65+
5266
@view_config(
5367
route_name="legacy.api.simple.detail",
5468
context=Project,
@@ -74,14 +88,4 @@ def simple_detail(project, request):
7488
# Get the latest serial number for this project.
7589
request.response.headers["X-PyPI-Last-Serial"] = str(project.last_serial)
7690

77-
# Get all of the files for this project.
78-
files = sorted(
79-
request.db.query(File)
80-
.options(joinedload(File.release))
81-
.join(Release)
82-
.filter(Release.project == project)
83-
.all(),
84-
key=lambda f: (parse(f.release.version), f.filename),
85-
)
86-
87-
return {"project": project, "files": files}
91+
return _simple_detail(project, request)

warehouse/packaging/utils.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import hashlib
2+
import os.path
3+
4+
from jinja2 import Environment, FileSystemLoader
5+
6+
import warehouse
7+
8+
from warehouse.legacy.api.simple import _simple_detail
9+
10+
11+
def render_simple_detail(project, request, store=False):
12+
context = _simple_detail(project, request)
13+
14+
# TODO: use pyramid_jinja2 "get_jinja2_environment" method instead:
15+
# https://docs.pylonsproject.org/projects/pyramid_jinja2/en/latest/api.html#pyramid_jinja2.get_jinja2_environment
16+
dir_name = os.path.join(os.path.dirname(warehouse.__file__), "templates")
17+
env = Environment(
18+
loader=FileSystemLoader(dir_name),
19+
extensions=[],
20+
cache_size=0,
21+
)
22+
23+
template = env.get_template("legacy/api/simple/detail.html")
24+
content = template.render(**context, request=request)
25+
26+
content_hasher = hashlib.blake2b(digest_size=256 // 8)
27+
content_hasher.update(content.encode("utf-8"))
28+
content_hash = content_hasher.hexdigest().lower()
29+
simple_detail_path = f"/simple/{project.normalized_name}/{content_hash}/"
30+
31+
if store:
32+
# TODO: Store generated file in FileStorage
33+
# We should probably configure a new FileStorage for a new simple-files bucket in GCS
34+
pass
35+
36+
return (content_hash, simple_detail_path)

0 commit comments

Comments
 (0)