Skip to content

Commit

Permalink
Enable pycodestyle warnings for ruff (#2316)
Browse files Browse the repository at this point in the history
  • Loading branch information
cutwater authored Oct 17, 2024
1 parent ccce076 commit d2a3f2e
Show file tree
Hide file tree
Showing 12 changed files with 43 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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'
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@ class Migration(migrations.Migration):
code=add_content_guard,
reverse_code=migrations.RunPython.noop
)
]
]
16 changes: 8 additions & 8 deletions galaxy_ng/app/migrations/0029_move_perms_to_roles.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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.
Expand Down Expand Up @@ -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


Expand All @@ -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]
Expand Down Expand Up @@ -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:<perm_name> roles will only have the
a single permission in them for <perm_name>.
Expand Down Expand Up @@ -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):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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/'):
Expand Down
4 changes: 2 additions & 2 deletions galaxy_ng/app/migrations/0038_namespace_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()


Expand Down Expand Up @@ -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()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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/':
Expand Down
2 changes: 1 addition & 1 deletion galaxy_ng/app/migrations/_dab_rbac.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 ...
Expand Down
20 changes: 10 additions & 10 deletions galaxy_ng/tests/unit/migrations/test_0029_move_perms_to_roles.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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


Expand All @@ -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:
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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)
Original file line number Diff line number Diff line change
Expand Up @@ -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/')

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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/')

Expand Down
2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ extend-exclude = [

[tool.ruff.lint]
extend-select = [
# pycodestyle warnings
"W",
# flake8-bugbear
"B",
# flake8-comprehensions
Expand Down

0 comments on commit d2a3f2e

Please sign in to comment.