Skip to content

Make JSONSetting more ergonomic #7076

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions lms/models/json_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,9 @@ class JSONSettings(MutableDict):
>>> model.settings.changed()
"""

fields: Mapping[Any, JSONSetting] | None = None
fields: Mapping[Any, JSONSetting]
"""
An optional spec for the acceptable fields and types.
An spec for the acceptable fields and types.
"""

def get(self, group: str, key: str, default=None): # type: ignore[override]
Expand All @@ -109,7 +109,12 @@ def get(self, group: str, key: str, default=None): # type: ignore[override]
"""
return super().get(group, {}).get(key, default)

def get_setting(self, setting: JSONSetting):
def get_setting(self, setting_name: Any) -> Any:
setting = self.fields.get(setting_name)
if not setting:
err = f"Unknown setting for type {self.__class__.__name__}: {setting_name}"
raise ValueError(err)

value = self.get(setting.group, setting.key, setting.default)
if value is None: # None is used a sentinel for "unset" or "default"
value = setting.default
Expand Down
4 changes: 2 additions & 2 deletions lms/resources/_js_config/file_picker_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ def blackboard_config(cls, request, application_instance):
def moodle_config(cls, request, application_instance):
ai_settings = application_instance.settings
files_enabled = ai_settings.get_setting(
ai_settings.fields[ai_settings.Settings.MOODLE_FILES_ENABLED]
ai_settings.Settings.MOODLE_FILES_ENABLED
)
pages_enabled = ai_settings.get_setting(
ai_settings.fields[ai_settings.Settings.MOODLE_PAGES_ENABLED]
ai_settings.Settings.MOODLE_PAGES_ENABLED
)

config = {"enabled": files_enabled, "pagesEnabled": pages_enabled}
Expand Down
4 changes: 1 addition & 3 deletions lms/services/roster.py
Original file line number Diff line number Diff line change
Expand Up @@ -523,9 +523,7 @@ def _get_roster_users(
if LTIRoleService.is_instructor(roles) or (
LTIRoleService.is_learner(roles)
and ai_settings.get_setting(
ai_settings.fields[
ai_settings.Settings.HYPOTHESIS_COLLECT_STUDENT_EMAILS
]
ai_settings.Settings.HYPOTHESIS_COLLECT_STUDENT_EMAILS
)
):
email = member.get("email")
Expand Down
4 changes: 1 addition & 3 deletions lms/services/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,7 @@ def upsert_user(self, lti_user: LTIUser) -> User:
if lti_user.is_instructor or (
lti_user.is_learner
and ai_settings.get_setting(
ai_settings.fields[
ai_settings.Settings.HYPOTHESIS_COLLECT_STUDENT_EMAILS
]
ai_settings.Settings.HYPOTHESIS_COLLECT_STUDENT_EMAILS
)
):
# Always store instructor emails
Expand Down
4 changes: 1 addition & 3 deletions lms/services/youtube.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,7 @@ def factory(_context, request) -> YouTubeService:
app_settings = request.registry.settings

return YouTubeService(
enabled=ai_settings.get_setting(
ai_settings.fields[ai_settings.Settings.YOUTUBE_ENABLED]
),
enabled=ai_settings.get_setting(ai_settings.Settings.YOUTUBE_ENABLED),
api_key=app_settings.get("youtube_api_key"),
http=request.find_service(name="http"),
)
8 changes: 2 additions & 6 deletions lms/views/lti/basic_launch.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,16 +250,12 @@ def _show_document(self, assignment):
# should be enabled here via `self.context.js_config.enable_client_feature`.
ai_settings = self.request.lti_user.application_instance.settings
if (
ai_settings.get_setting(
ai_settings.fields[ai_settings.Settings.HYPOTHESIS_MENTIONS]
)
ai_settings.get_setting(ai_settings.Settings.HYPOTHESIS_MENTIONS)
and
# Currently the only way to notify about mentions is email,
# if we are not emails disable mentions altogether
ai_settings.get_setting(
ai_settings.fields[
ai_settings.Settings.HYPOTHESIS_COLLECT_STUDENT_EMAILS
]
ai_settings.Settings.HYPOTHESIS_COLLECT_STUDENT_EMAILS
)
):
self.context.js_config.enable_client_feature("at_mentions")
Expand Down
Loading