PulpDebClient::ApiError - HTTP 500 when running katello:delete_orphaned_content

Just updated to katello 4.2, and I see that the patch is not going to land for long time.

Applied the changes manually with the following patch:

--- /usr/lib/python3.6/site-packages/pulpcore/app/models/repository.py  2021-12-15 19:47:21.557677732 +0000
+++ ./repository-fixed.py       2021-12-15 19:41:23.959648140 +0000
@@ -1013,18 +1013,22 @@
     @property
     def content_href(self):
         """
-        Generate URLs for the content types present in the RepositoryVersion.
-
-        For each content type present in the RepositoryVersion, create the URL of the viewset of
-        that variety of content along with a query parameter which filters it by presence in this
-        RepositoryVersion.
+        Generate URLs for the content types added, removed, or present in the RepositoryVersion.
+        For each content type present in or removed from this RepositoryVersion, create the URL of
+        the viewset of that variety of content along with a query parameter which filters it by
+        presence in this RepositoryVersion summary.

         Args:
             obj (pulpcore.app.models.RepositoryVersion): The RepositoryVersion being serialized.
         Returns:
             dict: {<pulp_type>: <url>}
         """
-        ctype_model = Content.objects.filter(pulp_type=self.content_type).first().cast().__class__
+        repository = self.repository_version.repository.cast()
+        repository_content = RepositoryContent.objects.filter(repository=repository)
+        ctype_query = Content.objects.filter(
+            pulp_type=self.content_type, pk__in=repository_content.values_list("content", flat=True)
+        )
+        ctype_model = ctype_query.first().cast().__class__
         ctype_view = get_view_name_for_model(ctype_model, "list")
         try:
             ctype_url = reverse(ctype_view)
@@ -1032,7 +1036,7 @@
             # We've hit a content type for which there is no viewset.
             # There's nothing we can do here, except to skip it.
             return
-        repository = self.repository_version.repository.cast()
+
         repository_view = get_view_name_for_model(repository.__class__, "list")

         repository_url = reverse(repository_view)

Then applied it and restarted all services

patch /usr/lib/python3.6/site-packages/pulpcore/app/models/repository.py  < pulpdeb_api_error500.patch

However, I am still getting same error when trying to run katello:delete_orphaned_content. Here is the stacktrace:

Dec 15 19:52:32 host pulpcore-api[11021]: pulp [23e6e70d41cb407b9f4a28b6429db7f2]: django.request:ERROR: Internal Server Error: /pulp/api/v3/repositories/deb/apt/93fe3eca-961c-4688-8782-9239955f1ef5/
Dec 15 19:52:32 host pulpcore-api[11021]: Traceback (most recent call last):
Dec 15 19:52:32 host pulpcore-api[11021]: File "/usr/lib/python3.6/site-packages/django/core/handlers/exception.py", line 34, in inner
Dec 15 19:52:32 host pulpcore-api[11021]: response = get_response(request)
Dec 15 19:52:32 host pulpcore-api[11021]: File "/usr/lib/python3.6/site-packages/django/core/handlers/base.py", line 115, in _get_response
Dec 15 19:52:32 host pulpcore-api[11021]: response = self.process_exception_by_middleware(e, request)
Dec 15 19:52:32 host pulpcore-api[11021]: File "/usr/lib/python3.6/site-packages/django/core/handlers/base.py", line 113, in _get_response
Dec 15 19:52:32 host pulpcore-api[11021]: response = wrapped_callback(request, *callback_args, **callback_kwargs)
Dec 15 19:52:32 host pulpcore-api[11021]: File "/usr/lib/python3.6/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
Dec 15 19:52:32 host pulpcore-api[11021]: return view_func(*args, **kwargs)
Dec 15 19:52:32 host pulpcore-api[11021]: File "/usr/lib/python3.6/site-packages/rest_framework/viewsets.py", line 125, in view
Dec 15 19:52:32 host pulpcore-api[11021]: return self.dispatch(request, *args, **kwargs)
Dec 15 19:52:32 host pulpcore-api[11021]: File "/usr/lib/python3.6/site-packages/rest_framework/views.py", line 509, in dispatch
Dec 15 19:52:32 host pulpcore-api[11021]: response = self.handle_exception(exc)
Dec 15 19:52:32 host pulpcore-api[11021]: File "/usr/lib/python3.6/site-packages/rest_framework/views.py", line 469, in handle_exception
Dec 15 19:52:32 host pulpcore-api[11021]: self.raise_uncaught_exception(exc)
Dec 15 19:52:32 host pulpcore-api[11021]: File "/usr/lib/python3.6/site-packages/rest_framework/views.py", line 480, in raise_uncaught_exception
Dec 15 19:52:32 host pulpcore-api[11021]: raise exc
Dec 15 19:52:32 host pulpcore-api[11021]: File "/usr/lib/python3.6/site-packages/rest_framework/views.py", line 506, in dispatch
Dec 15 19:52:32 host pulpcore-api[11021]: response = handler(request, *args, **kwargs)
Dec 15 19:52:32 host pulpcore-api[11021]: File "/usr/lib/python3.6/site-packages/rest_framework/mixins.py", line 43, in list
Dec 15 19:52:32 host pulpcore-api[11021]: return self.get_paginated_response(serializer.data)
Dec 15 19:52:32 host pulpcore-api[11021]: File "/usr/lib/python3.6/site-packages/rest_framework/serializers.py", line 745, in data
Dec 15 19:52:32 host pulpcore-api[11021]: ret = super().data
Dec 15 19:52:32 host pulpcore-api[11021]: File "/usr/lib/python3.6/site-packages/rest_framework/serializers.py", line 246, in data
Dec 15 19:52:32 host pulpcore-api[11021]: self._data = self.to_representation(self.instance)
Dec 15 19:52:32 host pulpcore-api[11021]: File "/usr/lib/python3.6/site-packages/rest_framework/serializers.py", line 664, in to_representation
Dec 15 19:52:32 host pulpcore-api[11021]: self.child.to_representation(item) for item in iterable
Dec 15 19:52:32 host pulpcore-api[11021]: File "/usr/lib/python3.6/site-packages/rest_framework/serializers.py", line 664, in <listcomp>
Dec 15 19:52:32 host pulpcore-api[11021]: self.child.to_representation(item) for item in iterable
Dec 15 19:52:32 host pulpcore-api[11021]: File "/usr/lib/python3.6/site-packages/rest_framework/serializers.py", line 515, in to_representation
Dec 15 19:52:32 host pulpcore-api[11021]: ret[field.field_name] = field.to_representation(attribute)
Dec 15 19:52:32 host pulpcore-api[11021]: File "/usr/lib/python3.6/site-packages/pulpcore/app/serializers/repository.py", line 360, in to_representation
Dec 15 19:52:32 host pulpcore-api[11021]: item_dict = {"count": count_detail.count, "href": count_detail.content_href}
Dec 15 19:52:32 host pulpcore-api[11021]: File "/usr/lib/python3.6/site-packages/pulpcore/app/models/repository.py", line 1031, in content_href
Dec 15 19:52:32 host pulpcore-api[11021]: ctype_model = ctype_query.first().cast().__class__
Dec 15 19:52:32 host pulpcore-api[11021]: AttributeError: 'NoneType' object has no attribute 'cast'