Skip to content

Commit b0d5dc8

Browse files
committed
tmp
1 parent 583f3fe commit b0d5dc8

File tree

2 files changed

+78
-13
lines changed

2 files changed

+78
-13
lines changed

pulp_python/app/pypi/views.py

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ def list(self, request, path):
287287
kwargs = {"content_type": media_type}
288288
return StreamingHttpResponse(index_data, **kwargs)
289289

290-
def pull_through_package_simple(self, package, path, remote):
290+
def pull_through_package_simple(self, package, path, remote, media_type):
291291
"""Gets the package's simple page from remote."""
292292

293293
def parse_package(release_package):
@@ -299,7 +299,8 @@ def parse_package(release_package):
299299
"filename": release_package.filename,
300300
"url": d_url,
301301
"sha256": release_package.digests.get("sha256", ""),
302-
# todo: more fields?
302+
"requires_python": release_package.requires_python,
303+
"metadata_sha256": (release_package.metadata_digests or {}).get("sha256", ""),
303304
}
304305

305306
rfilter = get_remote_package_filter(remote)
@@ -324,27 +325,33 @@ def parse_package(release_package):
324325
packages = [
325326
parse_package(p) for p in page.packages if rfilter.filter_release(package, p.version)
326327
]
327-
return HttpResponse(write_simple_detail(package, packages))
328+
329+
if media_type == PYPI_SIMPLE_V1_JSON:
330+
detail_data = write_simple_detail_json(package, packages)
331+
headers = {"X-PyPI-Last-Serial": str(PYPI_SERIAL_CONSTANT)}
332+
return Response(detail_data, headers=headers)
333+
else:
334+
detail_data = write_simple_detail(package, packages)
335+
kwargs = {"content_type": media_type}
336+
return HttpResponse(detail_data, **kwargs) # todo: streamed?
328337

329338
@extend_schema(operation_id="pypi_simple_package_read", summary="Get package simple page")
330339
def retrieve(self, request, path, package):
331-
"""Retrieves the simple api html page for a package."""
340+
"""Retrieves the simple api html/json page for a package."""
341+
media_type = request.accepted_renderer.media_type
342+
332343
repo_ver, content = self.get_rvc()
333344
# Should I redirect if the normalized name is different?
334345
normalized = canonicalize_name(package)
335346
if self.distribution.remote:
336-
return self.pull_through_package_simple(normalized, path, self.distribution.remote)
347+
return self.pull_through_package_simple(
348+
normalized, path, self.distribution.remote, media_type
349+
)
337350
if self.should_redirect(repo_version=repo_ver):
338351
return redirect(urljoin(self.base_content_url, f"{path}/simple/{normalized}/"))
339352
packages = (
340353
content.filter(name__normalize=normalized)
341-
.values_list(
342-
"filename",
343-
"sha256",
344-
"name",
345-
"metadata_sha256",
346-
"requires_python",
347-
)
354+
.values_list("filename", "sha256", "name", "metadata_sha256", "requires_python")
348355
.iterator()
349356
)
350357
try:
@@ -373,7 +380,7 @@ def retrieve(self, request, path, package):
373380
else:
374381
detail_data = write_simple_detail(name, releases, streamed=True)
375382
kwargs = {"content_type": media_type}
376-
return StreamingHttpResponse(detail_data, kwargs)
383+
return StreamingHttpResponse(detail_data, **kwargs)
377384

378385
@extend_schema(
379386
request=PackageUploadSerializer,

pulp_python/tests/functional/api/test_full_mirror.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from random import sample
1616

1717

18+
# json
1819
def test_pull_through_install(
1920
python_bindings, python_remote_factory, python_distribution_factory, delete_orphans_pre
2021
):
@@ -41,6 +42,7 @@ def test_pull_through_install(
4142
assert content.count == 1
4243

4344

45+
# json
4446
@pytest.mark.parallel
4547
def test_pull_through_simple(python_remote_factory, python_distribution_factory, pulp_content_url):
4648
"""Tests that the simple page is properly modified when requesting a pull-through."""
@@ -58,6 +60,61 @@ def test_pull_through_simple(python_remote_factory, python_distribution_factory,
5860
assert PYTHON_XS_FIXTURE_CHECKSUMS[package.filename] == package.digests["sha256"]
5961

6062

63+
@pytest.mark.parallel
64+
def test_pull_through_simple_html(
65+
python_remote_factory, python_distribution_factory, pulp_content_url
66+
):
67+
"""Tests that the simple HTML page works for pull-through."""
68+
media_type = "text/html"
69+
remote = python_remote_factory(url=PYPI_URL, includes=["shelf-reader"])
70+
distro = python_distribution_factory(remote=remote.pulp_href)
71+
72+
url = f"{distro.base_url}simple/shelf-reader/"
73+
headers = {"Accept": media_type}
74+
response = requests.get(url, headers=headers)
75+
76+
assert response.status_code == 200
77+
assert media_type in response.headers["Content-Type"]
78+
# assert "X-PyPI-Last-Serial" in response.headers
79+
80+
project_page = ProjectPage.from_response(response, "shelf-reader")
81+
assert len(project_page.packages) == 2
82+
83+
for package in project_page.packages:
84+
assert package.filename in PYTHON_XS_FIXTURE_CHECKSUMS
85+
relative_path = f"{distro.base_path}/{package.filename}?redirect="
86+
assert urljoin(pulp_content_url, relative_path) in package.url
87+
assert PYTHON_XS_FIXTURE_CHECKSUMS[package.filename] == package.digests["sha256"]
88+
89+
90+
@pytest.mark.parallel
91+
def test_pull_through_simple_json(
92+
python_remote_factory, python_distribution_factory, pulp_content_url
93+
):
94+
"""Tests that the simple JSON API page works for pull-through."""
95+
media_type = "application/vnd.pypi.simple.v1+json"
96+
remote = python_remote_factory(url=PYPI_URL, includes=["shelf-reader"])
97+
distro = python_distribution_factory(remote=remote.pulp_href)
98+
99+
url = f"{distro.base_url}simple/shelf-reader/"
100+
headers = {"Accept": media_type}
101+
response = requests.get(url, headers=headers)
102+
103+
assert response.status_code == 200
104+
assert media_type in response.headers["Content-Type"]
105+
assert "X-PyPI-Last-Serial" in response.headers
106+
107+
project_page = ProjectPage.from_response(response, "shelf-reader")
108+
assert len(project_page.packages) == 2
109+
110+
for package in project_page.packages:
111+
assert package.filename in PYTHON_XS_FIXTURE_CHECKSUMS
112+
relative_path = f"{distro.base_path}/{package.filename}?redirect="
113+
assert urljoin(pulp_content_url, relative_path) in package.url
114+
assert PYTHON_XS_FIXTURE_CHECKSUMS[package.filename] == package.digests["sha256"]
115+
116+
117+
# json, html
61118
@pytest.mark.parallel
62119
def test_pull_through_filter(python_remote_factory, python_distribution_factory):
63120
"""Tests that pull-through respects the includes/excludes filter on the remote."""
@@ -96,6 +153,7 @@ def test_pull_through_filter(python_remote_factory, python_distribution_factory)
96153
assert r.status_code == 200
97154

98155

156+
# html
99157
@pytest.mark.parallel
100158
def test_pull_through_with_repo(
101159
python_repo_factory,

0 commit comments

Comments
 (0)