-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: ACADEMIC-16210: Created a module for API methods
- Loading branch information
Showing
10 changed files
with
552 additions
and
100 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
""" | ||
Implements an API for updating unit and course settings. | ||
""" | ||
from ai_aside.models import AIAsideCourseEnabled, AIAsideUnitEnabled | ||
from ai_aside.waffle import summaries_configuration_enabled | ||
|
||
|
||
class NotFoundError(Exception): | ||
"Raised when the course/unit is not found in the database" | ||
|
||
|
||
def _get_course(course_key): | ||
"Private method that gets a course based on an id" | ||
try: | ||
record = AIAsideCourseEnabled.objects.get( | ||
course_key=course_key, | ||
) | ||
except AIAsideCourseEnabled.DoesNotExist as exc: | ||
raise NotFoundError from exc | ||
|
||
return record | ||
|
||
|
||
def _get_unit(course_key, unit_key): | ||
"Private method that gets a unit based on an id" | ||
try: | ||
record = AIAsideUnitEnabled.objects.get( | ||
course_key=course_key, | ||
unit_key=unit_key, | ||
) | ||
except AIAsideUnitEnabled.DoesNotExist as exc: | ||
raise NotFoundError from exc | ||
|
||
return record | ||
|
||
|
||
def get_course_settings(course_key): | ||
"Gets the settings of a course" | ||
record = _get_course(course_key) | ||
fields = { | ||
'enabled': record.enabled | ||
} | ||
|
||
return fields | ||
|
||
|
||
def set_course_settings(course_key, settings): | ||
"Sets the settings of a course" | ||
|
||
enabled = settings['enabled'] | ||
|
||
if not isinstance(enabled, bool): | ||
raise TypeError | ||
|
||
update = {'enabled': enabled} | ||
|
||
AIAsideCourseEnabled.objects.update_or_create( | ||
course_key=course_key, | ||
defaults=update, | ||
) | ||
|
||
return True | ||
|
||
|
||
def delete_course_settings(course_key): | ||
"Deletes the settings of a course" | ||
record = _get_course(course_key) | ||
record.delete() | ||
|
||
return True | ||
|
||
|
||
def get_unit_settings(course_key, unit_key): | ||
"Gets the settings of a course's unit" | ||
record = _get_unit(course_key, unit_key) | ||
|
||
fields = { | ||
'enabled': record.enabled | ||
} | ||
|
||
return fields | ||
|
||
|
||
def set_unit_settings(course_key, unit_key, settings): | ||
"Sets the settings of a course's unit" | ||
|
||
enabled = settings['enabled'] | ||
|
||
if not isinstance(enabled, bool): | ||
raise TypeError | ||
|
||
update = {'enabled': enabled} | ||
|
||
AIAsideUnitEnabled.objects.update_or_create( | ||
course_key=course_key, | ||
unit_key=unit_key, | ||
defaults=update, | ||
) | ||
|
||
return True | ||
|
||
|
||
def delete_unit_settings(course_key, unit_key): | ||
"Deletes the settings of a course's unit" | ||
record = _get_unit(course_key, unit_key) | ||
record.delete() | ||
|
||
return True | ||
|
||
|
||
def is_summary_enabled(course_key, unit_key=None): | ||
""" | ||
Gets the enabled state of a course's unit. | ||
It considers both the state of a unit's override and a course defaults. | ||
""" | ||
|
||
# If the feature flag is disabled, do not restrict | ||
if not summaries_configuration_enabled(course_key): | ||
return True | ||
|
||
if unit_key is not None: | ||
try: | ||
unit = _get_unit(course_key, unit_key) | ||
|
||
if unit is not None: | ||
return unit.enabled | ||
except NotFoundError: | ||
pass | ||
|
||
try: | ||
course = _get_course(course_key) | ||
except NotFoundError: | ||
return False | ||
|
||
if course is not None: | ||
return course.enabled | ||
|
||
return False |
Oops, something went wrong.