Support adding cohorts from the instructor dashboard
TNL-162
This commit is contained in:
@@ -3,11 +3,12 @@ This file contains the logic for cohort groups, as exposed internally to the
|
||||
forums, and to the cohort admin views.
|
||||
"""
|
||||
|
||||
from django.http import Http404
|
||||
|
||||
import logging
|
||||
import random
|
||||
|
||||
from django.http import Http404
|
||||
from django.utils.translation import ugettext as _
|
||||
|
||||
from courseware import courses
|
||||
from student.models import get_user_by_username_or_email
|
||||
from .models import CourseUserGroup
|
||||
@@ -140,7 +141,7 @@ def get_cohorted_commentables(course_key):
|
||||
|
||||
def get_cohort(user, course_key):
|
||||
"""
|
||||
Given a django User and a CourseKey, return the user's cohort in that
|
||||
Given a Django user and a CourseKey, return the user's cohort in that
|
||||
cohort.
|
||||
|
||||
Arguments:
|
||||
@@ -180,7 +181,7 @@ def get_cohort(user, course_key):
|
||||
# Use the "default cohort".
|
||||
group_name = DEFAULT_COHORT_NAME
|
||||
|
||||
group, _ = CourseUserGroup.objects.get_or_create(
|
||||
group, __ = CourseUserGroup.objects.get_or_create(
|
||||
course_id=course_key,
|
||||
group_type=CourseUserGroup.COHORT,
|
||||
name=group_name
|
||||
@@ -250,7 +251,7 @@ def add_cohort(course_key, name):
|
||||
if CourseUserGroup.objects.filter(course_id=course_key,
|
||||
group_type=CourseUserGroup.COHORT,
|
||||
name=name).exists():
|
||||
raise ValueError("Can't create two cohorts with the same name")
|
||||
raise ValueError(_("You cannot create two cohorts with the same name"))
|
||||
|
||||
try:
|
||||
course = courses.get_course_by_id(course_key)
|
||||
@@ -296,9 +297,10 @@ def add_user_to_cohort(cohort, username_or_email):
|
||||
)
|
||||
if course_cohorts.exists():
|
||||
if course_cohorts[0] == cohort:
|
||||
raise ValueError("User {0} already present in cohort {1}".format(
|
||||
user.username,
|
||||
cohort.name))
|
||||
raise ValueError("User {user_name} already present in cohort {cohort_name}".format(
|
||||
user_name=user.username,
|
||||
cohort_name=cohort.name
|
||||
))
|
||||
else:
|
||||
previous_cohort = course_cohorts[0].name
|
||||
course_cohorts[0].users.remove(user)
|
||||
@@ -313,8 +315,9 @@ def delete_empty_cohort(course_key, name):
|
||||
"""
|
||||
cohort = get_cohort_by_name(course_key, name)
|
||||
if cohort.users.exists():
|
||||
raise ValueError(
|
||||
"Can't delete non-empty cohort {0} in course {1}".format(
|
||||
name, course_key))
|
||||
raise ValueError(_("You cannot delete non-empty cohort {cohort_name} in course {course_key}").format(
|
||||
cohort_name=name,
|
||||
course_key=course_key
|
||||
))
|
||||
|
||||
cohort.delete()
|
||||
|
||||
@@ -9,6 +9,9 @@ from xmodule.modulestore import ModuleStoreEnum
|
||||
|
||||
|
||||
class CohortFactory(DjangoModelFactory):
|
||||
"""
|
||||
Factory for constructing mock cohorts.
|
||||
"""
|
||||
FACTORY_FOR = CourseUserGroup
|
||||
|
||||
name = Sequence("cohort{}".format)
|
||||
@@ -17,6 +20,9 @@ class CohortFactory(DjangoModelFactory):
|
||||
|
||||
@post_generation
|
||||
def users(self, create, extracted, **kwargs): # pylint: disable=W0613
|
||||
"""
|
||||
Returns the users associated with the cohort.
|
||||
"""
|
||||
if extracted:
|
||||
self.users.add(*extracted)
|
||||
|
||||
|
||||
@@ -277,7 +277,7 @@ class AddCohortTestCase(CohortViewsTestCase):
|
||||
self.verify_contains_added_cohort(
|
||||
self.request_add_cohort(cohort_name, self.course),
|
||||
cohort_name,
|
||||
expected_error_msg="Can't create two cohorts with the same name"
|
||||
expected_error_msg="You cannot create two cohorts with the same name"
|
||||
)
|
||||
|
||||
|
||||
|
||||
@@ -37,7 +37,7 @@ class MembershipPage(PageObject):
|
||||
|
||||
def _get_cohort_options(self):
|
||||
"""
|
||||
Returns the available options in the cohort dropdown, including the initial "Select a cohort".
|
||||
Returns the available options in the cohort dropdown, including the initial "Select a cohort group".
|
||||
"""
|
||||
return self.q(css=".cohort-management #cohort-select option")
|
||||
|
||||
@@ -55,7 +55,7 @@ class MembershipPage(PageObject):
|
||||
|
||||
def get_cohorts(self):
|
||||
"""
|
||||
Returns, as a list, the names of the available cohorts in the drop-down, filtering out "Select a cohort".
|
||||
Returns, as a list, the names of the available cohorts in the drop-down, filtering out "Select a cohort group".
|
||||
"""
|
||||
return [
|
||||
self._cohort_name(opt.text)
|
||||
@@ -86,6 +86,15 @@ class MembershipPage(PageObject):
|
||||
lambda el: self._cohort_name(el.text) == cohort_name
|
||||
).first.click()
|
||||
|
||||
def add_cohort(self, cohort_name):
|
||||
"""
|
||||
Adds a new manual cohort with the specified name.
|
||||
"""
|
||||
self.q(css="div.cohort-management-nav .action-create").first.click()
|
||||
textinput = self.q(css="#cohort-create-name").results[0]
|
||||
textinput.send_keys(cohort_name)
|
||||
self.q(css="div.form-actions .action-save").first.click()
|
||||
|
||||
def get_cohort_group_setup(self):
|
||||
"""
|
||||
Returns the description of the current cohort
|
||||
|
||||
@@ -11,6 +11,8 @@ from ...pages.lms.auto_auth import AutoAuthPage
|
||||
from ...pages.lms.instructor_dashboard import InstructorDashboardPage
|
||||
from ...pages.studio.settings_advanced import AdvancedSettingsPage
|
||||
|
||||
import uuid
|
||||
|
||||
|
||||
class CohortConfigurationTest(UniqueCourseTest, CohortTestMixin):
|
||||
"""
|
||||
@@ -149,6 +151,30 @@ class CohortConfigurationTest(UniqueCourseTest, CohortTestMixin):
|
||||
self.assertEqual("There was an error when trying to add students:", error_messages[0])
|
||||
self.assertEqual("Unknown user: unknown_user", error_messages[1])
|
||||
self.assertEqual(
|
||||
self.student_name+",unknown_user,",
|
||||
self.student_name + ",unknown_user,",
|
||||
self.membership_page.get_cohort_student_input_field_value()
|
||||
)
|
||||
|
||||
def test_add_new_cohort(self):
|
||||
"""
|
||||
Scenario: A new manual cohort can be created, and a student assigned to it.
|
||||
|
||||
Given I have a course with a user in the course
|
||||
When I add a new manual cohort to the course via the LMS instructor dashboard
|
||||
Then the new cohort is displayed and has no users in it
|
||||
And when I add the user to the new cohort
|
||||
Then the cohort has 1 user
|
||||
"""
|
||||
new_cohort = str(uuid.uuid4().get_hex()[0:20])
|
||||
self.assertFalse(new_cohort in self.membership_page.get_cohorts())
|
||||
self.membership_page.add_cohort(new_cohort)
|
||||
# After adding the cohort, it should automatically be selected
|
||||
EmptyPromise(
|
||||
lambda: new_cohort == self.membership_page.get_selected_cohort(), "Waiting for new cohort to appear"
|
||||
).fulfill()
|
||||
self.assertEqual(0, self.membership_page.get_selected_cohort_count())
|
||||
self.membership_page.add_students_to_selected_cohort([self.instructor_name])
|
||||
# Wait for the number of users in the cohort to change, indicating that the add operation is complete.
|
||||
EmptyPromise(
|
||||
lambda: 1 == self.membership_page.get_selected_cohort_count(), 'Waiting for student to be added'
|
||||
).fulfill()
|
||||
|
||||
Reference in New Issue
Block a user