Skip to content

Commit 5996ef9

Browse files
refactor: address quality issues
1 parent b2bb892 commit 5996ef9

6 files changed

Lines changed: 12 additions & 17 deletions

File tree

openedx_authz/api/roles.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
from openedx_authz.api.permissions import get_permission_from_policy
2323
from openedx_authz.engine.enforcer import AuthzEnforcer
2424

25-
2625
__all__ = [
2726
"get_permissions_for_single_role",
2827
"get_permissions_for_roles",

openedx_authz/apps.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
"""
44

55
from django.apps import AppConfig
6-
from django.core.exceptions import ImproperlyConfigured
76

87

98
class OpenedxAuthzConfig(AppConfig):
@@ -40,10 +39,3 @@ class OpenedxAuthzConfig(AppConfig):
4039
},
4140
},
4241
}
43-
44-
def ready(self):
45-
"""Initialization layer for the openedx_authz app."""
46-
# DO NOT initialize the enforcer here to avoid issues when
47-
# apps are not fully loaded (e.g., while pulling translations).
48-
# It's best to lazy load the enforcer when needed it's first used.
49-
pass

openedx_authz/engine/apps.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
1+
"""Initialization for the casbin_adapter Django application.
2+
3+
This overrides the default AppConfig to avoid making queries to the database
4+
when the app is not fully loaded (e.g., while pulling translations). Moved
5+
the initialization of the enforcer to a lazy load when it's first used.
6+
7+
See openedx_authz/engine/enforcer.py for the enforcer implementation.
8+
"""
9+
110
from django.apps import AppConfig
2-
from django.conf import settings
3-
from django.core.exceptions import ImproperlyConfigured
411

512

613
class CasbinAdapterConfig(AppConfig):
714
name = "casbin_adapter"
815

916
def ready(self):
10-
"""Initialization layer for the casbin_adapter app."""
17+
"""Initialize the casbin_adapter app."""
1118
# DO NOT initialize the enforcer here to avoid issues when
1219
# apps are not fully loaded (e.g., while pulling translations).
1320
# It's best to lazy load the enforcer when needed it's first used.
14-
pass

openedx_authz/engine/enforcer.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@
1919
import logging
2020

2121
from casbin import FastEnforcer
22+
from casbin_adapter.enforcer import initialize_enforcer
2223
from django.conf import settings
2324

2425
from openedx_authz.engine.adapter import ExtendedAdapter
2526
from openedx_authz.engine.watcher import Watcher
26-
from casbin_adapter.enforcer import initialize_enforcer
2727

2828
logger = logging.getLogger(__name__)
2929

@@ -86,7 +86,7 @@ def _initialize_enforcer() -> FastEnforcer:
8686
# Best to lazy load it when it's first used to ensure the database is ready and avoid
8787
# issues when the app is not fully loaded (e.g., while pulling translations, etc.).
8888
initialize_enforcer(db_alias)
89-
except Exception as e: # pylint: disable=broad-exception-caught
89+
except Exception as e:
9090
logger.error(f"Failed to initialize Casbin enforcer with DB alias '{db_alias}': {e}")
9191
raise
9292

openedx_authz/management/commands/load_policies.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
from openedx_authz.engine.utils import migrate_policy_between_enforcers
1717

1818

19-
2019
class Command(BaseCommand):
2120
"""Django management command to load policies into the authorization Django model.
2221

openedx_authz/tests/api/test_roles.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
from openedx_authz.engine.utils import migrate_policy_between_enforcers
3636

3737

38-
3938
class BaseRolesTestCase(TestCase):
4039
"""Base test case with helper methods for roles testing.
4140

0 commit comments

Comments
 (0)