|
| 1 | +import logging |
1 | 2 | from gettext import gettext as _ |
2 | 3 | from django.conf import settings |
| 4 | +from django.db.utils import IntegrityError |
3 | 5 | from packaging.requirements import Requirement |
4 | 6 | from rest_framework import serializers |
5 | 7 |
|
|
8 | 10 | from pulpcore.plugin.util import get_domain |
9 | 11 |
|
10 | 12 | from pulp_python.app import models as python_models |
11 | | -from pulp_python.app.utils import artifact_to_python_content_data |
| 13 | +from pulp_python.app.utils import ( |
| 14 | + DIST_EXTENSIONS, |
| 15 | + artifact_to_python_content_data, |
| 16 | + get_project_metadata_from_file, |
| 17 | +) |
| 18 | + |
| 19 | +log = logging.getLogger(__name__) |
12 | 20 |
|
13 | 21 |
|
14 | 22 | class PythonRepositorySerializer(core_serializers.RepositorySerializer): |
@@ -215,7 +223,7 @@ class PythonPackageContentSerializer(core_serializers.SingleArtifactContentUploa |
215 | 223 | required=False, |
216 | 224 | allow_blank=True, |
217 | 225 | help_text=_( |
218 | | - "A string stating the markup syntax (if any) used in the distribution’s" |
| 226 | + "A string stating the markup syntax (if any) used in the distribution's" |
219 | 227 | " description, so that tools can intelligently render the description." |
220 | 228 | ), |
221 | 229 | ) |
@@ -357,6 +365,55 @@ class Meta: |
357 | 365 | model = python_models.PythonPackageContent |
358 | 366 |
|
359 | 367 |
|
| 368 | +class PythonPackageContentUploadSerializer(PythonPackageContentSerializer): |
| 369 | + """ |
| 370 | + A serializer for requests to synchronously upload Python packages. |
| 371 | + """ |
| 372 | + |
| 373 | + class Meta(PythonPackageContentSerializer.Meta): |
| 374 | + # This API does not support uploading to a repository or using a custom relative_path |
| 375 | + fields = tuple( |
| 376 | + f |
| 377 | + for f in PythonPackageContentSerializer.Meta.fields |
| 378 | + if f not in ["repository", "relative_path"] |
| 379 | + ) |
| 380 | + model = python_models.PythonPackageContent |
| 381 | + # Name used for the OpenAPI request object |
| 382 | + ref_name = "PythonPackageContentUpload" |
| 383 | + |
| 384 | + def validate(self, data): |
| 385 | + file = data.pop("file") |
| 386 | + |
| 387 | + for ext, packagetype in DIST_EXTENSIONS.items(): |
| 388 | + if file.name.endswith(ext): |
| 389 | + break |
| 390 | + else: |
| 391 | + raise serializers.ValidationError( |
| 392 | + _( |
| 393 | + "Extension on {} is not a valid python extension " |
| 394 | + "(.whl, .exe, .egg, .tar.gz, .tar.bz2, .zip)" |
| 395 | + ).format(file.name) |
| 396 | + ) |
| 397 | + |
| 398 | + metadata = get_project_metadata_from_file(file) |
| 399 | + artifact = core_models.Artifact.init_and_validate(file) |
| 400 | + try: |
| 401 | + artifact.save() |
| 402 | + except IntegrityError: |
| 403 | + artifact = core_models.Artifact.objects.get( |
| 404 | + sha256=artifact.sha256, pulp_domain=get_domain() |
| 405 | + ) |
| 406 | + artifact.touch() |
| 407 | + log.info(f"Artifact for {file.name} already existed in database") |
| 408 | + |
| 409 | + data["artifact"] = artifact |
| 410 | + data["sha256"] = artifact.sha256 |
| 411 | + data["filename"] = file.name |
| 412 | + data["relative_path"] = file.name |
| 413 | + data.update(vars(metadata)) |
| 414 | + return data |
| 415 | + |
| 416 | + |
360 | 417 | class MinimalPythonPackageContentSerializer(PythonPackageContentSerializer): |
361 | 418 | """ |
362 | 419 | A Serializer for PythonPackageContent. |
|
0 commit comments