-
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.
Merge pull request #43 from edx/rijuma/16210-separate-logic-into-api-…
…layer [ACADEMIC-16210] Moved API logic to api module and added check
- Loading branch information
Showing
23 changed files
with
672 additions
and
130 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
File renamed without changes.
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,142 @@ | ||
""" | ||
Implements an API for updating unit and course settings. | ||
""" | ||
from ai_aside.config_api.internal import NotFoundError, _get_course, _get_unit | ||
from ai_aside.models import AIAsideCourseEnabled, AIAsideUnitEnabled | ||
from ai_aside.waffle import summaries_configuration_enabled | ||
|
||
|
||
def get_course_settings(course_key): | ||
""" | ||
Gets the settings of a course. | ||
Returns: dictionary of the form: | ||
`{'enabled': bool}` | ||
""" | ||
record = _get_course(course_key) | ||
fields = { | ||
'enabled': record.enabled | ||
} | ||
|
||
return fields | ||
|
||
|
||
def set_course_settings(course_key, settings): | ||
""" | ||
Sets the settings of a course. | ||
Expects: settings to be a dictionary of the form: | ||
`{'enabled': bool}` | ||
Raises NotFoundError if the settings are not found. | ||
""" | ||
enabled = settings['enabled'] | ||
|
||
if not isinstance(enabled, bool): | ||
raise TypeError | ||
|
||
update = {'enabled': enabled} | ||
|
||
AIAsideCourseEnabled.objects.update_or_create( | ||
course_key=course_key, | ||
defaults=update, | ||
) | ||
|
||
|
||
def delete_course_settings(course_key): | ||
""" | ||
Deletes the settings of a course. | ||
Raises NotFoundError if the settings are not found. | ||
""" | ||
record = _get_course(course_key) | ||
record.delete() | ||
|
||
|
||
def get_unit_settings(course_key, unit_key): | ||
""" | ||
Gets the settings of a unit. | ||
Returns: dictionary of the form: | ||
`{'enabled': bool}` | ||
""" | ||
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. | ||
Expects: settings as a dictionary of the form: | ||
`{'enabled': bool}` | ||
Raises NotFoundError if the settings are not found. | ||
""" | ||
enabled = settings['enabled'] | ||
|
||
if not isinstance(enabled, bool): | ||
raise TypeError | ||
|
||
settings = {'enabled': enabled} | ||
|
||
AIAsideUnitEnabled.objects.update_or_create( | ||
course_key=course_key, | ||
unit_key=unit_key, | ||
defaults=settings, | ||
) | ||
|
||
|
||
def delete_unit_settings(course_key, unit_key): | ||
""" | ||
Deletes the settings of a unit. | ||
Raises NotFoundError if the settings are not found. | ||
""" | ||
record = _get_unit(course_key, unit_key) | ||
record.delete() | ||
|
||
|
||
def is_summary_config_enabled(course_key): | ||
""" | ||
Is this course even allowed to configure summaries? | ||
This is just a wrapper for the waffle flag for use | ||
by the views. | ||
""" | ||
return summaries_configuration_enabled(course_key) | ||
|
||
|
||
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, always returns false. | ||
if not summaries_configuration_enabled(course_key): | ||
return False | ||
|
||
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 |
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,33 @@ | ||
""" | ||
Internal methods for the API. | ||
""" | ||
from ai_aside.models import AIAsideCourseEnabled, AIAsideUnitEnabled | ||
|
||
|
||
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 |
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
Oops, something went wrong.