Exemplo n.º 1
0
    def set_assessment_content(self, unit, assessment_content, errors=None):
        """Updates the content of an assessment."""
        if errors is None:
            errors = []

        path = self._app_context.fs.impl.physical_to_logical(
            self.get_assessment_filename(unit.unit_id))
        root_name = 'assessment'

        try:
            content, noverify_text = verify.convert_javascript_to_python(
                assessment_content, root_name)
            assessment = verify.evaluate_python_expression_from_text(
                content, root_name, verify.Assessment().scope, noverify_text)
        except Exception:  # pylint: disable-msg=broad-except
            errors.append('Unable to parse %s:\n%s' % (
                root_name,
                str(sys.exc_info()[1])))
            return

        verifier = verify.Verifier()
        try:
            verifier.verify_assessment_instance(assessment, path)
        except verify.SchemaException:
            errors.append('Error validating %s\n' % root_name)
            return

        fs = self.app_context.fs
        fs.put(
            path, vfs.string_to_stream(assessment_content),
            is_draft=not unit.now_available)
Exemplo n.º 2
0
    def set_assessment_content(self, unit, assessment_content, errors=None):
        """Updates the content of an assessment."""
        if errors is None:
            errors = []

        path = self._app_context.fs.impl.physical_to_logical(
            self.get_assessment_filename(unit.unit_id))
        root_name = 'assessment'

        try:
            content, noverify_text = verify.convert_javascript_to_python(
                assessment_content, root_name)
            assessment = verify.evaluate_python_expression_from_text(
                content, root_name, verify.Assessment().scope, noverify_text)
        except Exception:  # pylint: disable-msg=broad-except
            errors.append('Unable to parse %s:\n%s' % (
                root_name,
                str(sys.exc_info()[1])))
            return

        verifier = verify.Verifier()
        try:
            verifier.verify_assessment_instance(assessment, path)
        except verify.SchemaException:
            errors.append('Error validating %s\n' % root_name)
            return

        fs = self.app_context.fs
        fs.put(
            path, vfs.string_to_stream(assessment_content),
            is_draft=not unit.now_available)
Exemplo n.º 3
0
    def init_new_course_settings(self, title, admin_email):
        """Initializes new course.yaml file if it does not yet exists."""
        fs = self.app_context.fs.impl
        course_yaml = fs.physical_to_logical('/course.yaml')
        if fs.isfile(course_yaml):
            return False

        title = title.replace('\'', '\'\'')
        course_yaml_text = u"""# my new course.yaml
course:
  title: '%s'
  admin_user_emails: '[%s]'
  now_available: False
""" % (title, admin_email)

        fs.put(course_yaml, vfs.string_to_stream(course_yaml_text))
        return True
Exemplo n.º 4
0
    def init_new_course_settings(self, title, admin_email):
        """Initializes new course.yaml file if it does not yet exists."""
        fs = self.app_context.fs.impl
        course_yaml = fs.physical_to_logical('/course.yaml')
        if fs.isfile(course_yaml):
            return False

        title = title.replace('\'', '\'\'')
        course_yaml_text = u"""# my new course.yaml
course:
  title: '%s'
  admin_user_emails: '[%s]'
  now_available: False
""" % (title, admin_email)

        fs.put(course_yaml, vfs.string_to_stream(course_yaml_text))
        return True
Exemplo n.º 5
0
    def save_settings(cls, course, course_settings):
        if 'reg_form' in course_settings:
            if 'registration' in course_settings['reg_form']:
                course_settings['reg_form']['can_register'] = (
                    course_settings['reg_form']['registration'] == 'OPEN')

        content = yaml.safe_dump(course_settings)
        try:
            cls.validate_settings_content(content)
        except yaml.YAMLError as e:  # pylint: disable=W0703
            logging.error('Failed to validate course settings: %s.', str(e))
            return False

        content_stream = vfs.string_to_stream(unicode(content))
        # Store settings.
        course.set_file_content('/course.yaml', content_stream)

        course_item = course_settings['course']
        reg_item = dict()
        if 'reg_form' in course_settings:
            reg_item = course_settings['reg_form']
        explorer_item = dict()
        if 'explorer' in course_settings:
            explorer_item = course_settings['explorer']

        CourseListDAO.update_course_details(
            namespace_manager.get_namespace(),
            title=course_item.get('title'),
            browsable=course_item.get('browsable'),
            visible=course_item.get('now_available'),
            allowlist=course_item.get('allowlist'),
            can_register=reg_item.get('can_register'),
            registration=reg_item.get('registration'),
            blurb=explorer_item.get('blurb'),
            should_list=explorer_item.get('list_in_explorer'),
            closed=explorer_item.get('closed', False),
            course_admin_email=course_item.get('admin_user_emails'))
        course.invalidate_cached_course_settings()
        return True