|
3 | 3 | from tempfile import NamedTemporaryFile |
4 | 4 | from urllib.parse import urlparse |
5 | 5 |
|
6 | | -from django.db import DatabaseError |
| 6 | +from django.db import DatabaseError, IntegrityError |
7 | 7 | from rest_framework.serializers import ( |
8 | 8 | CharField, |
9 | 9 | FileField, |
|
14 | 14 | from pulpcore.app.files import PulpTemporaryUploadedFile |
15 | 15 | from pulpcore.app.models import Artifact, PulpTemporaryFile, Remote, Upload, UploadChunk |
16 | 16 | from pulpcore.app.serializers import ( |
17 | | - ArtifactSerializer, |
18 | 17 | NoArtifactContentSerializer, |
19 | 18 | RelatedField, |
20 | 19 | SingleArtifactContentSerializer, |
@@ -259,19 +258,21 @@ def deferred_validate(self, data): |
259 | 258 | # if artifact already exists, let's use it |
260 | 259 | try: |
261 | 260 | artifact = Artifact.objects.get( |
262 | | - sha256=file.hashers["sha256"].hexdigest(), |
263 | | - pulp_domain=get_domain_pk(), |
| 261 | + sha256=file.hashers["sha256"].hexdigest(), pulp_domain=get_domain_pk() |
264 | 262 | ) |
265 | 263 | if not artifact.pulp_domain.get_storage().exists(artifact.file.name): |
266 | 264 | artifact.file = file |
267 | 265 | artifact.save() |
268 | 266 | else: |
269 | 267 | artifact.touch() |
270 | 268 | except (Artifact.DoesNotExist, DatabaseError): |
271 | | - artifact_data = {"file": file} |
272 | | - serializer = ArtifactSerializer(data=artifact_data) |
273 | | - serializer.is_valid(raise_exception=True) |
274 | | - artifact = serializer.save() |
| 269 | + artifact = Artifact.init_and_validate(file) |
| 270 | + try: |
| 271 | + artifact.save() |
| 272 | + except IntegrityError: |
| 273 | + artifact = Artifact.objects.get( |
| 274 | + sha256=artifact.sha256, pulp_domain=get_domain_pk() |
| 275 | + ) |
275 | 276 | data["artifact"] = artifact |
276 | 277 | return data |
277 | 278 |
|
|
0 commit comments