From d2a3f2e9527a3bb4933d84958c4ec63dadb34144 Mon Sep 17 00:00:00 2001 From: Alexander Saprykin Date: Thu, 17 Oct 2024 16:32:49 +0200 Subject: [PATCH] Enable pycodestyle warnings for ruff (#2316) --- .../0012_move_collections_by_certification.py | 6 +++--- ...0025_add_content_guard_to_distributions.py | 2 +- .../migrations/0029_move_perms_to_roles.py | 16 +++++++-------- ...pdate_collection_remote_rhcertified_url.py | 6 +++--- .../app/migrations/0038_namespace_sync.py | 4 ++-- .../0047_update_role_search_vector_trigger.py | 4 ++-- ...pdate_collection_remote_rhcertified_url.py | 6 +++--- galaxy_ng/app/migrations/_dab_rbac.py | 2 +- .../test_0029_move_perms_to_roles.py | 20 +++++++++---------- ...pdate_collection_remote_rhcertified_url.py | 8 ++++---- ...pdate_collection_remote_rhcertified_url.py | 8 ++++---- pyproject.toml | 2 ++ 12 files changed, 43 insertions(+), 41 deletions(-) diff --git a/galaxy_ng/app/migrations/0012_move_collections_by_certification.py b/galaxy_ng/app/migrations/0012_move_collections_by_certification.py index 0124737389..0be273e931 100644 --- a/galaxy_ng/app/migrations/0012_move_collections_by_certification.py +++ b/galaxy_ng/app/migrations/0012_move_collections_by_certification.py @@ -37,7 +37,7 @@ to_version UUID; BEGIN IF NOT EXISTS(SELECT 1 FROM ansible_collectionversion cv WHERE cv.certification = filter) THEN - RAISE NOTICE 'Nothing to migrate from %, to %, filter %', + RAISE NOTICE 'Nothing to migrate from %, to %, filter %', from_repo_name, to_repo_name, filter; RETURN; END IF; @@ -70,11 +70,11 @@ core_repositorycontent, ansible_collectionversion IN ACCESS EXCLUSIVE MODE; - + SELECT _move_collection_versions_by_certification( 'published', 'rejected', 'not_certified' ); - + SELECT _move_collection_versions_by_certification( 'published', 'staging', 'needs_review' ); diff --git a/galaxy_ng/app/migrations/0025_add_content_guard_to_distributions.py b/galaxy_ng/app/migrations/0025_add_content_guard_to_distributions.py index 0ee29e7328..793b180c45 100644 --- a/galaxy_ng/app/migrations/0025_add_content_guard_to_distributions.py +++ b/galaxy_ng/app/migrations/0025_add_content_guard_to_distributions.py @@ -32,4 +32,4 @@ class Migration(migrations.Migration): code=add_content_guard, reverse_code=migrations.RunPython.noop ) - ] \ No newline at end of file + ] diff --git a/galaxy_ng/app/migrations/0029_move_perms_to_roles.py b/galaxy_ng/app/migrations/0029_move_perms_to_roles.py index 5025ff9b1b..c38a3c3f3b 100644 --- a/galaxy_ng/app/migrations/0029_move_perms_to_roles.py +++ b/galaxy_ng/app/migrations/0029_move_perms_to_roles.py @@ -170,7 +170,7 @@ def get_roles_from_permissions(permission_iterable, translator, Role, Permission # Use set comparisons instead of querysets to avoid unnecesary trips to the DB permissions = {(p.content_type.app_label, p.codename) for p in permission_iterable} - + # Iterate through each locked role, apply any roles that match the group's permission # set and remove any permissions that are applied via roles for locked_perm_names, locked_rolename in translator: @@ -186,9 +186,9 @@ def get_roles_from_permissions(permission_iterable, translator, Role, Permission # don't bother setting the permissions on the locked roles. They'll get applied in # the post migration hook. role, _ = Role.objects.get_or_create(name=locked_rolename, locked=True) - roles_to_add.append(role) + roles_to_add.append(role) permissions = permissions - role_perms - + for label, perm in permissions: # prefix permission roles with _permission: instead of galaxy. so that they are hidden # by default in the roles UI. @@ -223,7 +223,7 @@ def get_global_group_permissions(group, Role, GroupRole, Permission): # Add locked roles that match the group's permission set for role in roles: group_roles.append(GroupRole(group=group, role=role)) - + return group_roles @@ -250,7 +250,7 @@ def get_object_group_permissions(group, Role, GroupRole, ContentType, Permission objects_with_perms[key].append(permission_id) else: objects_with_perms[key] = [permission_id,] - + # for each object permission that this group has, map it to a role. for k in objects_with_perms: perm_list = objects_with_perms[k] @@ -327,14 +327,14 @@ def migrate_group_permissions_to_roles(apps, schema_editor): - galaxy.delete_collection - galaxy.view_group - galaxy.view_user - + The following roles would get applied: - galaxy.collection_namespace_owner - _permission:galaxy.view_group - _permission:galaxy.view_user galaxy.collection_namespace_owner is applied because the user has all the permissions that match it. - After applying galaxy.collection_namespace_owner, the view_group and view_group permissions are left + After applying galaxy.collection_namespace_owner, the view_group and view_group permissions are left over so _permission:galaxy.view_group and _permission:galaxy.view_user are created for each missing permission and added to the group. _permision: roles will only have the a single permission in them for . @@ -431,7 +431,7 @@ def edit_guardian_tables(apps, schema_editor): for name in fk_constraints: cursor.execute( f"ALTER TABLE {table} DROP CONSTRAINT {name};" - ) + ) def clear_model_permissions(apps, schema_editor): diff --git a/galaxy_ng/app/migrations/0030_update_collection_remote_rhcertified_url.py b/galaxy_ng/app/migrations/0030_update_collection_remote_rhcertified_url.py index 696a3e2500..6f85b223ff 100644 --- a/galaxy_ng/app/migrations/0030_update_collection_remote_rhcertified_url.py +++ b/galaxy_ng/app/migrations/0030_update_collection_remote_rhcertified_url.py @@ -2,12 +2,12 @@ def update_collection_remote_rhcertified_url(apps, schema_editor): """ - Updates the existing collection remote `rh-certified` url field + Updates the existing collection remote `rh-certified` url field if startswith `https://cloud.redhat.com/`. """ - + CollectionRemote = apps.get_model('ansible', 'CollectionRemote') - + rh_remote = CollectionRemote.objects.filter(name='rh-certified').first() if rh_remote and rh_remote.url.startswith('https://cloud.redhat.com/'): diff --git a/galaxy_ng/app/migrations/0038_namespace_sync.py b/galaxy_ng/app/migrations/0038_namespace_sync.py index 27d1fdbe05..36fd0d69f2 100644 --- a/galaxy_ng/app/migrations/0038_namespace_sync.py +++ b/galaxy_ng/app/migrations/0038_namespace_sync.py @@ -8,7 +8,7 @@ def calculate_metadata_sha256(metadata): """Calculates the metadata_sha256 from the other metadata fields.""" metadata_json = json.dumps(metadata, sort_keys=True).encode("utf-8") hasher = hashlib.sha256(metadata_json) - + return hasher.hexdigest() @@ -74,7 +74,7 @@ def add_namespace_metadata_to_published_repository(apps, schema_editor): RepositoryContent = apps.get_model('core', 'RepositoryContent') RepositoryVersion = apps.get_model('core', 'RepositoryVersion') RepositoryVersionContentDetails = apps.get_model('core', 'RepositoryVersionContentDetails') - + repo = AnsibleDistribution.objects.get(base_path="published").repository repo_v = RepositoryVersion.objects.filter(repository=repo).order_by("-number").first() diff --git a/galaxy_ng/app/migrations/0047_update_role_search_vector_trigger.py b/galaxy_ng/app/migrations/0047_update_role_search_vector_trigger.py index e8b686709f..2ccb3acace 100755 --- a/galaxy_ng/app/migrations/0047_update_role_search_vector_trigger.py +++ b/galaxy_ng/app/migrations/0047_update_role_search_vector_trigger.py @@ -18,8 +18,8 @@ _search_vector := (((( setweight(to_tsvector(COALESCE(_namespace, '')), 'A') || setweight(to_tsvector(COALESCE(NEW."name", '')), 'A')) - || setweight(to_tsvector(COALESCE(((NEW."full_metadata"->'tags'))::text, '')), 'B')) - || setweight(to_tsvector(COALESCE(((NEW."full_metadata"->'platforms'))::text, '')), 'C')) + || setweight(to_tsvector(COALESCE(((NEW."full_metadata"->'tags'))::text, '')), 'B')) + || setweight(to_tsvector(COALESCE(((NEW."full_metadata"->'platforms'))::text, '')), 'C')) || setweight(to_tsvector(COALESCE((NEW."full_metadata"->>'description'), '')), 'D')); INSERT INTO galaxy_legacyrolesearchvector(role_id,search_vector,modified) diff --git a/galaxy_ng/app/migrations/0048_update_collection_remote_rhcertified_url.py b/galaxy_ng/app/migrations/0048_update_collection_remote_rhcertified_url.py index 4ac7917c23..5438164ae5 100644 --- a/galaxy_ng/app/migrations/0048_update_collection_remote_rhcertified_url.py +++ b/galaxy_ng/app/migrations/0048_update_collection_remote_rhcertified_url.py @@ -2,12 +2,12 @@ def update_collection_remote_rhcertified_url(apps, schema_editor): """ - Updates the existing collection remote `rh-certified` url field + Updates the existing collection remote `rh-certified` url field to add `content/published/`. """ - + CollectionRemote = apps.get_model('ansible', 'CollectionRemote') - + rh_remote = CollectionRemote.objects.filter(name='rh-certified').first() if rh_remote and rh_remote.url == 'https://console.redhat.com/api/automation-hub/': diff --git a/galaxy_ng/app/migrations/_dab_rbac.py b/galaxy_ng/app/migrations/_dab_rbac.py index d11c34340c..cf5ddc0dd6 100644 --- a/galaxy_ng/app/migrations/_dab_rbac.py +++ b/galaxy_ng/app/migrations/_dab_rbac.py @@ -52,7 +52,7 @@ def split_pulp_roles(apps, schema_editor): for assignment_cls in (UserRole, GroupRole): for pulp_assignment in assignment_cls.objects.filter(role=corerole, content_type__isnull=False): if pulp_assignment.content_type_id not in split_roles: - + # Get all permissions relevant to this content model. # If any model (like synclist) hasn't been registered in the permission # system, it should not be split/recreated ... diff --git a/galaxy_ng/tests/unit/migrations/test_0029_move_perms_to_roles.py b/galaxy_ng/tests/unit/migrations/test_0029_move_perms_to_roles.py index d94646afe2..0f56e929d3 100644 --- a/galaxy_ng/tests/unit/migrations/test_0029_move_perms_to_roles.py +++ b/galaxy_ng/tests/unit/migrations/test_0029_move_perms_to_roles.py @@ -187,7 +187,7 @@ def _get_permission(self, permission_name): content_type__app_label=app_label, codename=codename ) - + def _run_migrations(self): migration = importlib.import_module("galaxy_ng.app.migrations.0029_move_perms_to_roles") migration.migrate_group_permissions_to_roles(apps, None) @@ -231,7 +231,7 @@ def _get_assign_perm(self): if self._assign_perm is None: from guardian.shortcuts import assign_perm as guardian_assign_perm self._assign_perm = guardian_assign_perm - + return self._assign_perm @@ -243,7 +243,7 @@ def test_group_model_locked_role_mapping(self): name=role, permissions=LOCKED_ROLES[role]["permissions"] ) - + self._run_migrations() for role in roles: @@ -284,7 +284,7 @@ def test_group_model_locked_role_mapping_with_dangling_permissions(self): ] self.assertEqual(GroupRole.objects.filter(group=group).count(), len(expected_roles)) - + for role in expected_roles: role_obj = Role.objects.get(name=role) self.assertEqual(GroupRole.objects.filter(group=group, role=role_obj).count(), 1) @@ -366,25 +366,25 @@ def test_group_object_locked_role_mapping(self): for role in ns_users: permissions = LOCKED_ROLES[role]["permissions"] user, group = ns_users[role] - + for perm in permissions: self.assertTrue(user.has_perm(perm, obj=namespace)) self.assertFalse(user.has_perm(perm)) - + self.assertEqual(GroupRole.objects.filter(group=group).count(), 1) self.assertTrue(self._has_role(group, role, obj=namespace)) for role in c_ns_users: permissions = LOCKED_ROLES[role]["permissions"] user, group = c_ns_users[role] - + for perm in permissions: self.assertTrue(user.has_perm(perm, obj=container_namespace)) self.assertFalse(user.has_perm(perm)) - + self.assertEqual(GroupRole.objects.filter(group=group).count(), 1) self.assertTrue(self._has_role(group, role, obj=container_namespace)) - + # Verify super permissions work self.assertTrue(self._has_role(namespace_super_group, "galaxy.collection_namespace_owner", namespace)) self.assertTrue( @@ -423,6 +423,6 @@ def test_empty_groups(self): user, group = self._create_user_and_group_with_permissions("test", []) self._run_migrations() - + self.assertEqual(UserRole.objects.filter(user=user).count(), 0) self.assertEqual(GroupRole.objects.filter(group=group).count(), 0) diff --git a/galaxy_ng/tests/unit/migrations/test_0030_update_collection_remote_rhcertified_url.py b/galaxy_ng/tests/unit/migrations/test_0030_update_collection_remote_rhcertified_url.py index 6517841ef8..897fd68ea6 100644 --- a/galaxy_ng/tests/unit/migrations/test_0030_update_collection_remote_rhcertified_url.py +++ b/galaxy_ng/tests/unit/migrations/test_0030_update_collection_remote_rhcertified_url.py @@ -12,16 +12,16 @@ class TestRemoteRHCertifiedCollectionURL(TestCase): def _run_migration(self): migration = import_module("galaxy_ng.app.migrations.0030_update_collection_remote_rhcertified_url") migration.update_collection_remote_rhcertified_url(apps, connection.schema_editor()) - + def test_correct_url_update_after_migration(self): url = 'https://cloud.redhat.com/api/automation-hub/content/1237261-synclist/' - CollectionRemote.objects.filter(name="rh-certified").update(url=url) - + CollectionRemote.objects.filter(name="rh-certified").update(url=url) + remote = CollectionRemote.objects.get(name='rh-certified') self.assertEqual(remote.url, url) self._run_migration() - + remote.refresh_from_db() self.assertEqual(remote.url, 'https://console.redhat.com/api/automation-hub/content/1237261-synclist/') diff --git a/galaxy_ng/tests/unit/migrations/test_0048_update_collection_remote_rhcertified_url.py b/galaxy_ng/tests/unit/migrations/test_0048_update_collection_remote_rhcertified_url.py index f44a5c19a3..f3bb965ff3 100644 --- a/galaxy_ng/tests/unit/migrations/test_0048_update_collection_remote_rhcertified_url.py +++ b/galaxy_ng/tests/unit/migrations/test_0048_update_collection_remote_rhcertified_url.py @@ -12,16 +12,16 @@ class TestRemoteRHCertifiedCollectionURL(TestCase): def _run_migration(self): migration = import_module("galaxy_ng.app.migrations.0048_update_collection_remote_rhcertified_url") migration.update_collection_remote_rhcertified_url(apps, connection.schema_editor()) - + def test_correct_url_update_after_migration(self): url = 'https://console.redhat.com/api/automation-hub/' - CollectionRemote.objects.filter(name="rh-certified").update(url=url) - + CollectionRemote.objects.filter(name="rh-certified").update(url=url) + remote = CollectionRemote.objects.get(name='rh-certified') self.assertEqual(remote.url, url) self._run_migration() - + remote.refresh_from_db() self.assertEqual(remote.url, 'https://console.redhat.com/api/automation-hub/content/published/') diff --git a/pyproject.toml b/pyproject.toml index cf248eb07d..4a860b5500 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -101,6 +101,8 @@ extend-exclude = [ [tool.ruff.lint] extend-select = [ + # pycodestyle warnings + "W", # flake8-bugbear "B", # flake8-comprehensions