Beispiel #1
0
def has_ccx_coach_role(user, course_key):
    """
    Check if user is a coach on this ccx.

    Arguments:
        user (User): the user whose descriptor access we are checking.
        course_key (CCXLocator): Key to CCX.

    Returns:
        bool: whether user is a coach on this ccx or not.
    """
    if hasattr(course_key, 'ccx'):
        ccx_id = course_key.ccx
        role = CourseCcxCoachRole(course_key)

        if role.has_user(user):
            list_ccx = CustomCourseForEdX.objects.filter(
                course_id=course_key.to_course_locator(), coach=user)
            if list_ccx.exists():
                coach_ccx = list_ccx[0]
                return str(coach_ccx.id) == ccx_id
    else:
        raise CCXLocatorValidationException(
            "Invalid CCX key. To verify that "
            "user is a coach on CCX, you must provide key to CCX")
    return False
Beispiel #2
0
 def test_post_list(self):
     """
     Test the creation of a CCX
     """
     outbox = self.get_outbox()
     data = {
         'master_course_id': self.master_course_key_str,
         'max_students_allowed': 111,
         'display_name': 'CCX Test Title',
         'coach_email': self.coach.email,
         'course_modules': self.master_course_chapters[0:1]
     }
     resp = self.client.post(self.list_url, data, format='json', HTTP_AUTHORIZATION=self.auth)
     assert resp.status_code == status.HTTP_201_CREATED
     # check if the response has at least the same data of the request
     for key, val in data.items():
         assert resp.data.get(key) == val
     assert 'ccx_course_id' in resp.data
     # check that the new CCX actually exists
     course_key = CourseKey.from_string(resp.data.get('ccx_course_id'))
     ccx_course = CustomCourseForEdX.objects.get(pk=course_key.ccx)
     assert str(CCXLocator.from_course_locator(ccx_course.course.id, ccx_course.id)) ==\
            resp.data.get('ccx_course_id')
     # check that the coach user has coach role on the master course
     coach_role_on_master_course = CourseCcxCoachRole(self.master_course_key)
     assert coach_role_on_master_course.has_user(self.coach)
     # check that the coach has been enrolled in the ccx
     ccx_course_object = get_course_by_id(course_key)
     assert CourseEnrollment.objects.filter(course_id=ccx_course_object.id, user=self.coach).exists()
     # check that an email has been sent to the coach
     assert len(outbox) == 1
     assert self.coach.email in outbox[0].recipients()
Beispiel #3
0
 def test_patch_detail(self):
     """
     Test for successful patch
     """
     outbox = self.get_outbox()
     # create a new coach
     new_coach = AdminFactory.create()
     data = {
         'max_students_allowed': 111,
         'display_name': 'CCX Title',
         'coach_email': new_coach.email
     }
     resp = self.client.patch(self.detail_url, data, format='json', HTTP_AUTHORIZATION=self.auth)
     assert resp.status_code == status.HTTP_204_NO_CONTENT
     ccx_from_db = CustomCourseForEdX.objects.get(id=self.ccx.id)
     assert ccx_from_db.max_student_enrollments_allowed == data['max_students_allowed']
     assert ccx_from_db.display_name == data['display_name']
     assert ccx_from_db.coach.email == data['coach_email']
     # check that the coach user has coach role on the master course
     coach_role_on_master_course = CourseCcxCoachRole(self.master_course_key)
     assert coach_role_on_master_course.has_user(new_coach)
     # check that the coach has been enrolled in the ccx
     ccx_course_object = get_course_by_id(self.ccx_key)
     assert CourseEnrollment.objects.filter(course_id=ccx_course_object.id, user=new_coach).exists()
     # check that an email has been sent to the coach
     assert len(outbox) == 1
     assert new_coach.email in outbox[0].recipients()
Beispiel #4
0
    def is_enabled(cls, course, user=None):
        """
        Returns true if CCX has been enabled and the specified user is a coach
        """
        if not settings.FEATURES.get('CUSTOM_COURSES_EDX', False) or not course.enable_ccx:
            # If ccx is not enable do not show ccx coach tab.
            return False

        if hasattr(course.id, 'ccx') and bool(user.has_perm(VIEW_CCX_COACH_DASHBOARD, course)):
            return True

        # check if user has coach access.
        role = CourseCcxCoachRole(course.id)
        return role.has_user(user)
Beispiel #5
0
def assign_staff_role_to_ccx(ccx_locator, user, master_course_id):
    """
    Check if user has ccx_coach role on master course then assign them staff role on ccx only
    if role is not already assigned. Because of this coach can open dashboard from master course
    as well as ccx.
    :param ccx_locator: CCX key
    :param user: User to whom we want to assign role.
    :param master_course_id: Master course key
    """
    coach_role_on_master_course = CourseCcxCoachRole(master_course_id)
    # check if user has coach role on master course
    if coach_role_on_master_course.has_user(user):
        # Check if user has staff role on ccx.
        role = CourseStaffRole(ccx_locator)
        if not role.has_user(user):
            # assign user the staff role on ccx
            with ccx_course(ccx_locator) as course:
                allow_access(course, user, "staff", send_email=False)
    def wrapper(request, course_id):
        """
        Wraps the view function, performing access check, loading the course,
        and modifying the view's call signature.
        """
        course_key = CourseKey.from_string(course_id)
        ccx = None
        if isinstance(course_key, CCXLocator):
            ccx_id = course_key.ccx
            try:
                ccx = CustomCourseForEdX.objects.get(pk=ccx_id)
            except CustomCourseForEdX.DoesNotExist:
                raise Http404  # lint-amnesty, pylint: disable=raise-missing-from

        if ccx:
            course_key = ccx.course_id
        course = get_course_by_id(course_key, depth=None)

        if not course.enable_ccx:  # lint-amnesty, pylint: disable=no-else-raise
            raise Http404
        else:
            if bool(request.user.has_perm(VIEW_CCX_COACH_DASHBOARD, course)):
                # if user is staff or instructor then he can view ccx coach dashboard.
                return view(request, course, ccx)
            else:
                # if there is a ccx, we must validate that it is the ccx for this coach
                role = CourseCcxCoachRole(course_key)
                if not role.has_user(request.user):
                    return HttpResponseForbidden(
                        _('You must be a CCX Coach to access this view.'))
                elif ccx is not None:
                    coach_ccx = get_ccx_by_ccx_id(course, request.user, ccx.id)
                    if coach_ccx is None:
                        return HttpResponseForbidden(
                            _('You must be the coach for this ccx to access this view'
                              ))

        return view(request, course, ccx)