We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
django-admin dump-permissions command fails
`
django-admin dump-permissions  --settings=pulpcore.app.settings
Traceback (most recent call last):
  File "/var/lib/pulp/pulpvenv/bin/django-admin", line 8, in <module>
    sys.exit(execute_from_command_line())
  File "/var/lib/pulp/pulpvenv/lib64/python3.8/site-packages/django/core/management/__init__.py", line 419, in execute_from_command_line
    utility.execute()
  File "/var/lib/pulp/pulpvenv/lib64/python3.8/site-packages/django/core/management/__init__.py", line 413, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/var/lib/pulp/pulpvenv/lib64/python3.8/site-packages/django/core/management/base.py", line 354, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/var/lib/pulp/pulpvenv/lib64/python3.8/site-packages/django/core/management/base.py", line 398, in execute
    output = self.handle(*args, **options)
  File "/var/lib/pulp/pulpvenv/lib64/python3.8/site-packages/pulpcore/app/management/commands/dump-permissions.py", line 231, in handle
    data["group_object_permissions"] = list(_get_group_object_permissions())
  File "/var/lib/pulp/pulpvenv/lib64/python3.8/site-packages/pulpcore/app/management/commands/dump-permissions.py", line 156, in _get_group_object_permissions
    yield {"groupname": groupname, "object": obj, "permissions": permissions}
UnboundLocalError: local variable 'groupname' referenced before assignment
`

--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `pulpcore/app/management/commands/dump-permissions.py`
Content:
```
1 from gettext import gettext as _
2 from functools import lru_cache
3 import json
4 import sys
5 
6 from django.db import connection
7 from django.core.management import BaseCommand, CommandError
8 from django.contrib.contenttypes.models import ContentType
9 
10 from pulpcore.app.util import get_url
11 
12 
13 SEPARATOR = "\t"
14 
15 
16 @lru_cache
17 def _get_model(content_type_id):
18     return ContentType.objects.get(pk=content_type_id).model_class()
19 
20 
21 def _get_url(content_type_id, object_pk):
22     obj = _get_model(content_type_id).objects.get(pk=object_pk)
23     return get_url(obj)
24 
25 
26 def _get_user_model_permissions():
27     with connection.cursor() as cursor:
28         cursor.execute(
29             """
30             SELECT
31                 auth_user.id,
32                 array_agg(auth_permission.id),
33                 username,
34                 array_agg(concat(app_label, '.', codename))
35             FROM auth_user_user_permissions
36             LEFT JOIN auth_user ON (user_id=auth_user.id)
37             LEFT JOIN auth_permission ON (permission_id=auth_permission.id)
38             LEFT JOIN django_content_type ON (content_type_id=django_content_type.id)
39             GROUP BY auth_user.id
40             ORDER BY username
41             """
42         )
43         while row := cursor.fetchone():
44             (
45                 user_id,
46                 permission_pks,
47                 username,
48                 permissions,
49             ) = row
50             yield {"username": username, "permissions": permissions}
51 
52 
53 def _get_group_model_permissions():
54     with connection.cursor() as cursor:
55         cursor.execute(
56             """
57             SELECT
58                 auth_group.id,
59                 array_agg(auth_permission.id),
60                 auth_group.name,
61                 array_agg(concat(app_label, '.', codename))
62             FROM auth_group_permissions
63             LEFT JOIN auth_group ON (group_id=auth_group.id)
64             LEFT JOIN auth_permission ON (permission_id=auth_permission.id)
65             LEFT JOIN django_content_type ON (content_type_id=django_content_type.id)
66             GROUP BY auth_group.id
67             ORDER BY auth_group.name
68             """
69         )
70         while row := cursor.fetchone():
71             (
72                 group_id,
73                 permission_pks,
74                 groupname,
75                 permissions,
76             ) = row
77 
78             yield {"groupname": groupname, "permissions": permissions}
79 
80 
81 def _get_user_object_permissions():
82     with connection.cursor() as cursor:
83         cursor.execute(
84             """
85             SELECT
86                 auth_user.id,
87                 django_content_type.id,
88                 object_pk,
89                 array_agg(auth_permission.id),
90                 app_label,
91                 model,
92                 username,
93                 array_agg(concat(app_label, '.', codename))
94             FROM guardian_userobjectpermission
95             LEFT JOIN django_content_type ON (content_type_id=django_content_type.id)
96             LEFT JOIN auth_user ON (user_id=auth_user.id)
97             LEFT JOIN auth_permission ON (permission_id=auth_permission.id)
98             GROUP BY auth_user.id, django_content_type.id, object_pk
99             ORDER BY username, django_content_type.id, object_pk
100             """
101         )
102         while row := cursor.fetchone():
103             (
104                 user_id,
105                 content_type_id,
106                 object_pk,
107                 permission_pks,
108                 app_label,
109                 model,
110                 username,
111                 permissions,
112             ) = row
113             try:
114                 obj = _get_url(content_type_id, object_pk)
115             except Exception:
116                 obj = f"{app_label}.{model}:{object_pk}"
117             yield {"username": username, "object": obj, "permissions": permissions}
118 
119 
120 def _get_group_object_permissions():
121     with connection.cursor() as cursor:
122         cursor.execute(
123             """
124                     SELECT
125                         auth_group.id,
126                         django_content_type.id,
127                         object_pk,
128                         array_agg(auth_permission.id),
129                         app_label,
130                         model,
131                         auth_group.name,
132                         array_agg(concat(app_label, '.', codename))
133                     FROM guardian_groupobjectpermission
134                     LEFT JOIN django_content_type ON (content_type_id=django_content_type.id)
135                     LEFT JOIN auth_group ON (group_id=auth_group.id)
136                     LEFT JOIN auth_permission ON (permission_id=auth_permission.id)
137                     GROUP BY auth_group.id, django_content_type.id, object_pk
138                     ORDER BY auth_group.name, django_content_type.id, object_pk
139                     """
140         )
141         while row := cursor.fetchone():
142             (
143                 group_id,
144                 content_type_id,
145                 object_pk,
146                 permission_pks,
147                 app_label,
148                 model,
149                 groupname,
150                 permissions,
151             ) = row
152             try:
153                 obj = _get_url(content_type_id, object_pk)
154             except Exception:
155                 obj = f"{app_label}.{model}:{object_pk}"
156         yield {"groupname": groupname, "object": obj, "permissions": permissions}
157 
158 
159 class Command(BaseCommand):
160     """
161     Django management command for getting a data dump of deprecated permission.
162     """
163 
164     help = _("Dumps deprecated permissions.")
165 
166     def add_arguments(self, parser):
167         parser.add_argument("--tabular", action="store_true", help=_("Output table format."))
168 
169     def handle(self, *args, **options):
170         tabular = options.get("tabular", False)
171 
172         if tabular:
173             try:
174                 from prettytable import PrettyTable
175             except ImportError:
176                 raise CommandError("'prettytable' package must be installed for tabular output.")
177 
178         table_names = connection.introspection.table_names()
179         guardian_uop_available = "guardian_userobjectpermission" in table_names
180         guardian_gop_available = "guardian_groupobjectpermission" in table_names
181 
182         # User model permissions
183         if tabular:
184             print("# ==== " + _("User model permissions") + " ====")
185             table = PrettyTable()
186             table.field_names = [_("username"), _("permission")]
187             for t in _get_user_model_permissions():
188                 table.add_row([t["username"], t["permissions"][0]])
189                 table.add_rows((["", perm] for perm in t["permissions"][1:]))
190             print(table)
191             print()
192 
193             print("# ==== " + _("Group model permissions") + " ====")
194             table = PrettyTable()
195             table.field_names = [_("groupname"), _("permission")]
196             for t in _get_group_model_permissions():
197                 table.add_row([t["groupname"], t["permissions"][0]])
198                 table.add_rows((["", perm] for perm in t["permissions"][1:]))
199             print(table)
200             print()
201 
202             if guardian_uop_available:
203                 print("# ==== " + _("User object permissions") + " ====")
204                 table = PrettyTable()
205                 table.field_names = [_("username"), _("object"), _("permission")]
206                 for t in _get_user_object_permissions():
207                     table.add_row([t["username"], t["object"], t["permissions"][0]])
208                     table.add_rows((["", "", perm] for perm in t["permissions"][1:]))
209                 print(table)
210                 print()
211 
212             if guardian_gop_available:
213                 print("# ==== " + _("Group object permissions") + " ====")
214                 table = PrettyTable()
215                 table.field_names = [_("groupname"), _("object"), _("permissions")]
216                 for t in _get_group_object_permissions():
217                     table.add_row([t["groupname"], t["object"], t["permissions"][0]])
218                     table.add_rows((["", "", perm] for perm in t["permissions"][1:]))
219                 print(table)
220                 print()
221 
222         else:
223             # TODO Streaming would be nice...
224             data = {
225                 "user_model_permissions": list(_get_user_model_permissions()),
226                 "group_model_permissions": list(_get_group_model_permissions()),
227             }
228             if guardian_uop_available:
229                 data["user_object_permissions"] = list(_get_user_object_permissions())
230             if guardian_gop_available:
231                 data["group_object_permissions"] = list(_get_group_object_permissions())
232 
233             json.dump(
234                 data,
235                 sys.stdout,
236             )
237
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/pulpcore/app/management/commands/dump-permissions.py b/pulpcore/app/management/commands/dump-permissions.py
--- a/pulpcore/app/management/commands/dump-permissions.py
+++ b/pulpcore/app/management/commands/dump-permissions.py
@@ -153,7 +153,7 @@
                 obj = _get_url(content_type_id, object_pk)
             except Exception:
                 obj = f"{app_label}.{model}:{object_pk}"
-        yield {"groupname": groupname, "object": obj, "permissions": permissions}
+            yield {"groupname": groupname, "object": obj, "permissions": permissions}
 
 
 class Command(BaseCommand):

{"golden_diff": "diff --git a/pulpcore/app/management/commands/dump-permissions.py b/pulpcore/app/management/commands/dump-permissions.py\n--- a/pulpcore/app/management/commands/dump-permissions.py\n+++ b/pulpcore/app/management/commands/dump-permissions.py\n@@ -153,7 +153,7 @@\n                 obj = _get_url(content_type_id, object_pk)\n             except Exception:\n                 obj = f\"{app_label}.{model}:{object_pk}\"\n-        yield {\"groupname\": groupname, \"object\": obj, \"permissions\": permissions}\n+            yield {\"groupname\": groupname, \"object\": obj, \"permissions\": permissions}\n \n \n class Command(BaseCommand):\n", "issue": "django-admin dump-permissions command fails\n`\r\ndjango-admin dump-permissions  --settings=pulpcore.app.settings\r\nTraceback (most recent call last):\r\n  File \"/var/lib/pulp/pulpvenv/bin/django-admin\", line 8, in <module>\r\n    sys.exit(execute_from_command_line())\r\n  File \"/var/lib/pulp/pulpvenv/lib64/python3.8/site-packages/django/core/management/__init__.py\", line 419, in execute_from_command_line\r\n    utility.execute()\r\n  File \"/var/lib/pulp/pulpvenv/lib64/python3.8/site-packages/django/core/management/__init__.py\", line 413, in execute\r\n    self.fetch_command(subcommand).run_from_argv(self.argv)\r\n  File \"/var/lib/pulp/pulpvenv/lib64/python3.8/site-packages/django/core/management/base.py\", line 354, in run_from_argv\r\n    self.execute(*args, **cmd_options)\r\n  File \"/var/lib/pulp/pulpvenv/lib64/python3.8/site-packages/django/core/management/base.py\", line 398, in execute\r\n    output = self.handle(*args, **options)\r\n  File \"/var/lib/pulp/pulpvenv/lib64/python3.8/site-packages/pulpcore/app/management/commands/dump-permissions.py\", line 231, in handle\r\n    data[\"group_object_permissions\"] = list(_get_group_object_permissions())\r\n  File \"/var/lib/pulp/pulpvenv/lib64/python3.8/site-packages/pulpcore/app/management/commands/dump-permissions.py\", line 156, in _get_group_object_permissions\r\n    yield {\"groupname\": groupname, \"object\": obj, \"permissions\": permissions}\r\nUnboundLocalError: local variable 'groupname' referenced before assignment\r\n`\n", "before_files": [{"content": "from gettext import gettext as _\nfrom functools import lru_cache\nimport json\nimport sys\n\nfrom django.db import connection\nfrom django.core.management import BaseCommand, CommandError\nfrom django.contrib.contenttypes.models import ContentType\n\nfrom pulpcore.app.util import get_url\n\n\nSEPARATOR = \"\\t\"\n\n\n@lru_cache\ndef _get_model(content_type_id):\n    return ContentType.objects.get(pk=content_type_id).model_class()\n\n\ndef _get_url(content_type_id, object_pk):\n    obj = _get_model(content_type_id).objects.get(pk=object_pk)\n    return get_url(obj)\n\n\ndef _get_user_model_permissions():\n    with connection.cursor() as cursor:\n        cursor.execute(\n            \"\"\"\n            SELECT\n                auth_user.id,\n                array_agg(auth_permission.id),\n                username,\n                array_agg(concat(app_label, '.', codename))\n            FROM auth_user_user_permissions\n            LEFT JOIN auth_user ON (user_id=auth_user.id)\n            LEFT JOIN auth_permission ON (permission_id=auth_permission.id)\n            LEFT JOIN django_content_type ON (content_type_id=django_content_type.id)\n            GROUP BY auth_user.id\n            ORDER BY username\n            \"\"\"\n        )\n        while row := cursor.fetchone():\n            (\n                user_id,\n                permission_pks,\n                username,\n                permissions,\n            ) = row\n            yield {\"username\": username, \"permissions\": permissions}\n\n\ndef _get_group_model_permissions():\n    with connection.cursor() as cursor:\n        cursor.execute(\n            \"\"\"\n            SELECT\n                auth_group.id,\n                array_agg(auth_permission.id),\n                auth_group.name,\n                array_agg(concat(app_label, '.', codename))\n            FROM auth_group_permissions\n            LEFT JOIN auth_group ON (group_id=auth_group.id)\n            LEFT JOIN auth_permission ON (permission_id=auth_permission.id)\n            LEFT JOIN django_content_type ON (content_type_id=django_content_type.id)\n            GROUP BY auth_group.id\n            ORDER BY auth_group.name\n            \"\"\"\n        )\n        while row := cursor.fetchone():\n            (\n                group_id,\n                permission_pks,\n                groupname,\n                permissions,\n            ) = row\n\n            yield {\"groupname\": groupname, \"permissions\": permissions}\n\n\ndef _get_user_object_permissions():\n    with connection.cursor() as cursor:\n        cursor.execute(\n            \"\"\"\n            SELECT\n                auth_user.id,\n                django_content_type.id,\n                object_pk,\n                array_agg(auth_permission.id),\n                app_label,\n                model,\n                username,\n                array_agg(concat(app_label, '.', codename))\n            FROM guardian_userobjectpermission\n            LEFT JOIN django_content_type ON (content_type_id=django_content_type.id)\n            LEFT JOIN auth_user ON (user_id=auth_user.id)\n            LEFT JOIN auth_permission ON (permission_id=auth_permission.id)\n            GROUP BY auth_user.id, django_content_type.id, object_pk\n            ORDER BY username, django_content_type.id, object_pk\n            \"\"\"\n        )\n        while row := cursor.fetchone():\n            (\n                user_id,\n                content_type_id,\n                object_pk,\n                permission_pks,\n                app_label,\n                model,\n                username,\n                permissions,\n            ) = row\n            try:\n                obj = _get_url(content_type_id, object_pk)\n            except Exception:\n                obj = f\"{app_label}.{model}:{object_pk}\"\n            yield {\"username\": username, \"object\": obj, \"permissions\": permissions}\n\n\ndef _get_group_object_permissions():\n    with connection.cursor() as cursor:\n        cursor.execute(\n            \"\"\"\n                    SELECT\n                        auth_group.id,\n                        django_content_type.id,\n                        object_pk,\n                        array_agg(auth_permission.id),\n                        app_label,\n                        model,\n                        auth_group.name,\n                        array_agg(concat(app_label, '.', codename))\n                    FROM guardian_groupobjectpermission\n                    LEFT JOIN django_content_type ON (content_type_id=django_content_type.id)\n                    LEFT JOIN auth_group ON (group_id=auth_group.id)\n                    LEFT JOIN auth_permission ON (permission_id=auth_permission.id)\n                    GROUP BY auth_group.id, django_content_type.id, object_pk\n                    ORDER BY auth_group.name, django_content_type.id, object_pk\n                    \"\"\"\n        )\n        while row := cursor.fetchone():\n            (\n                group_id,\n                content_type_id,\n                object_pk,\n                permission_pks,\n                app_label,\n                model,\n                groupname,\n                permissions,\n            ) = row\n            try:\n                obj = _get_url(content_type_id, object_pk)\n            except Exception:\n                obj = f\"{app_label}.{model}:{object_pk}\"\n        yield {\"groupname\": groupname, \"object\": obj, \"permissions\": permissions}\n\n\nclass Command(BaseCommand):\n    \"\"\"\n    Django management command for getting a data dump of deprecated permission.\n    \"\"\"\n\n    help = _(\"Dumps deprecated permissions.\")\n\n    def add_arguments(self, parser):\n        parser.add_argument(\"--tabular\", action=\"store_true\", help=_(\"Output table format.\"))\n\n    def handle(self, *args, **options):\n        tabular = options.get(\"tabular\", False)\n\n        if tabular:\n            try:\n                from prettytable import PrettyTable\n            except ImportError:\n                raise CommandError(\"'prettytable' package must be installed for tabular output.\")\n\n        table_names = connection.introspection.table_names()\n        guardian_uop_available = \"guardian_userobjectpermission\" in table_names\n        guardian_gop_available = \"guardian_groupobjectpermission\" in table_names\n\n        # User model permissions\n        if tabular:\n            print(\"# ==== \" + _(\"User model permissions\") + \" ====\")\n            table = PrettyTable()\n            table.field_names = [_(\"username\"), _(\"permission\")]\n            for t in _get_user_model_permissions():\n                table.add_row([t[\"username\"], t[\"permissions\"][0]])\n                table.add_rows(([\"\", perm] for perm in t[\"permissions\"][1:]))\n            print(table)\n            print()\n\n            print(\"# ==== \" + _(\"Group model permissions\") + \" ====\")\n            table = PrettyTable()\n            table.field_names = [_(\"groupname\"), _(\"permission\")]\n            for t in _get_group_model_permissions():\n                table.add_row([t[\"groupname\"], t[\"permissions\"][0]])\n                table.add_rows(([\"\", perm] for perm in t[\"permissions\"][1:]))\n            print(table)\n            print()\n\n            if guardian_uop_available:\n                print(\"# ==== \" + _(\"User object permissions\") + \" ====\")\n                table = PrettyTable()\n                table.field_names = [_(\"username\"), _(\"object\"), _(\"permission\")]\n                for t in _get_user_object_permissions():\n                    table.add_row([t[\"username\"], t[\"object\"], t[\"permissions\"][0]])\n                    table.add_rows(([\"\", \"\", perm] for perm in t[\"permissions\"][1:]))\n                print(table)\n                print()\n\n            if guardian_gop_available:\n                print(\"# ==== \" + _(\"Group object permissions\") + \" ====\")\n                table = PrettyTable()\n                table.field_names = [_(\"groupname\"), _(\"object\"), _(\"permissions\")]\n                for t in _get_group_object_permissions():\n                    table.add_row([t[\"groupname\"], t[\"object\"], t[\"permissions\"][0]])\n                    table.add_rows(([\"\", \"\", perm] for perm in t[\"permissions\"][1:]))\n                print(table)\n                print()\n\n        else:\n            # TODO Streaming would be nice...\n            data = {\n                \"user_model_permissions\": list(_get_user_model_permissions()),\n                \"group_model_permissions\": list(_get_group_model_permissions()),\n            }\n            if guardian_uop_available:\n                data[\"user_object_permissions\"] = list(_get_user_object_permissions())\n            if guardian_gop_available:\n                data[\"group_object_permissions\"] = list(_get_group_object_permissions())\n\n            json.dump(\n                data,\n                sys.stdout,\n            )\n", "path": "pulpcore/app/management/commands/dump-permissions.py"}], "after_files": [{"content": "from gettext import gettext as _\nfrom functools import lru_cache\nimport json\nimport sys\n\nfrom django.db import connection\nfrom django.core.management import BaseCommand, CommandError\nfrom django.contrib.contenttypes.models import ContentType\n\nfrom pulpcore.app.util import get_url\n\n\nSEPARATOR = \"\\t\"\n\n\n@lru_cache\ndef _get_model(content_type_id):\n    return ContentType.objects.get(pk=content_type_id).model_class()\n\n\ndef _get_url(content_type_id, object_pk):\n    obj = _get_model(content_type_id).objects.get(pk=object_pk)\n    return get_url(obj)\n\n\ndef _get_user_model_permissions():\n    with connection.cursor() as cursor:\n        cursor.execute(\n            \"\"\"\n            SELECT\n                auth_user.id,\n                array_agg(auth_permission.id),\n                username,\n                array_agg(concat(app_label, '.', codename))\n            FROM auth_user_user_permissions\n            LEFT JOIN auth_user ON (user_id=auth_user.id)\n            LEFT JOIN auth_permission ON (permission_id=auth_permission.id)\n            LEFT JOIN django_content_type ON (content_type_id=django_content_type.id)\n            GROUP BY auth_user.id\n            ORDER BY username\n            \"\"\"\n        )\n        while row := cursor.fetchone():\n            (\n                user_id,\n                permission_pks,\n                username,\n                permissions,\n            ) = row\n            yield {\"username\": username, \"permissions\": permissions}\n\n\ndef _get_group_model_permissions():\n    with connection.cursor() as cursor:\n        cursor.execute(\n            \"\"\"\n            SELECT\n                auth_group.id,\n                array_agg(auth_permission.id),\n                auth_group.name,\n                array_agg(concat(app_label, '.', codename))\n            FROM auth_group_permissions\n            LEFT JOIN auth_group ON (group_id=auth_group.id)\n            LEFT JOIN auth_permission ON (permission_id=auth_permission.id)\n            LEFT JOIN django_content_type ON (content_type_id=django_content_type.id)\n            GROUP BY auth_group.id\n            ORDER BY auth_group.name\n            \"\"\"\n        )\n        while row := cursor.fetchone():\n            (\n                group_id,\n                permission_pks,\n                groupname,\n                permissions,\n            ) = row\n\n            yield {\"groupname\": groupname, \"permissions\": permissions}\n\n\ndef _get_user_object_permissions():\n    with connection.cursor() as cursor:\n        cursor.execute(\n            \"\"\"\n            SELECT\n                auth_user.id,\n                django_content_type.id,\n                object_pk,\n                array_agg(auth_permission.id),\n                app_label,\n                model,\n                username,\n                array_agg(concat(app_label, '.', codename))\n            FROM guardian_userobjectpermission\n            LEFT JOIN django_content_type ON (content_type_id=django_content_type.id)\n            LEFT JOIN auth_user ON (user_id=auth_user.id)\n            LEFT JOIN auth_permission ON (permission_id=auth_permission.id)\n            GROUP BY auth_user.id, django_content_type.id, object_pk\n            ORDER BY username, django_content_type.id, object_pk\n            \"\"\"\n        )\n        while row := cursor.fetchone():\n            (\n                user_id,\n                content_type_id,\n                object_pk,\n                permission_pks,\n                app_label,\n                model,\n                username,\n                permissions,\n            ) = row\n            try:\n                obj = _get_url(content_type_id, object_pk)\n            except Exception:\n                obj = f\"{app_label}.{model}:{object_pk}\"\n            yield {\"username\": username, \"object\": obj, \"permissions\": permissions}\n\n\ndef _get_group_object_permissions():\n    with connection.cursor() as cursor:\n        cursor.execute(\n            \"\"\"\n                    SELECT\n                        auth_group.id,\n                        django_content_type.id,\n                        object_pk,\n                        array_agg(auth_permission.id),\n                        app_label,\n                        model,\n                        auth_group.name,\n                        array_agg(concat(app_label, '.', codename))\n                    FROM guardian_groupobjectpermission\n                    LEFT JOIN django_content_type ON (content_type_id=django_content_type.id)\n                    LEFT JOIN auth_group ON (group_id=auth_group.id)\n                    LEFT JOIN auth_permission ON (permission_id=auth_permission.id)\n                    GROUP BY auth_group.id, django_content_type.id, object_pk\n                    ORDER BY auth_group.name, django_content_type.id, object_pk\n                    \"\"\"\n        )\n        while row := cursor.fetchone():\n            (\n                group_id,\n                content_type_id,\n                object_pk,\n                permission_pks,\n                app_label,\n                model,\n                groupname,\n                permissions,\n            ) = row\n            try:\n                obj = _get_url(content_type_id, object_pk)\n            except Exception:\n                obj = f\"{app_label}.{model}:{object_pk}\"\n            yield {\"groupname\": groupname, \"object\": obj, \"permissions\": permissions}\n\n\nclass Command(BaseCommand):\n    \"\"\"\n    Django management command for getting a data dump of deprecated permission.\n    \"\"\"\n\n    help = _(\"Dumps deprecated permissions.\")\n\n    def add_arguments(self, parser):\n        parser.add_argument(\"--tabular\", action=\"store_true\", help=_(\"Output table format.\"))\n\n    def handle(self, *args, **options):\n        tabular = options.get(\"tabular\", False)\n\n        if tabular:\n            try:\n                from prettytable import PrettyTable\n            except ImportError:\n                raise CommandError(\"'prettytable' package must be installed for tabular output.\")\n\n        table_names = connection.introspection.table_names()\n        guardian_uop_available = \"guardian_userobjectpermission\" in table_names\n        guardian_gop_available = \"guardian_groupobjectpermission\" in table_names\n\n        # User model permissions\n        if tabular:\n            print(\"# ==== \" + _(\"User model permissions\") + \" ====\")\n            table = PrettyTable()\n            table.field_names = [_(\"username\"), _(\"permission\")]\n            for t in _get_user_model_permissions():\n                table.add_row([t[\"username\"], t[\"permissions\"][0]])\n                table.add_rows(([\"\", perm] for perm in t[\"permissions\"][1:]))\n            print(table)\n            print()\n\n            print(\"# ==== \" + _(\"Group model permissions\") + \" ====\")\n            table = PrettyTable()\n            table.field_names = [_(\"groupname\"), _(\"permission\")]\n            for t in _get_group_model_permissions():\n                table.add_row([t[\"groupname\"], t[\"permissions\"][0]])\n                table.add_rows(([\"\", perm] for perm in t[\"permissions\"][1:]))\n            print(table)\n            print()\n\n            if guardian_uop_available:\n                print(\"# ==== \" + _(\"User object permissions\") + \" ====\")\n                table = PrettyTable()\n                table.field_names = [_(\"username\"), _(\"object\"), _(\"permission\")]\n                for t in _get_user_object_permissions():\n                    table.add_row([t[\"username\"], t[\"object\"], t[\"permissions\"][0]])\n                    table.add_rows(([\"\", \"\", perm] for perm in t[\"permissions\"][1:]))\n                print(table)\n                print()\n\n            if guardian_gop_available:\n                print(\"# ==== \" + _(\"Group object permissions\") + \" ====\")\n                table = PrettyTable()\n                table.field_names = [_(\"groupname\"), _(\"object\"), _(\"permissions\")]\n                for t in _get_group_object_permissions():\n                    table.add_row([t[\"groupname\"], t[\"object\"], t[\"permissions\"][0]])\n                    table.add_rows(([\"\", \"\", perm] for perm in t[\"permissions\"][1:]))\n                print(table)\n                print()\n\n        else:\n            # TODO Streaming would be nice...\n            data = {\n                \"user_model_permissions\": list(_get_user_model_permissions()),\n                \"group_model_permissions\": list(_get_group_model_permissions()),\n            }\n            if guardian_uop_available:\n                data[\"user_object_permissions\"] = list(_get_user_object_permissions())\n            if guardian_gop_available:\n                data[\"group_object_permissions\"] = list(_get_group_object_permissions())\n\n            json.dump(\n                data,\n                sys.stdout,\n            )\n", "path": "pulpcore/app/management/commands/dump-permissions.py"}]}