Skip to content

Commit

Permalink
Rename ContentSerializer fields
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthias Dellweg committed Sep 11, 2019
1 parent 90a3267 commit f96b494
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 14 deletions.
1 change: 1 addition & 0 deletions CHANGES/5428.removal
@@ -0,0 +1 @@
Rename the fields on the ContentSerializers to not start with underscore.
30 changes: 22 additions & 8 deletions pulpcore/app/serializers/content.py
Expand Up @@ -27,16 +27,27 @@ class Meta:


class SingleArtifactContentSerializer(BaseContentSerializer):
_artifact = fields.SingleContentArtifactField(
artifact = fields.SingleContentArtifactField(
help_text=_("Artifact file representing the physical content"),
)

_relative_path = serializers.CharField(
relative_path = serializers.CharField(
help_text=_("Path where the artifact is located relative to distributions base_path"),
validators=[fields.relative_path_validator],
write_only=True,
)

def __init__(self, *args, **kwargs):
"""
Initializer for SingleArtifactContentSerializer
"""
super().__init__(*args, **kwargs)

# If the content model has its own database field 'relative_path',
# we should not mark the field write_only
if hasattr(self.Meta.model, 'relative_path'):
self.fields["relative_path"].write_only = False

@transaction.atomic
def create(self, validated_data):
"""
Expand All @@ -45,8 +56,11 @@ def create(self, validated_data):
Args:
validated_data (dict): Data to save to the database
"""
artifact = validated_data.pop('_artifact')
relative_path = validated_data.pop('_relative_path')
artifact = validated_data.pop('artifact')
if self.fields["relative_path"].write_only:
relative_path = validated_data.pop('relative_path')
else:
relative_path = validated_data.get('relative_path')
content = self.Meta.model.objects.create(**validated_data)
models.ContentArtifact.objects.create(
artifact=artifact,
Expand All @@ -57,11 +71,11 @@ def create(self, validated_data):

class Meta:
model = models.Content
fields = BaseContentSerializer.Meta.fields + ('_artifact', '_relative_path')
fields = BaseContentSerializer.Meta.fields + ('artifact', 'relative_path')


class MultipleArtifactContentSerializer(BaseContentSerializer):
_artifacts = fields.ContentArtifactsField(
artifacts = fields.ContentArtifactsField(
help_text=_("A dict mapping relative paths inside the Content to the corresponding"
"Artifact URLs. E.g.: {'relative/path': "
"'/artifacts/1/'"),
Expand All @@ -75,9 +89,9 @@ def create(self, validated_data):
Args:
validated_data (dict): Data to save to the database
"""
_artifacts = validated_data.pop('_artifacts')
artifacts = validated_data.pop('artifacts')
content = self.Meta.model.objects.create(**validated_data)
for relative_path, artifact in _artifacts.items():
for relative_path, artifact in artifacts.items():
models.ContentArtifact.objects.create(
artifact=artifact,
content=content,
Expand Down
2 changes: 1 addition & 1 deletion pulpcore/app/serializers/fields.py
Expand Up @@ -123,7 +123,7 @@ def run_validation(self, data):
"""
ret = {}
if data is empty:
raise serializers.ValidationError(_('_artifacts field must be specified.'))
raise serializers.ValidationError(_('artifacts field must be specified.'))
for relative_path, url in data.items():
relative_path_validator(relative_path)
artifactfield = RelatedField(view_name='artifacts-detail',
Expand Down
2 changes: 1 addition & 1 deletion pulpcore/tests/functional/api/using_plugin/test_orphans.py
Expand Up @@ -80,7 +80,7 @@ def test_clean_orphan_content_unit(self):
)

# Verify that the artifact is present on disk.
artifact_path = os.path.join(MEDIA_PATH, self.api_client.get(content['_artifact'])['file'])
artifact_path = os.path.join(MEDIA_PATH, self.api_client.get(content['artifact'])['file'])
cmd = ('ls', artifact_path)
self.cli_client.run(cmd, sudo=True)

Expand Down
Expand Up @@ -330,7 +330,7 @@ def test_delete_first_version(self):
get_content(self.repo, self.repo_version_hrefs[0])
for repo_version_href in self.repo_version_hrefs[1:]:
artifact_paths = get_artifact_paths(self.repo, repo_version_href)
self.assertIn(self.content[0]['_artifact'], artifact_paths)
self.assertIn(self.content[0]['artifact'], artifact_paths)

def test_delete_last_version(self):
"""Delete the last repository version.
Expand All @@ -352,8 +352,8 @@ def test_delete_last_version(self):
self.repo = self.client.get(self.repo['_href'])
artifact_paths = get_artifact_paths(self.repo)

self.assertNotIn(self.content[-2]['_artifact'], artifact_paths)
self.assertIn(self.content[-1]['_artifact'], artifact_paths)
self.assertNotIn(self.content[-2]['artifact'], artifact_paths)
self.assertIn(self.content[-1]['artifact'], artifact_paths)

def test_delete_middle_version(self):
"""Delete a middle version."""
Expand All @@ -365,7 +365,7 @@ def test_delete_middle_version(self):

for repo_version_href in self.repo_version_hrefs[index + 1:]:
artifact_paths = get_artifact_paths(self.repo, repo_version_href)
self.assertIn(self.content[index]['_artifact'], artifact_paths)
self.assertIn(self.content[index]['artifact'], artifact_paths)

def test_delete_publication(self):
"""Delete a publication.
Expand Down

0 comments on commit f96b494

Please sign in to comment.