def test_notifications_handler_dismiss(self): state = CourseRerunUIStateManager.State.FAILED should_display = True rerun_course_key = CourseLocator(org='testx', course='test_course', run='test_run') # add an instructor to this course user2 = UserFactory() add_instructor(rerun_course_key, self.user, user2) # create a test notification rerun_state = CourseRerunState.objects.update_state( course_key=rerun_course_key, new_state=state, allow_not_found=True) CourseRerunState.objects.update_should_display( entry_id=rerun_state.id, user=user2, should_display=should_display) # try to get information on this notification notification_dismiss_url = reverse_course_url( 'course_notifications_handler', self.course.id, kwargs={ 'action_state_id': rerun_state.id, }) resp = self.client.delete(notification_dismiss_url) self.assertEquals(resp.status_code, 200) with self.assertRaises(CourseRerunState.DoesNotExist): # delete nofications that are dismissed CourseRerunState.objects.get(id=rerun_state.id) self.assertFalse(has_course_author_access(user2, rerun_course_key))
def _create_new_course(request, course_key, fields): """ Create a new course. Returns the URL for the course overview page. """ # Set a unique wiki_slug for newly created courses. To maintain active wiki_slugs for # existing xml courses this cannot be changed in CourseDescriptor. # # TODO get rid of defining wiki slug in this org/course/run specific way and reconcile # w/ xmodule.course_module.CourseDescriptor.__init__ wiki_slug = u"{0}.{1}.{2}".format(course_key.org, course_key.course, course_key.run) definition_data = {'wiki_slug': wiki_slug} fields.update(definition_data) # Creating the course raises InvalidLocationError if an existing course with this org/name is found new_course = modulestore().create_course( course_key.org, course_key.course, course_key.run, request.user.id, fields=fields, ) # Make sure user has instructor and staff access to the new course add_instructor(new_course.id, request.user, request.user) # Initialize permissions for user in the new course initialize_permissions(new_course.id, request.user) return JsonResponse({ 'url': reverse_course_url('course_handler', new_course.id) })
def test_notifications_handler_dismiss(self): state = CourseRerunUIStateManager.State.FAILED should_display = True rerun_course_key = CourseLocator(org='testx', course='test_course', run='test_run') # add an instructor to this course user2 = UserFactory() add_instructor(rerun_course_key, self.user, user2) # create a test notification rerun_state = CourseRerunState.objects.update_state( course_key=rerun_course_key, new_state=state, allow_not_found=True ) CourseRerunState.objects.update_should_display( entry_id=rerun_state.id, user=user2, should_display=should_display ) # try to get information on this notification notification_dismiss_url = reverse_course_url('course_notifications_handler', self.course.id, kwargs={ 'action_state_id': rerun_state.id, }) resp = self.client.delete(notification_dismiss_url) self.assertEquals(resp.status_code, 200) with self.assertRaises(CourseRerunState.DoesNotExist): # delete nofications that are dismissed CourseRerunState.objects.get(id=rerun_state.id) self.assertFalse(has_course_author_access(user2, rerun_course_key))
def _create_library(request): """ Helper method for creating a new library. """ if not auth.has_access(request.user, CourseCreatorRole()): log.exception(u"User %s tried to create a library without permission", request.user.username) raise PermissionDenied() display_name = None try: display_name = request.json['display_name'] org = request.json['org'] library = request.json.get('number', None) if library is None: library = request.json['library'] store = modulestore() with store.default_store(ModuleStoreEnum.Type.split): new_lib = store.create_library( org=org, library=library, user_id=request.user.id, fields={"display_name": display_name}, ) # Give the user admin ("Instructor") role for this library: add_instructor(new_lib.location.library_key, request.user, request.user) except KeyError as error: log.exception("Unable to create library - missing required JSON key.") return JsonResponseBadRequest({ "ErrMsg": _("Unable to create library - missing required field '{field}'". format(field=error.message)) }) except InvalidKeyError as error: log.exception("Unable to create library - invalid key.") return JsonResponseBadRequest({ "ErrMsg": _("Unable to create library '{name}'.\n\n{err}").format( name=display_name, err=error.message) }) except DuplicateCourseError: log.exception( "Unable to create library - one already exists with the same key.") return JsonResponseBadRequest({ 'ErrMsg': _('There is already a library defined with the same ' 'organization and library code. Please ' 'change your library code so that it is unique within your organization.' ) }) lib_key_str = unicode(new_lib.location.library_key) return JsonResponse({ 'url': reverse_library_url('library_handler', lib_key_str), 'library_key': lib_key_str, })
def _create_library(request): """ Helper method for creating a new library. """ display_name = None try: display_name = request.json['display_name'] org = request.json['org'] library = request.json.get('number', None) # [COLARAZ_CUSTOM] if not request.user.is_superuser and not org in configuration_helpers.get_value('course_org_filter'): log.exception('Unable to create library - invalid organization ({}).'.format(org)) return JsonResponseBadRequest({ "ErrMsg": _("Unable to create library '{}'.\n\n{} is not a valid organization.").format(display_name, org) }) if library is None: library = request.json['library'] store = modulestore() with store.default_store(ModuleStoreEnum.Type.split): new_lib = store.create_library( org=org, library=library, user_id=request.user.id, fields={"display_name": display_name}, ) # Give the user admin ("Instructor") role for this library: add_instructor(new_lib.location.library_key, request.user, request.user) except KeyError as error: log.exception("Unable to create library - missing required JSON key.") return JsonResponseBadRequest({ "ErrMsg": _("Unable to create library - missing required field '{field}'").format(field=text_type(error)) }) except InvalidKeyError as error: log.exception("Unable to create library - invalid key.") return JsonResponseBadRequest({ "ErrMsg": _("Unable to create library '{name}'.\n\n{err}").format(name=display_name, err=text_type(error)) }) except DuplicateCourseError: log.exception("Unable to create library - one already exists with the same key.") return JsonResponseBadRequest({ 'ErrMsg': _( 'There is already a library defined with the same ' 'organization and library code. Please ' 'change your library code so that it is unique within your organization.' ) }) lib_key_str = unicode(new_lib.location.library_key) return JsonResponse({ 'url': reverse_library_url('library_handler', lib_key_str), 'library_key': lib_key_str, })
def _create_library(request): """ Helper method for creating a new library. """ if not auth.has_access(request.user, CourseCreatorRole()): log.exception(u"User %s tried to create a library without permission", request.user.username) raise PermissionDenied() display_name = None try: display_name = request.json['display_name'] org = request.json['org'] library = request.json.get('number', None) if library is None: library = request.json['library'] store = modulestore() with store.default_store(ModuleStoreEnum.Type.split): new_lib = store.create_library( org=org, library=library, user_id=request.user.id, fields={"display_name": display_name}, ) # Give the user admin ("Instructor") role for this library: add_instructor(new_lib.location.library_key, request.user, request.user) except KeyError as error: log.exception("Unable to create library - missing required JSON key.") return JsonResponseBadRequest({ "ErrMsg": _("Unable to create library - missing required field '{field}'".format(field=error.message)) }) except InvalidKeyError as error: log.exception("Unable to create library - invalid key.") return JsonResponseBadRequest({ "ErrMsg": _("Unable to create library '{name}'.\n\n{err}").format(name=display_name, err=error.message) }) except DuplicateCourseError: log.exception("Unable to create library - one already exists with the same key.") return JsonResponseBadRequest({ 'ErrMsg': _( 'There is already a library defined with the same ' 'organization and library code. Please ' 'change your library code so that it is unique within your organization.' ) }) lib_key_str = unicode(new_lib.location.library_key) return JsonResponse({ 'url': reverse_library_url('library_handler', lib_key_str), 'library_key': lib_key_str, })
def get_master_course(user=None, command=None): from contentstore.utils import add_instructor, initialize_permissions from courseware.courses import get_course_by_id from opaque_keys.edx.locations import SlashSeparatedCourseKey from student.roles import CourseRole from xmodule.modulestore.exceptions import InvalidLocationError if not user: user = User.objects.get(id=ADMIN_USER_ID) display_name = "LabsterX Master" org, number, run = COURSE_ID.split('/') try: course_key = SlashSeparatedCourseKey(org, number, run) fields = {'display_name': display_name} wiki_slug = u"{0}.{1}.{2}".format(course_key.org, course_key.course, course_key.run) definition_data = {'wiki_slug': wiki_slug} fields.update(definition_data) if CourseRole.course_group_already_exists(course_key): raise InvalidLocationError() course = get_modulestore().create_course( course_key.org, course_key.course, course_key.run, user.id, fields=fields, ) # Make sure user has instructor and staff access to the new course add_instructor(course.id, user, user) # Initialize permissions for user in the new course initialize_permissions(course.id, user) command and command.stdout.write("name: {}\n".format(course.display_name)) except InvalidLocationError: course = get_course_by_id(course_key) return course
def _rerun_course(request, destination_course_key, fields): """ Reruns an existing course. Returns the URL for the course listing page. """ source_course_key = CourseKey.from_string(request.json.get('source_course_key')) # verify user has access to the original course if not has_course_access(request.user, source_course_key): raise PermissionDenied() # Make sure user has instructor and staff access to the destination course # so the user can see the updated status for that course add_instructor(destination_course_key, request.user, request.user) # Mark the action as initiated CourseRerunState.objects.initiated(source_course_key, destination_course_key, request.user) # Rerun the course as a new celery task rerun_course.delay(source_course_key, destination_course_key, request.user.id, fields) # Return course listing page return JsonResponse({'url': reverse_url('course_handler')})
def get_or_create_course(source, target, user): source_course = get_course_by_id(SlashSeparatedCourseKey.from_deprecated_string(source)) display_name = source_course.display_name org, number, run = target.split('/') course_key = SlashSeparatedCourseKey(org, number, run) fields = {'display_name': display_name} wiki_slug = u"{0}.{1}.{2}".format(course_key.org, course_key.course, course_key.run) definition_data = {'wiki_slug': wiki_slug} fields.update(definition_data) try: if CourseRole.course_group_already_exists(course_key): raise InvalidLocationError() course = modulestore().create_course( course_key.org, course_key.course, course_key.run, user.id, fields=fields, ) except InvalidLocationError: course = get_course(course_key) else: # Make sure user has instructor and staff access to the new course add_instructor(course.id, user, user) # Initialize permissions for user in the new course initialize_permissions(course.id, user) return course
def force_create_course(source, target, user, extra_fields=None): source_course_key = SlashSeparatedCourseKey.from_deprecated_string(source) source_course = get_course_by_id(source_course_key) display_name = source_course.display_name fields = {'display_name': display_name} # duplicated_fields = [ # 'key_dates', 'video', # 'course_staff_short', 'course_staff_extended', # 'requirements', 'textbook', 'faq', 'more_info', # 'number', 'instructors', 'end_date', # 'prerequisites', 'ocw_links', # ] # for field in duplicated_fields: # fields[field] = getattr(source_course, field) course = None start_index = 0 org, original_number, run = target.split('/') number = original_number while course is None: course_key = SlashSeparatedCourseKey(org, number, run) wiki_slug = u"{0}.{1}.{2}".format(course_key.org, course_key.course, course_key.run) definition_data = {'wiki_slug': wiki_slug} fields.update(definition_data) if extra_fields: fields.update(extra_fields) try: if CourseRole.course_group_already_exists(course_key): raise InvalidLocationError() course = modulestore().create_course( course_key.org, course_key.course, course_key.run, user.id, fields=fields, ) except InvalidLocationError: start_index += 1 number = "{}{}".format(original_number, start_index) continue else: # Make sure user has instructor and staff access to the new course add_instructor(course.id, user, user) # Initialize permissions for user in the new course initialize_permissions(course.id, user) copy_about_fields(user, source_course_key, course_key, course) UserProfile.objects\ .filter(user_id=user.id)\ .exclude(user_type=UserProfile.USER_TYPE_TEACHER)\ .update(user_type=UserProfile.USER_TYPE_TEACHER) return course