Repository: pulp/pulpcore

Issue:
Cannot purge tasks with a new user
A runtime error is raised when a random user tries to purge tasks. Everything works correctly for an admin user.

These tests do not properly test the workflow: https://github.com/pulp/pulp_file/blob/189da4a6902a4339a8b7f47ea89ae53692b7c81a/pulp_file/tests/functional/api/from_pulpcore/test_purge.py (We are sending requests with admin credentials: https://github.com/pulp/pulp_file/blob/189da4a6902a4339a8b7f47ea89ae53692b7c81a/pulp_file/tests/functional/api/from_pulpcore/test_purge.py#L264, https://github.com/pulp/pulp_file/blob/189da4a6902a4339a8b7f47ea89ae53692b7c81a/pulp_file/tests/functional/api/from_pulpcore/test_purge.py#L296). 


```
(pulp) [vagrant@pulp3-source-fedora35 backup]$ pulp user create --username test --password characters
{
  "pulp_href": "/pulp/api/v3/users/2/",
  "id": 2,
  "username": "test",
  "first_name": "",
  "last_name": "",
  "email": "",
  "is_staff": false,
  "is_active": true,
  "date_joined": "2022-09-29T18:13:59.604621Z",
  "groups": []
}

(pulp) [vagrant@pulp3-source-fedora35 backup]$ pulp --username test --password characters task purge
Started background task /pulp/api/v3/tasks/16d5b9c8-92aa-4e2d-a381-96195c199f5f/
Error: Task /pulp/api/v3/tasks/16d5b9c8-92aa-4e2d-a381-96195c199f5f/ failed: 'Cannot filter a query once a slice has been taken.'
(pulp) [vagrant@pulp3-source-fedora35 backup]$ http :24817/pulp/api/v3/tasks/16d5b9c8-92aa-4e2d-a381-96195c199f5f/
HTTP/1.1 200 OK
Access-Control-Expose-Headers: Correlation-ID
Allow: GET, PATCH, DELETE, HEAD, OPTIONS
Connection: close
Content-Length: 1595
Content-Type: application/json
Correlation-ID: 691f71bf781f4cca9bbeec7ac22ea9eb
Date: Thu, 29 Sep 2022 18:14:16 GMT
Referrer-Policy: same-origin
Server: gunicorn
Vary: Accept, Cookie
X-Content-Type-Options: nosniff
X-Frame-Options: DENY

{
    "child_tasks": [],
    "created_resources": [],
    "error": {
        "description": "Cannot filter a query once a slice has been taken.",
        "traceback": "  File \"/home/vagrant/devel/pulpcore/pulpcore/tasking/pulpcore_worker.py\", line 452, in _perform_task\n    result = func(*args, **kwargs)\n  File \"/home/vagrant/devel/pulpcore/pulpcore/app/tasks/purge.py\", line 74, in purge\n    delete_qs = get_objects_for_user(current_user, \"core.delete_task\", qs=tasks_qs[:DELETE_LIMIT])\n  File \"/home/vagrant/devel/pulpcore/pulpcore/app/role_util.py\", line 125, in get_objects_for_user\n    new_qs |= get_objects_for_user_roles(\n  File \"/home/vagrant/devel/pulpcore/pulpcore/app/role_util.py\", line 108, in get_objects_for_user_roles\n    return qs.annotate(pk_str=Cast(\"pk\", output_field=CharField())).filter(\n  File \"/usr/local/lib/pulp/lib64/python3.10/site-packages/django/db/models/query.py\", line 941, in filter\n    return self._filter_or_exclude(False, args, kwargs)\n  File \"/usr/local/lib/pulp/lib64/python3.10/site-packages/django/db/models/query.py\", line 953, in _filter_or_exclude\n    assert not self.query.is_sliced, \\\n"
    },
    "finished_at": "2022-09-29T18:14:05.194220Z",
    "logging_cid": "25aa82231d8b44939f07d3110fb82f8f",
    "name": "pulpcore.app.tasks.purge.purge",
    "parent_task": null,
    "progress_reports": [],
    "pulp_created": "2022-09-29T18:14:05.119070Z",
    "pulp_href": "/pulp/api/v3/tasks/16d5b9c8-92aa-4e2d-a381-96195c199f5f/",
    "reserved_resources_record": [],
    "started_at": "2022-09-29T18:14:05.156556Z",
    "state": "failed",
    "task_group": null,
    "worker": "/pulp/api/v3/workers/d4dfe511-5c98-40de-954b-b594c5b42d97/"
}


(pulp) [vagrant@pulp3-source-fedora35 backup]$ pulp task purge
Started background task /pulp/api/v3/tasks/91423959-cfe1-4b33-b0bf-4b92365fbd32/
Done.
```


Discussion:

Resolving patch:
diff --git a/pulpcore/app/tasks/purge.py b/pulpcore/app/tasks/purge.py
--- a/pulpcore/app/tasks/purge.py
+++ b/pulpcore/app/tasks/purge.py
@@ -1,4 +1,5 @@
 from gettext import gettext as _
+
 from django_currentuser.middleware import get_current_authenticated_user
 from pulpcore.app.models import (
     ProgressReport,
@@ -71,7 +72,6 @@ def purge(finished_before, states):
     # Tasks, prior to the specified date, in the specified state, owned by the current-user
     tasks_qs = Task.objects.filter(finished_at__lt=finished_before, state__in=states)
     candidate_qs = get_objects_for_user(current_user, "core.delete_task", qs=tasks_qs)
-    delete_qs = get_objects_for_user(current_user, "core.delete_task", qs=tasks_qs[:DELETE_LIMIT])
     # Progress bar reporting total-units
     totals_pb = ProgressReport(
         message=_("Purged task-related-objects total"),
@@ -101,7 +101,7 @@ def purge(finished_before, states):
     # Until our query returns "No tasks deleted", add results into totals and Do It Again
     while units_deleted > 0:
         units_deleted, details = Task.objects.filter(
-            pk__in=delete_qs.values_list("pk", flat=True)
+            pk__in=candidate_qs[:DELETE_LIMIT].values_list("pk", flat=True)
         ).delete()
         _details_reporting(details_reports, details, totals_pb)
 


Test patch: