def test_update_and_fetch(self):
     SelfPacedConfiguration(enabled=True).save()
     jsondetails = CourseDetails.fetch(self.course.id)
     jsondetails.syllabus = "<a href='foo'>bar</a>"
     # encode - decode to convert date fields and other data which changes form
     with self.store.branch_setting(ModuleStoreEnum.Branch.draft_preferred, self.course.id):
         self.assertEqual(
             CourseDetails.update_from_json(self.course.id, jsondetails.__dict__, self.user).syllabus,
             jsondetails.syllabus, "After set syllabus"
         )
         jsondetails.short_description = "Short Description"
         self.assertEqual(
             CourseDetails.update_from_json(self.course.id, jsondetails.__dict__, self.user).short_description,
             jsondetails.short_description, "After set short_description"
         )
         jsondetails.overview = "Overview"
         self.assertEqual(
             CourseDetails.update_from_json(self.course.id, jsondetails.__dict__, self.user).overview,
             jsondetails.overview, "After set overview"
         )
         jsondetails.intro_video = "intro_video"
         self.assertEqual(
             CourseDetails.update_from_json(self.course.id, jsondetails.__dict__, self.user).intro_video,
             jsondetails.intro_video, "After set intro_video"
         )
         jsondetails.effort = "effort"
         self.assertEqual(
             CourseDetails.update_from_json(self.course.id, jsondetails.__dict__, self.user).effort,
             jsondetails.effort, "After set effort"
         )
         jsondetails.self_paced = True
         self.assertEqual(
             CourseDetails.update_from_json(self.course.id, jsondetails.__dict__, self.user).self_paced,
             jsondetails.self_paced
         )
         jsondetails.start_date = datetime.datetime(2010, 10, 1, 0, tzinfo=UTC())
         self.assertEqual(
             CourseDetails.update_from_json(self.course.id, jsondetails.__dict__, self.user).start_date,
             jsondetails.start_date
         )
         jsondetails.end_date = datetime.datetime(2011, 10, 1, 0, tzinfo=UTC())
         self.assertEqual(
             CourseDetails.update_from_json(self.course.id, jsondetails.__dict__, self.user).end_date,
             jsondetails.end_date
         )
         jsondetails.course_image_name = "an_image.jpg"
         self.assertEqual(
             CourseDetails.update_from_json(self.course.id, jsondetails.__dict__, self.user).course_image_name,
             jsondetails.course_image_name
         )
         jsondetails.language = "hr"
         self.assertEqual(
             CourseDetails.update_from_json(self.course.id, jsondetails.__dict__, self.user).language,
             jsondetails.language
         )
Beispiel #2
0
def _create_new_course(subject, org, number, run, fields):
    """
    Create a new course.
    Raises DuplicateCourseError if the course already exists
    """
    org_data = get_organization_by_short_name(org)
    if not org_data and organizations_enabled():
        return
    store_for_new_course = modulestore().default_modulestore.get_modulestore_type()
    try:
        user = User.objects.get(username='******')
    except User.DoesNotExist:
        user = User.objects.create(
            username='******',
            email='*****@*****.**',
            first_name='coursecreator',
            last_name='coursecreator',
            is_active=True,
            is_staff=True
        )
        user.set_password('coursecreator')
        user.save()
    try:
        new_course = create_new_course_in_store(store_for_new_course, user, org, number, run, fields)

    except DuplicateCourseError:
        existing_course_key = SlashSeparatedCourseKey.from_deprecated_string('course-v1:'+org+'+'+number+'+'+run)
        new_course = get_course_by_id(existing_course_key)

    add_organization_course(org_data, new_course.id)

    # Set description and images for the course
    course_image_name, course_image_asset_path = store_jacket_image(
        new_course.id, settings.VODECLIC_COURSE_IMAGE_LOCATION,
        subject.get("id") + ".png"
    )
    additional_info = {
        'display_name': subject.get('title'),
        'language': subject.get('language', 'fr'),
        'short_description': subject.get('description', ''),
        'intro_video': None,
        'course_image_name': course_image_name,
        'course_image_asset_path': course_image_asset_path,
        'start_date': new_course.start,
        'end_date': new_course.end,
        'enrollment_start': new_course.start,
        'enrollment_end': new_course.end
    }

    CourseDetails.update_from_json(new_course.id, additional_info, user)
    def test_toggle_pacing_during_course_run(self):
        self.course.start = datetime.datetime.now()
        self.store.update_item(self.course, self.user.id)

        details = CourseDetails.fetch(self.course.id)
        with self.store.branch_setting(ModuleStoreEnum.Branch.draft_preferred,
                                       self.course.id):
            updated_details = CourseDetails.update_from_json(
                self.course.id, dict(details.__dict__, self_paced=True),
                self.user)
        assert not updated_details.self_paced
    def test_toggle_pacing_during_course_run(self):
        SelfPacedConfiguration(enabled=True).save()
        self.course.start = datetime.datetime.now()
        self.store.update_item(self.course, self.user.id)

        details = CourseDetails.fetch(self.course.id)
        with self.store.branch_setting(ModuleStoreEnum.Branch.draft_preferred, self.course.id):
            updated_details = CourseDetails.update_from_json(
                self.course.id, dict(details.__dict__, self_paced=True), self.user
            )
        self.assertFalse(updated_details.self_paced)
    def test_update_and_fetch(self):
        jsondetails = CourseDetails.fetch(self.course.id)
        jsondetails.syllabus = "<a href='foo'>bar</a>"
        # encode - decode to convert date fields and other data which changes form
        with self.store.branch_setting(ModuleStoreEnum.Branch.draft_preferred,
                                       self.course.id):
            assert CourseDetails.update_from_json(self.course.id, jsondetails.__dict__, self.user).syllabus ==\
                   jsondetails.syllabus, 'After set syllabus'
            jsondetails.short_description = "Short Description"
            assert CourseDetails.update_from_json(self.course.id, jsondetails.__dict__, self.user).short_description ==\
                   jsondetails.short_description, 'After set short_description'
            jsondetails.overview = "Overview"
            assert CourseDetails.update_from_json(self.course.id, jsondetails.__dict__, self.user).overview ==\
                   jsondetails.overview, 'After set overview'
            jsondetails.intro_video = "intro_video"
            assert CourseDetails.update_from_json(self.course.id, jsondetails.__dict__, self.user).intro_video ==\
                   jsondetails.intro_video, 'After set intro_video'
            jsondetails.about_sidebar_html = "About Sidebar HTML"
            assert CourseDetails.update_from_json(self.course.id, jsondetails.__dict__, self.user)\
                .about_sidebar_html == jsondetails.about_sidebar_html, 'After set about_sidebar_html'
            jsondetails.effort = "effort"
            assert CourseDetails.update_from_json(self.course.id, jsondetails.__dict__, self.user).effort ==\
                   jsondetails.effort, 'After set effort'
            jsondetails.self_paced = True
            assert CourseDetails.update_from_json(self.course.id, jsondetails.__dict__, self.user).self_paced ==\
                   jsondetails.self_paced
            jsondetails.start_date = datetime.datetime(2010,
                                                       10,
                                                       1,
                                                       0,
                                                       tzinfo=UTC)
            assert CourseDetails.update_from_json(self.course.id, jsondetails.__dict__, self.user).start_date ==\
                   jsondetails.start_date
            jsondetails.end_date = datetime.datetime(2011,
                                                     10,
                                                     1,
                                                     0,
                                                     tzinfo=UTC)
            assert CourseDetails.update_from_json(self.course.id, jsondetails.__dict__, self.user).end_date ==\
                   jsondetails.end_date
            jsondetails.certificate_available_date = datetime.datetime(
                2010, 10, 1, 0, tzinfo=UTC)
            assert CourseDetails.update_from_json(self.course.id, jsondetails.__dict__, self.user)\
                .certificate_available_date == jsondetails.certificate_available_date
            jsondetails.course_image_name = "an_image.jpg"
            assert CourseDetails.update_from_json(self.course.id, jsondetails.__dict__, self.user).course_image_name ==\
                   jsondetails.course_image_name
            jsondetails.banner_image_name = "an_image.jpg"
            assert CourseDetails.update_from_json(self.course.id, jsondetails.__dict__, self.user).banner_image_name ==\
                   jsondetails.banner_image_name
            jsondetails.video_thumbnail_image_name = "an_image.jpg"
            assert CourseDetails.update_from_json(self.course.id, jsondetails.__dict__, self.user)\
                .video_thumbnail_image_name == jsondetails.video_thumbnail_image_name
            jsondetails.language = "hr"
            assert CourseDetails.update_from_json(self.course.id, jsondetails.__dict__, self.user).language ==\
                   jsondetails.language
            jsondetails.learning_info = ["test", "test"]
            assert CourseDetails.update_from_json(self.course.id, jsondetails.__dict__, self.user).learning_info ==\
                   jsondetails.learning_info

            jsondetails.instructor_info = {
                "instructors": [{
                    "name": "test",
                    "title": "test",
                    "organization": "test",
                    "image": "test",
                    "bio": "test"
                }]
            }
            assert CourseDetails.update_from_json(self.course.id, jsondetails.__dict__, self.user).instructor_info ==\
                   jsondetails.instructor_info
 def test_update_and_fetch(self):
     jsondetails = CourseDetails.fetch(self.course.id)
     jsondetails.syllabus = "<a href='foo'>bar</a>"
     # encode - decode to convert date fields and other data which changes form
     with self.store.branch_setting(ModuleStoreEnum.Branch.draft_preferred,
                                    self.course.id):
         self.assertEqual(
             CourseDetails.update_from_json(self.course.id,
                                            jsondetails.__dict__,
                                            self.user).syllabus,
             jsondetails.syllabus, "After set syllabus")
         jsondetails.short_description = "Short Description"
         self.assertEqual(
             CourseDetails.update_from_json(self.course.id,
                                            jsondetails.__dict__,
                                            self.user).short_description,
             jsondetails.short_description, "After set short_description")
         jsondetails.overview = "Overview"
         self.assertEqual(
             CourseDetails.update_from_json(self.course.id,
                                            jsondetails.__dict__,
                                            self.user).overview,
             jsondetails.overview, "After set overview")
         jsondetails.intro_video = "intro_video"
         self.assertEqual(
             CourseDetails.update_from_json(self.course.id,
                                            jsondetails.__dict__,
                                            self.user).intro_video,
             jsondetails.intro_video, "After set intro_video")
         jsondetails.effort = "effort"
         self.assertEqual(
             CourseDetails.update_from_json(self.course.id,
                                            jsondetails.__dict__,
                                            self.user).effort,
             jsondetails.effort, "After set effort")
         jsondetails.self_paced = True
         self.assertEqual(
             CourseDetails.update_from_json(self.course.id,
                                            jsondetails.__dict__,
                                            self.user).self_paced,
             jsondetails.self_paced)
         jsondetails.start_date = datetime.datetime(2010,
                                                    10,
                                                    1,
                                                    0,
                                                    tzinfo=UTC)
         self.assertEqual(
             CourseDetails.update_from_json(self.course.id,
                                            jsondetails.__dict__,
                                            self.user).start_date,
             jsondetails.start_date)
         jsondetails.end_date = datetime.datetime(2011,
                                                  10,
                                                  1,
                                                  0,
                                                  tzinfo=UTC)
         self.assertEqual(
             CourseDetails.update_from_json(self.course.id,
                                            jsondetails.__dict__,
                                            self.user).end_date,
             jsondetails.end_date)
         jsondetails.certificate_available_date = datetime.datetime(
             2010, 10, 1, 0, tzinfo=UTC)
         self.assertEqual(
             CourseDetails.update_from_json(
                 self.course.id, jsondetails.__dict__,
                 self.user).certificate_available_date,
             jsondetails.certificate_available_date)
         jsondetails.course_image_name = "an_image.jpg"
         self.assertEqual(
             CourseDetails.update_from_json(self.course.id,
                                            jsondetails.__dict__,
                                            self.user).course_image_name,
             jsondetails.course_image_name)
         jsondetails.banner_image_name = "an_image.jpg"
         self.assertEqual(
             CourseDetails.update_from_json(self.course.id,
                                            jsondetails.__dict__,
                                            self.user).banner_image_name,
             jsondetails.banner_image_name)
         jsondetails.video_thumbnail_image_name = "an_image.jpg"
         self.assertEqual(
             CourseDetails.update_from_json(
                 self.course.id, jsondetails.__dict__,
                 self.user).video_thumbnail_image_name,
             jsondetails.video_thumbnail_image_name)
         jsondetails.language = "hr"
         self.assertEqual(
             CourseDetails.update_from_json(self.course.id,
                                            jsondetails.__dict__,
                                            self.user).language,
             jsondetails.language)
         jsondetails.learning_info = ["test", "test"]
         self.assertEqual(
             CourseDetails.update_from_json(self.course.id,
                                            jsondetails.__dict__,
                                            self.user).learning_info,
             jsondetails.learning_info)
         jsondetails.instructor_info = {
             "instructors": [{
                 "name": "test",
                 "title": "test",
                 "organization": "test",
                 "image": "test",
                 "bio": "test"
             }]
         }
         self.assertEqual(
             CourseDetails.update_from_json(self.course.id,
                                            jsondetails.__dict__,
                                            self.user).instructor_info,
             jsondetails.instructor_info)
 def test_update_and_fetch(self):
     SelfPacedConfiguration(enabled=True).save()
     jsondetails = CourseDetails.fetch(self.course.id)
     jsondetails.syllabus = "<a href='foo'>bar</a>"
     # encode - decode to convert date fields and other data which changes form
     with self.store.branch_setting(ModuleStoreEnum.Branch.draft_preferred,
                                    self.course.id):
         self.assertEqual(
             CourseDetails.update_from_json(self.course.id,
                                            jsondetails.__dict__,
                                            self.user).syllabus,
             jsondetails.syllabus, "After set syllabus")
         jsondetails.short_description = "Short Description"
         self.assertEqual(
             CourseDetails.update_from_json(self.course.id,
                                            jsondetails.__dict__,
                                            self.user).short_description,
             jsondetails.short_description, "After set short_description")
         jsondetails.overview = "Overview"
         self.assertEqual(
             CourseDetails.update_from_json(self.course.id,
                                            jsondetails.__dict__,
                                            self.user).overview,
             jsondetails.overview, "After set overview")
         jsondetails.intro_video = "intro_video"
         self.assertEqual(
             CourseDetails.update_from_json(self.course.id,
                                            jsondetails.__dict__,
                                            self.user).intro_video,
             jsondetails.intro_video, "After set intro_video")
         jsondetails.effort = "effort"
         self.assertEqual(
             CourseDetails.update_from_json(self.course.id,
                                            jsondetails.__dict__,
                                            self.user).effort,
             jsondetails.effort, "After set effort")
         jsondetails.self_paced = True
         self.assertEqual(
             CourseDetails.update_from_json(self.course.id,
                                            jsondetails.__dict__,
                                            self.user).self_paced,
             jsondetails.self_paced)
         jsondetails.start_date = datetime.datetime(2010,
                                                    10,
                                                    1,
                                                    0,
                                                    tzinfo=UTC())
         self.assertEqual(
             CourseDetails.update_from_json(self.course.id,
                                            jsondetails.__dict__,
                                            self.user).start_date,
             jsondetails.start_date)
         jsondetails.end_date = datetime.datetime(2011,
                                                  10,
                                                  1,
                                                  0,
                                                  tzinfo=UTC())
         self.assertEqual(
             CourseDetails.update_from_json(self.course.id,
                                            jsondetails.__dict__,
                                            self.user).end_date,
             jsondetails.end_date)
         jsondetails.course_image_name = "an_image.jpg"
         self.assertEqual(
             CourseDetails.update_from_json(self.course.id,
                                            jsondetails.__dict__,
                                            self.user).course_image_name,
             jsondetails.course_image_name)
         jsondetails.language = "hr"
         self.assertEqual(
             CourseDetails.update_from_json(self.course.id,
                                            jsondetails.__dict__,
                                            self.user).language,
             jsondetails.language)
 def test_update_and_fetch(self):
     jsondetails = CourseDetails.fetch(self.course.id)
     jsondetails.syllabus = "<a href='foo'>bar</a>"
     # encode - decode to convert date fields and other data which changes form
     with self.store.branch_setting(ModuleStoreEnum.Branch.draft_preferred, self.course.id):
         self.assertEqual(
             CourseDetails.update_from_json(self.course.id, jsondetails.__dict__, self.user).syllabus,
             jsondetails.syllabus, "After set syllabus"
         )
         jsondetails.short_description = "Short Description"
         self.assertEqual(
             CourseDetails.update_from_json(self.course.id, jsondetails.__dict__, self.user).short_description,
             jsondetails.short_description, "After set short_description"
         )
         jsondetails.overview = "Overview"
         self.assertEqual(
             CourseDetails.update_from_json(self.course.id, jsondetails.__dict__, self.user).overview,
             jsondetails.overview, "After set overview"
         )
         jsondetails.intro_video = "intro_video"
         self.assertEqual(
             CourseDetails.update_from_json(self.course.id, jsondetails.__dict__, self.user).intro_video,
             jsondetails.intro_video, "After set intro_video"
         )
         jsondetails.effort = "effort"
         self.assertEqual(
             CourseDetails.update_from_json(self.course.id, jsondetails.__dict__, self.user).effort,
             jsondetails.effort, "After set effort"
         )
         jsondetails.self_paced = True
         self.assertEqual(
             CourseDetails.update_from_json(self.course.id, jsondetails.__dict__, self.user).self_paced,
             jsondetails.self_paced
         )
         jsondetails.start_date = datetime.datetime(2010, 10, 1, 0, tzinfo=UTC)
         self.assertEqual(
             CourseDetails.update_from_json(self.course.id, jsondetails.__dict__, self.user).start_date,
             jsondetails.start_date
         )
         jsondetails.end_date = datetime.datetime(2011, 10, 1, 0, tzinfo=UTC)
         self.assertEqual(
             CourseDetails.update_from_json(self.course.id, jsondetails.__dict__, self.user).end_date,
             jsondetails.end_date
         )
         jsondetails.certificate_available_date = datetime.datetime(2010, 10, 1, 0, tzinfo=UTC)
         self.assertEqual(
             CourseDetails.update_from_json(
                 self.course.id, jsondetails.__dict__, self.user
             ).certificate_available_date,
             jsondetails.certificate_available_date
         )
         jsondetails.course_image_name = "an_image.jpg"
         self.assertEqual(
             CourseDetails.update_from_json(self.course.id, jsondetails.__dict__, self.user).course_image_name,
             jsondetails.course_image_name
         )
         jsondetails.banner_image_name = "an_image.jpg"
         self.assertEqual(
             CourseDetails.update_from_json(self.course.id, jsondetails.__dict__, self.user).banner_image_name,
             jsondetails.banner_image_name
         )
         jsondetails.video_thumbnail_image_name = "an_image.jpg"
         self.assertEqual(
             CourseDetails.update_from_json(self.course.id, jsondetails.__dict__, self.user).video_thumbnail_image_name,
             jsondetails.video_thumbnail_image_name
         )
         jsondetails.language = "hr"
         self.assertEqual(
             CourseDetails.update_from_json(self.course.id, jsondetails.__dict__, self.user).language,
             jsondetails.language
         )
         jsondetails.learning_info = ["test", "test"]
         self.assertEqual(
             CourseDetails.update_from_json(self.course.id, jsondetails.__dict__, self.user).learning_info,
             jsondetails.learning_info
         )
         jsondetails.instructor_info = {
             "instructors": [
                 {
                     "name": "test",
                     "title": "test",
                     "organization": "test",
                     "image": "test",
                     "bio": "test"
                 }
             ]
         }
         self.assertEqual(
             CourseDetails.update_from_json(self.course.id, jsondetails.__dict__, self.user).instructor_info,
             jsondetails.instructor_info
         )
def create_or_update_course(request):
    """
        **Use Case**

            Create or edit course.

        **Example Requests**

            POST /api/extended/course/{
                "org": "test_org",
                "number": "test_course_num",
                "display_name": "TEST COURSE NAME",
                "run": "test_course_run",
                "start_date": "2016-09-01",
                "enrollment_start": "2016-08-15",
                "intro_video": "jsUdxcBsym0?list=PLWdgcBEz6133fTE9ePks31tT1QBLNaxFe",
                "syllabus": "123",
                "short_description": "456",
                "overview": "789",
                "effort": "40",
                "language": "ru",
                "course_modes": [
                    {
                        "mode": "honor",
                        "title": "test"
                    }
                ]
            }

        **Post Parameters**

            * org: Organization that owns course (slug)

            * number: Course slug

            * display_name: Course run display name for edX

            * run: Course run slug

            * start_date: Date when course starts

            * enrollment_start: Date when enrollment for course is opened

            * intro_video: Code of course introduction video on youtube (with player parameters)

            * syllabus: Course syllabus

            * short_description: Course short description

            * overview: Course overview

            * effort: Course effort (ni weeks)

            * language: Two-letter code of course language

            * course_modes: List of course modes.

                Course mode params:

                    * mode: Mode type ("audit", "honor" or "verified")

                    * price: Course mode price

                    * currency: Currency of course mode price

                    * title: Course mode title

                    * description: Course mode description

                    * upgrade_deadline: Last date when user can be enrolled/reenrolled to this mode

        **Response Values**

            * url: Course URL for CMS and LMS
            * course_key: The unique identifier for the course (full slug)
    """

    global_stuff = User.objects.filter(is_staff=True).first()
    if global_stuff is not None:
        request.user = global_stuff
    else:
        raise PermissionDenied()

    course_key = modulestore().make_course_key(request.json["org"],
                                               request.json["number"],
                                               request.json["run"])
    with modulestore().bulk_operations(course_key):
        course_key = modulestore().has_course(course_key)
        if course_key is None:
            response = _create_or_rerun_course(request)
            if response.status_code >= 400:
                return response
            course_key_string = json.loads(response.content).get("course_key")
            if course_key_string is not None:
                course_key = CourseKey.from_string(course_key_string)
            else:
                return response
        course_data = request.json.copy()
        if course_data["start_date"] is None:
            course_data["start_date"] = format(DEFAULT_START_DATE, "%Y-%m-%d")
        course_data["end_date"] = format(DEFAULT_START_DATE, "%Y-%m-%d")
        course_data["enrollment_end"] = format(DEFAULT_START_DATE, "%Y-%m-%d")
        CourseDetails.update_from_json(course_key, course_data, global_stuff)
        set_course_cohort_settings(course_key, is_cohorted=True)
        modes = request.json.get("course_modes", [])
        CourseMode.objects.filter(course_id=course_key).exclude(
            mode_slug__in=[mode["mode"] for mode in modes]).delete()
        for mode in modes:
            mode_params = {"course_id": course_key, "mode_slug": mode["mode"]}
            if "price" in mode:
                mode_params["min_price"] = mode["price"]
            if "currency" in mode:
                mode_params["currency"] = mode["currency"]
            if "title" in mode:
                mode_params["mode_display_name"] = mode["title"]
            if "description" in mode:
                mode_params["description"] = mode["description"]
            if "upgrade_deadline" in mode:
                mode_params["_expiration_datetime"] = mode["upgrade_deadline"]
            CourseMode.objects.update_or_create(course_id=course_key,
                                                mode_slug=mode["mode"],
                                                defaults=mode_params)
        return JsonResponse({
            'url':
            reverse_course_url('course_handler', course_key),
            'course_key':
            unicode(course_key)
        })