def _configure_course(self):
        """
        Configure course settings (e.g. start and end date)
        """
        url = STUDIO_BASE_URL + '/settings/details/' + self._course_key

        # First, get the current values
        response = self.session.get(url, headers=self.headers)

        if not response.ok:
            raise FixtureError(
                "Could not retrieve course details.  Status was {}".format(
                    response.status_code))

        try:
            details = response.json()
        except ValueError:
            raise FixtureError(  # lint-amnesty, pylint: disable=raise-missing-from
                f"Could not decode course details as JSON: '{details}'")

        # Update the old details with our overrides
        details.update(self._course_details)

        # POST the updated details to Studio
        response = self.session.post(
            url,
            data=self._encode_post_dict(details),
            headers=self.headers,
        )

        if not response.ok:
            raise FixtureError(
                "Could not update course details to '{}' with {}: Status was {}."
                .format(self._course_details, url, response.status_code))
    def _create_course(self):
        """
        Create the course described in the fixture.
        """
        # If the course already exists, this will respond
        # with a 200 and an error message, which we ignore.
        response = self.session.post(STUDIO_BASE_URL + '/course/',
                                     data=self._encode_post_dict(
                                         self._course_dict),
                                     headers=self.headers)

        try:
            err = response.json().get('ErrMsg')

        except ValueError:
            raise FixtureError(  # lint-amnesty, pylint: disable=raise-missing-from
                "Could not parse response from course request as JSON: '{}'".
                format(response.content))

        # This will occur if the course identifier is not unique
        if err is not None:
            raise FixtureError(
                f"Could not create course {self}.  Error message: '{err}'")

        if response.ok:
            self._course_key = response.json()['course_key']
        else:
            raise FixtureError(
                "Could not create course {}.  Status was {}\nResponse content was: {}"
                .format(self._course_dict, response.status_code,
                        response.content))
    def _upload_assets(self):
        """
        Upload assets
        :raise FixtureError:
        """
        url = STUDIO_BASE_URL + self._assets_url

        test_dir = Path(__file__).abspath().dirname().dirname().dirname()

        for asset_name in self._assets:
            asset_file_path = test_dir + '/data/uploads/' + asset_name

            asset_file = open(asset_file_path, mode='rb')  # lint-amnesty, pylint: disable=consider-using-with
            files = {
                'file': (asset_name, asset_file,
                         mimetypes.guess_type(asset_file_path)[0])
            }

            headers = {
                'Accept': 'application/json',
                'X-CSRFToken': self.session_cookies.get('csrftoken', '')
            }

            upload_response = self.session.post(url,
                                                files=files,
                                                headers=headers)

            if not upload_response.ok:
                raise FixtureError(
                    'Could not upload {asset_name} with {url}. Status code: {code}'
                    .format(asset_name=asset_name,
                            url=url,
                            code=upload_response.status_code))
    def _install_course_handouts(self):
        """
        Add handouts to the course info page.
        """
        url = STUDIO_BASE_URL + '/xblock/' + self._handouts_loc

        # Construct HTML with each of the handout links
        handouts_li = [
            f'<li><a href="/static/{handout}">Example Handout</a></li>'
            for handout in self._handouts
        ]
        handouts_html = '<ol class="treeview-handoutsnav">{}</ol>'.format(
            "".join(handouts_li))

        # Update the course's handouts HTML
        payload = json.dumps({
            'children': None,
            'data': handouts_html,
            'id': self._handouts_loc,
            'metadata': dict(),
        })

        response = self.session.post(url, data=payload, headers=self.headers)

        if not response.ok:
            raise FixtureError(
                f"Could not update course handouts with {url}.  Status was {response.status_code}"
            )
    def studio_course_outline_as_json(self):
        """
        Retrieves Studio course outline in JSON format.
        """
        url = STUDIO_BASE_URL + '/course/' + self._course_key + "?format=json"
        response = self.session.get(url, headers=self.headers)

        if not response.ok:
            raise FixtureError(
                "Could not retrieve course outline json.  Status was {}".
                format(response.status_code))

        try:
            course_outline_json = response.json()
        except ValueError:
            raise FixtureError(  # lint-amnesty, pylint: disable=raise-missing-from
                f"Could not decode course outline as JSON: '{response}'")
        return course_outline_json
Esempio n. 6
0
    def course_outline(self):
        """
        Retrieves course outline in JSON format.
        """
        url = STUDIO_BASE_URL + '/course/' + self._course_key + "?format=json"
        response = self.session.get(url, headers=self.headers)

        if not response.ok:
            raise FixtureError(
                "Could not retrieve course outline json.  Status was {0}".format(
                    response.status_code))

        try:
            course_outline_json = response.json()
        except ValueError:
            raise FixtureError(
                "Could not decode course outline as JSON: '{0}'".format(response)
            )
        return course_outline_json
    def _install_course_textbooks(self):
        """
        Add textbooks to the course, if any are configured.
        """
        url = STUDIO_BASE_URL + '/textbooks/' + self._course_key

        for book in self._textbooks:
            payload = json.dumps(book)
            response = self.session.post(url, headers=self.headers, data=payload)

            if not response.ok:
                raise FixtureError(
                    "Could not add book to course: {} with {}.  Status was {}".format(
                        book, url, response.status_code))
    def _add_advanced_settings(self):
        """
        Add advanced settings.
        """
        url = STUDIO_BASE_URL + "/settings/advanced/" + self._course_key

        # POST advanced settings to Studio
        response = self.session.post(
            url, data=self._encode_post_dict(self._advanced_settings),
            headers=self.headers,
        )

        if not response.ok:
            raise FixtureError(
                "Could not update advanced details to '{}' with {}: Status was {}.".format(
                    self._advanced_settings, url, response.status_code))
    def _install_course_updates(self):
        """
        Add updates to the course, if any are configured.
        """
        url = STUDIO_BASE_URL + '/course_info_update/' + self._course_key + '/'

        for update in self._updates:

            # Add the update to the course
            date, content = update
            payload = json.dumps({'date': date, 'content': content})
            response = self.session.post(url, headers=self.headers, data=payload)

            if not response.ok:
                raise FixtureError(
                    "Could not add update to course: {} with {}.  Status was {}".format(
                        update, url, response.status_code))
Esempio n. 10
0
    def _create_library(self):
        """
        Create the library described in the fixture.
        Will fail if the library already exists.
        """
        response = self.session.post(STUDIO_BASE_URL + '/library/',
                                     data=self._encode_post_dict(
                                         self.library_info),
                                     headers=self.headers)

        if response.ok:
            self._library_key = response.json()['library_key']
        else:
            try:
                err_msg = response.json().get('ErrMsg')
            except ValueError:
                err_msg = "Unknown Error"
            raise FixtureError(
                "Could not create library {}. Status was {}, error was: {}".
                format(self.library_info, response.status_code, err_msg))