Skip to content
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

Define schema for detail routes. #60

Merged
merged 1 commit into from Mar 26, 2018

Conversation

dparalen
Copy link

closes #3351

raise serializers.ValidationError(detail=_('Repository URI must be specified.'))
repository = self.get_resource(repository_uri, Repository)
serializer = _RepositorySyncURLSerializer(data=request.data)
if not serializer.is_valid():
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably just call serializer.is_valid(raise_exception=True) here

@@ -28,6 +28,37 @@ class Meta:
]


class _URLField(serializers.URLField):
Copy link
Contributor

@werwty werwty Mar 23, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why subclass URLField instead of using default URLField?

def publish(self, request, pk):
serializer = _RepositoryPublishURLSerializer(data=request.data)
if not serializer.is_valid():
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably just call serializer.is_valid(raise_exception=True) here

publisher = self.get_object()
repository = None
repository_version = None
if 'repository' not in request.data and 'repository_version' not in request.data:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would recommend cleaning up this logic a bit:

--- a/pulp_file/app/viewsets.py
+++ b/pulp_file/app/viewsets.py
@@ -121,23 +121,20 @@ class FilePublisherViewSet(PublisherViewSet):
         repository_uri = serializer.data.get('repository')
         repository_version_uri = serializer.data.get('repository_version')
         publisher = self.get_object()
-        repository, repository_version = None, None
+
         if not repository_uri and not repository_version_uri:
             raise serializers.ValidationError(_("Either the 'repository' or 'repository_version' "
                                               "need to be specified."))
 
-        if repository_uri:
-            repository = self.get_resource(repository_uri, Repository)
-
-        if repository_version_uri:
-            repository_version = self.get_resource(repository_version_uri, RepositoryVersion)
-
-        if repository and repository_version:
+        elif repository_uri and repository_version_uri:
             raise serializers.ValidationError(_("Either the 'repository' or 'repository_version' "
                                               "can be specified - not both."))
-
-        if not repository_version:
+        elif repository_uri:
+            repository = self.get_resource(repository_uri, Repository)
             repository_version = RepositoryVersion.latest(repository)
+        else:
+            repository_version = self.get_resource(repository_version_uri, RepositoryVersion)
+

Copy link
Contributor

@werwty werwty left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nicely done @dparalen! I thought for sure we needed to make upstream changes to drf_openapi to get this to work but I'm so glad you found a suitable solution 🥇 🥂

I'm running into some issues publishing a repository_version http POST $PUBLISHER_HREF'publish/' repository_version=http://pulp3.dev:8000/api/v3/repositories/30ed44e8-f0c3-434c-8bea-d4f646d90f61/versions/1/

Is this the right POST request?

  File "/home/vagrant/devel/pulp_file/pulp_file/app/viewsets.py", line 136, in publish
    repository_version = self.get_resource(repository_version_uri, RepositoryVersion)
  File "/home/vagrant/devel/pulp/pulpcore/pulpcore/app/viewsets/base.py", line 64, in get_resource
    pk = match.kwargs['pk']

@pep8speaks
Copy link

pep8speaks commented Mar 23, 2018

Hello @dparalen! Thanks for updating the PR.

Cheers ! There are no PEP8 issues in this Pull Request. 🍻

Comment last updated on March 23, 2018 at 21:02 Hours UTC

@dparalen
Copy link
Author

@werwty thanks for the review and the kind words! :D
w/r the logic refactoring, I moved part of it to a custom validator which, according to the docs should be the preferred way to do it; it gives me this nicely structured and (hopefully) self-explanatory no-no when I try to cheat:

(pulp) [vagrant@pulp3 pulpcore]$ phttp  :8000/api/v3/publishers/file/540f8af9-150a-4b1c-921a-da465248dc25/publish/ foo=bar
HTTP/1.0 400 Bad Request
Allow: POST, OPTIONS
Content-Length: 107
Content-Type: application/json
Date: Fri, 23 Mar 2018 12:25:56 GMT
Server: WSGIServer/0.2 CPython/3.6.4
Vary: Accept, Cookie
X-Frame-Options: SAMEORIGIN

{
    "non_field_errors": [
        "Either the 'repository' or 'repository_version' need to be specified but not both."
    ]
}

Unfortunately, I wasn't able to reproduce your error but maybe I'm running out-of-date core...
Please, let me know wdyt.

Cheers,
milan

required=False
)

def validate(self, data):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@@ -82,27 +114,18 @@ class FilePublisherViewSet(PublisherViewSet):
queryset = FilePublisher.objects.all()
serializer_class = FilePublisherSerializer

@detail_route(methods=('post',))
@detail_route(methods=('post',), serializer_class=_RepositoryPublishURLSerializer)
def publish(self, request, pk):
publisher = self.get_object()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would recommend adding a docstring here. This would show up in the auto docs like so:
screenshot from 2018-03-23 13-38-28

@werwty
Copy link
Contributor

werwty commented Mar 23, 2018

@dparalen I'm still running into an error when publishing repository_versions (but not repository). My pulp repo is up to date, and pulp_file is checked out from this PR. Can you check to see if you can reproduce it?

Copy link
Contributor

@werwty werwty left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see that I'm running into this issue: https://pulp.plan.io/issues/3466

Since that's the case, I think this PR is ready for merge, I just left an optional suggestion.
Thanks @dparalen

@dparalen
Copy link
Author

@werwty I just wanted to let you know that indeed that's the issue you're running into and I've been running my core with the fix applied all the time 🙀
...updating

@dparalen
Copy link
Author

@werwty in case my update is OK, please merge this change as I don't have the commit bit ;)

Thanks,
milan

@werwty
Copy link
Contributor

werwty commented Mar 26, 2018

@dparalen ha! I also don't have the commit bit. @daviddavis Can you take care of this?

@daviddavis
Copy link
Contributor

👍

@daviddavis daviddavis merged commit 413563f into pulp:master Mar 26, 2018
@dparalen
Copy link
Author

lol, thanks guys! 😘

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants