Commit 8c3dd293 by Robert Raposa Committed by GitHub

Merge pull request #16105 from edx/robrap/refactor-course-goal-enum

Refactor Goal Enum to Choices (a.k.a. No Commit Left Behind)
parents 649f3ccd 23fbde8e
"""
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]
......@@ -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(
......
......@@ -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.
......
......@@ -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
'<div tabindex="0" aria-label="{aria_label_choice}" class="goal-option dismissible" '
'data-choice="{goal_key}">'
).format(
goal_key=CourseGoalOption.UNSURE.value,
goal_key=GOAL_KEY_CHOICES.unsure,
aria_label_choice=Text(_("Set goal to: {choice}")).format(
choice=get_goal_text(CourseGoalOption.UNSURE.value)
choice=GOAL_KEY_CHOICES[GOAL_KEY_CHOICES.unsure]
),
),
choice=Text(_('{choice}')).format(
choice=get_goal_text(CourseGoalOption.UNSURE.value),
choice=GOAL_KEY_CHOICES[GOAL_KEY_CHOICES.unsure],
),
closing_tag=HTML('</div>'),
)
# Add the option to set a goal to earn a certificate,
# complete the course or explore the course
goal_options = [CourseGoalOption.CERTIFY.value, CourseGoalOption.COMPLETE.value, CourseGoalOption.EXPLORE.value]
goal_options = [GOAL_KEY_CHOICES.certify, GOAL_KEY_CHOICES.complete, GOAL_KEY_CHOICES.explore]
for goal_key in goal_options:
goal_text = get_goal_text(goal_key)
goal_text = GOAL_KEY_CHOICES[goal_key]
goal_choices_html += HTML(
'{initial_tag}{goal_text}{closing_tag}'
).format(
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment