Repository: pulp/pulpcore

Issue:
As a plugin writer, I want to have a function for touching content units
Author: @lubosmj (lmjachky)


Redmine Issue: 9419, https://pulp.plan.io/issues/9419

---

In the PR https://github.com/pulp/pulpcore/pull/1624, we introduced a method that uses `bulk_touch` for updating timestamps of content units. We should expose this method to all plugin writers (e.g., pulp_container currently implements the same method - this creates unnecessary duplicates).





Discussion:
This issue has been marked 'stale' due to lack of recent activity. If there is no further activity, the issue will be closed in another 30 days. Thank you for your contribution!

Resolving patch:
diff --git a/pulpcore/plugin/actions.py b/pulpcore/plugin/actions.py
--- a/pulpcore/plugin/actions.py
+++ b/pulpcore/plugin/actions.py
@@ -1,4 +1,5 @@
 from gettext import gettext as _
+
 from drf_spectacular.utils import extend_schema
 from rest_framework.decorators import action
 from rest_framework.serializers import ValidationError
@@ -48,7 +49,7 @@ def modify(self, request, pk):
             existing_content_units = Content.objects.filter(pk__in=content_units_pks)
             existing_content_units.touch()
 
-            self.verify_content_units(existing_content_units, add_content_units)
+            raise_for_unknown_content_units(existing_content_units, add_content_units)
 
             add_content_units = list(add_content_units.keys())
 
@@ -60,7 +61,7 @@ def modify(self, request, pk):
                     remove_content_units[NamedModelViewSet.extract_pk(url)] = url
                 content_units_pks = set(remove_content_units.keys())
                 existing_content_units = Content.objects.filter(pk__in=content_units_pks)
-                self.verify_content_units(existing_content_units, remove_content_units)
+                raise_for_unknown_content_units(existing_content_units, remove_content_units)
                 remove_content_units = list(remove_content_units.keys())
 
         task = dispatch(
@@ -75,14 +76,24 @@ def modify(self, request, pk):
         )
         return OperationPostponedResponse(task, request)
 
-    def verify_content_units(self, content_units, all_content_units):
-        """Verify referenced content units."""
-        existing_content_units_pks = content_units.values_list("pk", flat=True)
-        existing_content_units_pks = {str(pk) for pk in existing_content_units_pks}
-
-        missing_pks = set(all_content_units.keys()) - existing_content_units_pks
-        if missing_pks:
-            missing_hrefs = [all_content_units[pk] for pk in missing_pks]
-            raise ValidationError(
-                _("Could not find the following content units: {}").format(missing_hrefs)
-            )
+
+def raise_for_unknown_content_units(existing_content_units, content_units_pks_hrefs):
+    """Verify if all the specified content units were found in the database.
+
+    Args:
+        existing_content_units (pulpcore.plugin.models.Content): Content filtered by
+            specified_content_units.
+        content_units_pks_hrefs (dict): An original dictionary of pk-href pairs that
+            are used for the verification.
+    Raises:
+        ValidationError: If some of the referenced content units are not present in the database
+    """
+    existing_content_units_pks = existing_content_units.values_list("pk", flat=True)
+    existing_content_units_pks = set(map(str, existing_content_units_pks))
+
+    missing_pks = set(content_units_pks_hrefs.keys()) - existing_content_units_pks
+    if missing_pks:
+        missing_hrefs = [content_units_pks_hrefs[pk] for pk in missing_pks]
+        raise ValidationError(
+            _("Could not find the following content units: {}").format(missing_hrefs)
+        )


Test patch: