Skip to content

Commit

Permalink
Pass an adjusted request object to the serializer
Browse files Browse the repository at this point in the history
closes #7718
  • Loading branch information
lubosmj authored and dkliban committed Oct 23, 2020
1 parent 8bbe6c4 commit 4063c34
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGES/plugin_api/7718.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Enabled plugin writers to retrieve a request object from a serializer when look ups are
performed from within the task serializer.
19 changes: 15 additions & 4 deletions pulpcore/app/serializers/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,24 @@ def to_representation(self, data):
return None
except AttributeError:
pass
viewset = get_viewset_for_model(data.content_object)

# serializer contains all serialized fields because we are passing
# 'None' to the request's context
serializer = viewset.serializer_class(data.content_object, context={"request": None})
# query parameters can be ignored because we are looking just for 'pulp_href'; still,
# we need to use the request object due to contextual references required by some
# serializers
request = self._get_request_without_query_params()

viewset = get_viewset_for_model(data.content_object)
serializer = viewset.serializer_class(data.content_object, context={"request": request})
return serializer.data.get("pulp_href")

def _get_request_without_query_params(self):
"""Remove all query parameters from the request object."""
request = self.context["request"]
request.query_params._mutable = True
request.query_params.clear()
request.query_params._mutable = False
return request

class Meta:
model = models.CreatedResource
fields = []
Expand Down

0 comments on commit 4063c34

Please sign in to comment.