Skip to content

Commit 552a161

Browse files
refactor: address quality issues
1 parent 969f795 commit 552a161

5 files changed

Lines changed: 17 additions & 26 deletions

File tree

openedx_authz/api/roles.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ def assign_role_to_subject_in_scope(subject: SubjectData, role: RoleData, scope:
242242
subject=subject.namespaced_key,
243243
role=role.namespaced_key,
244244
scope=scope.namespaced_key,
245-
actor=getattr(get_current_user(), "username", None)
245+
actor=getattr(get_current_user(), "username", None),
246246
)
247247
)
248248
)
@@ -293,7 +293,7 @@ def unassign_role_from_subject_in_scope(subject: SubjectData, role: RoleData, sc
293293
subject=subject.namespaced_key,
294294
role=role.namespaced_key,
295295
scope=scope.namespaced_key,
296-
actor=getattr(get_current_user(), "username", None)
296+
actor=getattr(get_current_user(), "username", None),
297297
)
298298
)
299299
)
@@ -613,6 +613,5 @@ def get_all_role_assignments_per_scope_type(scope_types: tuple[type[ScopeData],
613613
list[RoleAssignmentData]: All assignments whose scope is an instance of any of the given scope types.
614614
"""
615615
return [
616-
role_assignment for role_assignment in get_role_assignments()
617-
if isinstance(role_assignment.scope, scope_types)
616+
role_assignment for role_assignment in get_role_assignments() if isinstance(role_assignment.scope, scope_types)
618617
]

openedx_authz/engine/utils.py

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -264,8 +264,7 @@ def migrate_legacy_course_roles_to_authz(course_access_role_model, course_id_lis
264264

265265
# Permission applied to individual user
266266
logger.info(
267-
f"Migrating permission for User: {permission.user.username} "
268-
f"to Role: {role} in Scope: {scope_external_key}"
267+
f"Migrating permission for User: {permission.user.username} to Role: {role} in Scope: {scope_external_key}"
269268
)
270269

271270
is_user_added = assign_role_to_user_in_scope(
@@ -322,25 +321,26 @@ def migrate_authz_to_legacy_course_roles(
322321
_validate_migration_input(course_id_list, org_id)
323322

324323
role_assignments = get_all_role_assignments_per_scope_type(
325-
scope_types=(CourseOverviewData, OrgCourseOverviewGlobData,)
324+
scope_types=(
325+
CourseOverviewData,
326+
OrgCourseOverviewGlobData,
327+
)
326328
)
327329

328330
# Two cases here:
329331
# 1. org_id provided: filter by org — includes org-level glob and course-level scopes for that org.
330332
# 2. only course_id_list provided: filter by course_id — org-level glob scopes are excluded (no course_id).
331333
if org_id:
332334
role_assignments = [
333-
role_assignment
334-
for role_assignment in role_assignments
335-
if role_assignment.scope.org == org_id
335+
role_assignment for role_assignment in role_assignments if role_assignment.scope.org == org_id
336336
]
337337

338338
if course_id_list and not org_id:
339339
role_assignments = [
340340
role_assignment
341341
for role_assignment in role_assignments
342-
if isinstance(role_assignment.scope, CourseOverviewData) and
343-
role_assignment.scope.course_id in course_id_list
342+
if isinstance(role_assignment.scope, CourseOverviewData)
343+
and role_assignment.scope.course_id in course_id_list
344344
]
345345

346346
roles_with_errors = []
@@ -350,13 +350,10 @@ def migrate_authz_to_legacy_course_roles(
350350
user_external_keys = {assignment.subject.external_key for assignment in role_assignments}
351351
users_by_username = {
352352
subject.user.username: subject.user
353-
for subject in user_subject_model.objects.filter(
354-
user__username__in=user_external_keys
355-
).select_related("user")
353+
for subject in user_subject_model.objects.filter(user__username__in=user_external_keys).select_related("user")
356354
}
357355

358356
for role_assignment in role_assignments:
359-
360357
# Per valid role assignment, create corresponding CourseAccessRole entry
361358
# depending on whether the scope is course-level or org-level glob
362359
try:

openedx_authz/migrations/0008_roleassignmentaudit.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55

66
class Migration(migrations.Migration):
7-
87
dependencies = [
98
("openedx_authz", "0007_coursescope"),
109
]
@@ -47,8 +46,7 @@ class Migration(migrations.Migration):
4746
"scope",
4847
models.CharField(
4948
help_text=(
50-
"Namespaced key of the scope"
51-
" (e.g. 'course-v1^course-v1:org+course+run') or glob pattern."
49+
"Namespaced key of the scope (e.g. 'course-v1^course-v1:org+course+run') or glob pattern."
5250
),
5351
max_length=255,
5452
),
@@ -58,8 +56,7 @@ class Migration(migrations.Migration):
5856
models.CharField(
5957
blank=True,
6058
help_text=(
61-
"Username of the user who performed the operation,"
62-
" or None for system-initiated actions."
59+
"Username of the user who performed the operation, or None for system-initiated actions."
6360
),
6461
max_length=150,
6562
null=True,

openedx_authz/models/core.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from openedx_authz.constants import AUTHZ_POLICY_ATTRIBUTES_SEPARATOR
1313
from openedx_authz.engine.filter import Filter
1414

15+
1516
class BaseRegistryModel(models.Model):
1617
"""Base model that supports automatic subclass registration.
1718

openedx_authz/tests/test_migrations.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1301,9 +1301,7 @@ def test_migrate_org_level_scope_creates_org_glob_assignment(self):
13011301
AuthzEnforcer.get_enforcer().load_policy()
13021302

13031303
org_scope = OrgCourseOverviewGlobData.build_external_key(org_short_name_new)
1304-
assignments = get_user_role_assignments_in_scope(
1305-
user_external_key=user.username, scope_external_key=org_scope
1306-
)
1304+
assignments = get_user_role_assignments_in_scope(user_external_key=user.username, scope_external_key=org_scope)
13071305
self.assertEqual(len(assignments), 1)
13081306
self.assertEqual(assignments[0].roles[0], COURSE_ADMIN)
13091307

@@ -1404,8 +1402,7 @@ def test_course_id_list_filter_excludes_glob_and_other_courses(self):
14041402
AuthzEnforcer.get_enforcer().load_policy()
14051403

14061404
errors, successes = migrate_authz_to_legacy_course_roles(
1407-
CourseAccessRole, UserSubject,
1408-
course_id_list=[self.course_id], org_id=None, delete_after_migration=False
1405+
CourseAccessRole, UserSubject, course_id_list=[self.course_id], org_id=None, delete_after_migration=False
14091406
)
14101407

14111408
migrated_users = {assignment.subject.external_key for assignment in successes}

0 commit comments

Comments
 (0)