From 416c929d21b915827cee3d4620daede2b18437e7 Mon Sep 17 00:00:00 2001
From: Filip Dobrovolny <fdobrovo@redhat.com>
Date: Fri, 11 Nov 2016 20:24:26 +0100
Subject: [PATCH] Migrate pulp.server.managers.content.orphan to Pulp3

---
 server/pulp/server/managers/content/orphan.py | 129 ++++++--------------------
 1 file changed, 28 insertions(+), 101 deletions(-)

diff --git a/server/pulp/server/managers/content/orphan.py b/server/pulp/server/managers/content/orphan.py
index bf09320..0ecd4bd 100644
--- a/server/pulp/server/managers/content/orphan.py
+++ b/server/pulp/server/managers/content/orphan.py
@@ -4,6 +4,7 @@ import logging
 import os
 import re
 import shutil
+from collections import Counter
 
 from celery import task
 
@@ -15,6 +16,10 @@ from pulp.server.controllers import units as units_controller
 from pulp.server.db.model.repository import RepoContentUnit
 from pulp.server.db import model
 from pulp.server.exceptions import MissingResource
+
+# New Imports
+
+from pulp.app.models import Content
 from pulp.tasking import UserFacingTask
 
 
@@ -31,12 +36,7 @@ class OrphanManager(object):
         :return: summary of orphaned units
         :rtype: dict
         """
-        summary = {}
-        for content_type_id in content_types_db.all_type_ids():
-            summary[content_type_id] = self.orphans_count_by_type(content_type_id)
-        for content_type_id in plugin_api.list_unit_models():
-            summary[content_type_id] = self.orphans_count_by_type(content_type_id)
-        return summary
+        return dict(Counter(self.orphans_queryset().values_list('type', flat=True)))
 
     def orphans_count_by_type(self, content_type_id):
         """
@@ -47,130 +47,57 @@ class OrphanManager(object):
         :return: count of orphaned units of the given type
         :rtype: int
         """
-        count = 0
-        for unit in OrphanManager.generate_orphans_by_type(content_type_id):
-            count += 1
-        return count
-
-    def generate_all_orphans(self, fields=None):
-        """
-        Return an generator of all orphaned content units.
-
-        If fields is not specified, only the `_id` field will be present.
+        return self.orphans_by_type(content_type_id).count()
 
-        :param fields: list of fields to include in each content unit
-        :type fields: list or None
-        :return: generator of orphaned content units
-        :rtype: generator
+    def orphans_queryset(self):
         """
+        Return orphans queryset.
 
-        for content_type_id in content_types_db.all_type_ids():
-            for content_unit in OrphanManager.generate_orphans_by_type(content_type_id, fields):
-                yield content_unit
-
-    def generate_all_orphans_with_unit_keys(self):
-        """
-        Return an generator of all orphaned content units.
-
-        Each orphan will contain the fields specified in the content type
-        definition's search indexes.
-
-        :return: generator of orphaned content units
-        :rtype: generator
-        """
-
-        for content_type_id in content_types_db.all_type_ids():
-            for content_unit in OrphanManager.generate_orphans_by_type_with_unit_keys(
-                    content_type_id):
-                yield content_unit
-
-    @staticmethod
-    def generate_orphans_by_type(content_type_id, fields=None):
-        """
-        Return an generator of all orphaned content units of the given content type.
-
-        If fields is not specified, only the `_id` field will be present.
-
-        :param content_type_id: id of the content type
-        :type content_type_id: basestring
-        :param fields: list of fields to include in each content unit
-        :type fields: list or None
-        :return: generator of orphaned content units for the given content type
-        :rtype: generator
+        :return: Queryset of orphans
+        :rtype: QuerySet
         """
 
-        fields = fields if fields is not None else ['_id']
-        content_units_collection = content_types_db.type_units_collection(content_type_id)
-        repo_content_units_collection = RepoContentUnit.get_collection()
-
-        for content_unit in content_units_collection.find({}, projection=fields):
+        return Content.objects.filter(repositories=None)
 
-            repo_content_units_cursor = repo_content_units_collection.find(
-                {'unit_id': content_unit['_id']})
-
-            if repo_content_units_cursor.count() > 0:
-                continue
-
-            yield content_unit
-
-    @staticmethod
-    def generate_orphans_by_type_with_unit_keys(content_type_id):
+    def orphans_by_type(self, content_type_id):
         """
-        Return an generator of all orphaned content units of the given content type.
+        Return orphans queryset of the given content type.
 
-        Each content unit will contain the fields specified in the content type
-        definition's search indexes.
 
         :param content_type_id: id of the content type
-        :type  content_type_id: basestring
-        :return: generator of orphaned content units for the given content type
-        :rtype: generator
+        :type content_type_id: string
+        :return: Queryset of orphans of the given type
+        :rtype: QuerySet
         """
-        try:
-            unit_key_fields = units_controller.get_unit_key_fields_for_type(content_type_id)
-        except ValueError:
-            raise MissingResource(content_type_id=content_type_id)
-        fields = ['_id', '_content_type_id']
-        fields.extend(unit_key_fields)
 
-        for content_unit in OrphanManager.generate_orphans_by_type(content_type_id, fields):
-            yield content_unit
+        return self.orphans_queryset().filter(type=content_type_id)
 
     def get_orphan(self, content_type_id, content_unit_id):
         """
         Look up a single orphaned content unit by content type and unit id.
 
         :param content_type_id: id of the content type
-        :type content_type_id: basestring
+        :type content_type_id: string
         :param content_unit_id: id of the content unit
-        :type content_unit_id: basestring
+        :type content_unit_id: string
         :return: orphaned content unit
-        :rtype: SON
+        :rtype: Content
         :raises MissingResource: if no orphaned content unit corresponds to the
                                  given content type and unit id
         """
 
-        for content_unit in OrphanManager.generate_orphans_by_type(content_type_id):
-
-            if content_unit['_id'] != content_unit_id:
-                continue
-
-            return content_unit
-
-        raise pulp_exceptions.MissingResource(content_type=content_type_id,
-                                              content_unit=content_unit_id)
+        try:
+            self.orphans_by_type(content_type_id).get(id=content_unit_id)
+        except Content.DoesNotExist:
+            raise pulp_exceptions.MissingResource(content_type=content_type_id,
+                                                  content_unit=content_unit_id)
 
-    @staticmethod
-    def delete_all_orphans():
+    def delete_all_orphans(self):
         """
         Delete all orphaned content units.
         """
 
-        for content_type_id in content_types_db.all_type_ids():
-            OrphanManager.delete_orphans_by_type(content_type_id)
-
-        for content_type_id in plugin_api.list_unit_models():
-            OrphanManager.delete_orphan_content_units_by_type(content_type_id)
+        self.orphans_queryset().delete()
 
     @staticmethod
     def delete_orphans_by_id(content_unit_list):
-- 
2.7.4

