diff --git a/lms/djangoapps/course_goals/api.py b/lms/djangoapps/course_goals/api.py index b52c1794fa..9efae78b88 100644 --- a/lms/djangoapps/course_goals/api.py +++ b/lms/djangoapps/course_goals/api.py @@ -1,10 +1,7 @@ """ Course Goals Python API """ -from enum import Enum from opaque_keys.edx.keys import CourseKey -from django.utils.translation import ugettext as _ -from openedx.core.djangolib.markup import Text from .models import CourseGoal @@ -16,8 +13,7 @@ def add_course_goal(user, course_id, goal_key): Arguments: user: The user that is setting the goal course_id (string): The id for the course the goal refers to - goal_key (string): The goal key that maps to one of the - enumerated goal keys from CourseGoalOption. + goal_key (string): The goal key for the new goal. """ # Create and save a new course goal @@ -43,34 +39,3 @@ def remove_course_goal(user, course_key): course_goal = get_course_goal(user, course_key) if course_goal: course_goal.delete() - - -class CourseGoalOption(Enum): - """ - Types of goals that a user can select. - - These options are set to a string goal key so that they can be - referenced elsewhere in the code when necessary. - """ - CERTIFY = 'certify' - COMPLETE = 'complete' - EXPLORE = 'explore' - UNSURE = 'unsure' - - @classmethod - def get_course_goal_keys(self): - return [key.value for key in self] - - -def get_goal_text(goal_option): - """ - This function is used to translate the course goal option into - a translated, user-facing string to be used to represent that - particular goal. - """ - return { - CourseGoalOption.CERTIFY.value: Text(_('Earn a certificate')), - CourseGoalOption.COMPLETE.value: Text(_('Complete the course')), - CourseGoalOption.EXPLORE.value: Text(_('Explore the course')), - CourseGoalOption.UNSURE.value: Text(_('Not sure yet')), - }[goal_option] diff --git a/lms/djangoapps/course_goals/models.py b/lms/djangoapps/course_goals/models.py index fac52a729a..b216bbbcdc 100644 --- a/lms/djangoapps/course_goals/models.py +++ b/lms/djangoapps/course_goals/models.py @@ -3,26 +3,27 @@ Course Goals Models """ from django.contrib.auth.models import User from django.db import models +from django.utils.translation import ugettext_lazy as _ from openedx.core.djangoapps.xmodule_django.models import CourseKeyField +from model_utils import Choices + + +# Each goal is represented by a goal key and a string description. +GOAL_KEY_CHOICES = Choices( + ('certify', _('Earn a certificate.')), + ('complete', _('Complete the course.')), + ('explore', _('Explore the course.')), + ('unsure', _('Not sure yet.')), +) class CourseGoal(models.Model): """ - Represents a course goal set by a user on the course home page. - - The goal_key represents the goal key that maps to a translated - string through using the CourseGoalOption class. + Represents a course goal set by the user. """ - GOAL_KEY_CHOICES = ( - ('certify', 'Earn a certificate.'), - ('complete', 'Complete the course.'), - ('explore', 'Explore the course.'), - ('unsure', 'Not sure yet.'), - ) - user = models.ForeignKey(User, blank=False) course_key = CourseKeyField(max_length=255, db_index=True) - goal_key = models.CharField(max_length=100, choices=GOAL_KEY_CHOICES, default='unsure') + goal_key = models.CharField(max_length=100, choices=GOAL_KEY_CHOICES, default=GOAL_KEY_CHOICES.unsure) def __unicode__(self): return 'CourseGoal: {user} set goal to {goal} for course {course}'.format( diff --git a/lms/djangoapps/course_goals/views.py b/lms/djangoapps/course_goals/views.py index 513d52c440..9f589e9f2a 100644 --- a/lms/djangoapps/course_goals/views.py +++ b/lms/djangoapps/course_goals/views.py @@ -11,9 +11,9 @@ from openedx.core.lib.api.permissions import IsStaffOrOwner from rest_framework import permissions, serializers, viewsets from rest_framework.authentication import SessionAuthentication -from .api import CourseGoalOption from .models import CourseGoal + User = get_user_model() @@ -27,19 +27,6 @@ class CourseGoalSerializer(serializers.ModelSerializer): model = CourseGoal fields = ('user', 'course_key', 'goal_key') - def validate_goal_key(self, value): - """ - Ensure that the goal_key is valid. - """ - if value not in CourseGoalOption.get_course_goal_keys(): - raise serializers.ValidationError( - 'Provided goal key, {goal_key}, is not a valid goal key (options= {goal_options}).'.format( - goal_key=value, - goal_options=[option.value for option in CourseGoalOption], - ) - ) - return value - def validate_course_key(self, value): """ Ensure that the course_key is valid. diff --git a/openedx/features/course_experience/views/course_home_messages.py b/openedx/features/course_experience/views/course_home_messages.py index 9b580c8582..a851695f21 100644 --- a/openedx/features/course_experience/views/course_home_messages.py +++ b/openedx/features/course_experience/views/course_home_messages.py @@ -18,7 +18,8 @@ from web_fragments.fragment import Fragment from course_modes.models import CourseMode from courseware.courses import get_course_with_access -from lms.djangoapps.course_goals.api import CourseGoalOption, get_course_goal, get_goal_text +from lms.djangoapps.course_goals.api import get_course_goal +from lms.djangoapps.course_goals.models import GOAL_KEY_CHOICES from openedx.core.djangoapps.plugin_api.views import EdxFragmentView from openedx.core.djangolib.markup import HTML, Text from openedx.features.course_experience import CourseHomeMessages @@ -157,22 +158,22 @@ def _register_course_home_messages(request, course_id, user_access, course_start '