-
Notifications
You must be signed in to change notification settings - Fork 86
Add domain support #673
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add domain support #673
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Added Domain support. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| # Generated by Django 4.2.10 on 2024-05-30 17:53 | ||
|
|
||
| from django.db import migrations, models | ||
| import django.db.models.deletion | ||
| import pulpcore.app.util | ||
|
|
||
|
|
||
| class Migration(migrations.Migration): | ||
|
|
||
| dependencies = [ | ||
| ("python", "0011_alter_pythondistribution_distribution_ptr_and_more"), | ||
| ] | ||
|
|
||
| operations = [ | ||
| migrations.AlterUniqueTogether( | ||
| name="pythonpackagecontent", | ||
| unique_together=set(), | ||
| ), | ||
| migrations.AddField( | ||
| model_name="pythonpackagecontent", | ||
| name="_pulp_domain", | ||
| field=models.ForeignKey( | ||
| default=pulpcore.app.util.get_domain_pk, | ||
| on_delete=django.db.models.deletion.PROTECT, | ||
| to="core.domain", | ||
| ), | ||
| ), | ||
| migrations.AlterField( | ||
| model_name="pythonpackagecontent", | ||
| name="sha256", | ||
| field=models.CharField(db_index=True, max_length=64), | ||
| ), | ||
| migrations.AlterUniqueTogether( | ||
| name="pythonpackagecontent", | ||
| unique_together={("sha256", "_pulp_domain")}, | ||
| ), | ||
| ] |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -26,7 +26,9 @@ | |
| from pathlib import PurePath | ||
| from pypi_simple.parse_stream import parse_links_stream_response | ||
|
|
||
| from pulpcore.plugin.viewsets import OperationPostponedResponse | ||
| from pulpcore.plugin.tasking import dispatch | ||
| from pulpcore.plugin.util import get_domain | ||
| from pulp_python.app.models import ( | ||
| PythonDistribution, | ||
| PythonPackageContent, | ||
|
|
@@ -75,7 +77,7 @@ def get_distribution(path): | |
| "repository", "publication", "publication__repository_version", "remote" | ||
| ) | ||
| try: | ||
| return distro_qs.get(base_path=path) | ||
| return distro_qs.get(base_path=path, pulp_domain=get_domain()) | ||
| except ObjectDoesNotExist: | ||
| raise Http404(f"No PythonDistribution found for base_path {path}") | ||
|
|
||
|
|
@@ -100,6 +102,14 @@ def get_drvc(self, path): | |
| content = self.get_content(repo_ver) | ||
| return distro, repo_ver, content | ||
|
|
||
| def initial(self, request, *args, **kwargs): | ||
| """Perform common initialization tasks for PyPI endpoints.""" | ||
| super().initial(request, *args, **kwargs) | ||
| if settings.DOMAIN_ENABLED: | ||
| self.base_content_url = urljoin(BASE_CONTENT_URL, f"{get_domain().name}/") | ||
| else: | ||
| self.base_content_url = BASE_CONTENT_URL | ||
|
|
||
|
|
||
| class PackageUploadMixin(PyPIMixin): | ||
| """A Mixin to provide package upload support.""" | ||
|
|
@@ -137,7 +147,7 @@ def upload(self, request, path): | |
| kwargs={"artifact_sha256": artifact.sha256, | ||
| "filename": filename, | ||
| "repository_pk": str(repo.pk)}) | ||
| return Response(data={"task": reverse('tasks-detail', args=[result.pk], request=None)}) | ||
| return OperationPostponedResponse(result, request) | ||
|
|
||
| def upload_package_group(self, repo, artifact, filename, session): | ||
| """Steps 4 & 5, spawns tasks to add packages to index.""" | ||
|
|
@@ -176,7 +186,7 @@ def create_group_upload_task(self, cur_session, repository, artifact, filename, | |
| return reverse('tasks-detail', args=[result.pk], request=None) | ||
|
|
||
|
|
||
| class SimpleView(ViewSet, PackageUploadMixin): | ||
| class SimpleView(PackageUploadMixin, ViewSet): | ||
| """View for the PyPI simple API.""" | ||
|
|
||
| permission_classes = [IsAuthenticatedOrReadOnly] | ||
|
|
@@ -186,7 +196,7 @@ def list(self, request, path): | |
| """Gets the simple api html page for the index.""" | ||
| distro, repo_version, content = self.get_drvc(path) | ||
| if self.should_redirect(distro, repo_version=repo_version): | ||
| return redirect(urljoin(BASE_CONTENT_URL, f'{path}/simple/')) | ||
| return redirect(urljoin(self.base_content_url, f'{path}/simple/')) | ||
| names = content.order_by('name').values_list('name', flat=True).distinct().iterator() | ||
| return StreamingHttpResponse(write_simple_index(names, streamed=True)) | ||
|
|
||
|
|
@@ -197,7 +207,7 @@ def parse_url(link): | |
| digest, _, value = parsed.fragment.partition('=') | ||
| stripped_url = urlunsplit(chain(parsed[:3], ("", ""))) | ||
| redirect = f'{path}/{link.text}?redirect={stripped_url}' | ||
| d_url = urljoin(BASE_CONTENT_URL, redirect) | ||
| d_url = urljoin(self.base_content_url, redirect) | ||
| return link.text, d_url, value if digest == 'sha256' else '' | ||
|
|
||
| url = remote.get_remote_artifact_url(f'simple/{package}/') | ||
|
|
@@ -224,7 +234,7 @@ def retrieve(self, request, path, package): | |
| if not repo_ver or not content.filter(name__normalize=normalized).exists(): | ||
| return self.pull_through_package_simple(normalized, path, distro.remote) | ||
| if self.should_redirect(distro, repo_version=repo_ver): | ||
| return redirect(urljoin(BASE_CONTENT_URL, f'{path}/simple/{normalized}/')) | ||
| return redirect(urljoin(self.base_content_url, f'{path}/simple/{normalized}/')) | ||
| packages = ( | ||
| content.filter(name__normalize=normalized) | ||
| .values_list('filename', 'sha256', 'name') | ||
|
|
@@ -237,7 +247,7 @@ def retrieve(self, request, path, package): | |
| else: | ||
| packages = chain([present], packages) | ||
| name = present[2] | ||
| releases = ((f, urljoin(BASE_CONTENT_URL, f'{path}/{f}'), d) for f, d, _ in packages) | ||
| releases = ((f, urljoin(self.base_content_url, f'{path}/{f}'), d) for f, d, _ in packages) | ||
| return StreamingHttpResponse(write_simple_detail(name, releases, streamed=True)) | ||
|
|
||
| @extend_schema(request=PackageUploadSerializer, | ||
|
|
@@ -253,7 +263,7 @@ def create(self, request, path): | |
| return self.upload(request, path) | ||
|
|
||
|
|
||
| class MetadataView(ViewSet, PyPIMixin): | ||
| class MetadataView(PyPIMixin, ViewSet): | ||
| """View for the PyPI JSON metadata endpoint.""" | ||
|
|
||
| authentication_classes = [] | ||
|
|
@@ -272,6 +282,7 @@ def retrieve(self, request, path, meta): | |
| meta_path = PurePath(meta) | ||
| name = None | ||
| version = None | ||
| domain = None | ||
| if meta_path.match("*/*/json"): | ||
| version = meta_path.parts[1] | ||
| name = meta_path.parts[0] | ||
|
|
@@ -281,13 +292,17 @@ def retrieve(self, request, path, meta): | |
| package_content = content.filter(name__iexact=name) | ||
| # TODO Change this value to the Repo's serial value when implemented | ||
| headers = {PYPI_LAST_SERIAL: str(PYPI_SERIAL_CONSTANT)} | ||
| json_body = python_content_to_json(path, package_content, version=version) | ||
| if settings.DOMAIN_ENABLED: | ||
| domain = get_domain() | ||
| json_body = python_content_to_json( | ||
| path, package_content, version=version, domain=domain | ||
| ) | ||
| if json_body: | ||
| return Response(data=json_body, headers=headers) | ||
| return Response(status="404") | ||
|
|
||
|
|
||
| class PyPIView(ViewSet, PyPIMixin): | ||
| class PyPIView(PyPIMixin, ViewSet): | ||
| """View for base_url of distribution.""" | ||
|
|
||
| authentication_classes = [] | ||
|
|
@@ -305,7 +320,7 @@ def retrieve(self, request, path): | |
| return Response(data=data) | ||
|
|
||
|
|
||
| class UploadView(ViewSet, PackageUploadMixin): | ||
| class UploadView(PackageUploadMixin, ViewSet): | ||
|
Comment on lines
-308
to
+323
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This looks like an unrelated change. Is it where the 200 -> 202 in the tests come from?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is me fixing the inheritance of the classes the to be in the correct order. The 200 -> 202 change comes from using the pulpcore |
||
| """View for the `/legacy` upload endpoint.""" | ||
|
|
||
| @extend_schema(request=PackageUploadSerializer, | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey, yeah!