def get_schema(self): registration = schema_fields.FieldRegistry('registration') registration.add_property( schema_fields.SchemaField('main_module', 'Main Module', 'string', optional=True)) registration.add_property( schema_fields.SchemaField('enabled', 'Is Enabled', 'bool', optional=True, default_value=True)) registration.add_property( schema_fields.SchemaField('enabled_for_tests', 'Is Enabled When Running Tests', 'bool', optional=True, default_value=False)) self.files = schema_fields.FieldArray( 'files', 'Module files', item_type=schema_fields.SchemaField('filename', 'Filename', 'string')) self.unit = schema_fields.FieldArray( 'unit', 'Unit test classes', item_type=schema_fields.SchemaField( 'entry', 'module.module.ClassName = test_count', 'string')) self.functional = schema_fields.FieldArray( 'functional', 'Functional test classes', item_type=schema_fields.SchemaField( 'entry', 'module.module.ClassName = test_count', 'string')) self.integration = schema_fields.FieldArray( 'integration', 'Integration test classes', item_type=schema_fields.SchemaField( 'entry', 'module.module.ClassName = test_count', 'string')) tests = schema_fields.FieldRegistry('tests_registry') tests.add_property(self.unit) tests.add_property(self.functional) tests.add_property(self.integration) manifest = schema_fields.FieldRegistry('manifest') manifest.add_property(self.files) self.tests = manifest.add_sub_registry( 'tests', title='Unit, functional and integration tests', registry=tests) self.registration = manifest.add_sub_registry('registration', title='Registration', registry=registration) return manifest
def get_schema(cls): youtube_event = schema_fields.FieldRegistry('event') youtube_event.add_property( schema_fields.SchemaField( 'position', 'Position', 'integer', description='Offset from start of video, in seconds.')) youtube_event.add_property( schema_fields.SchemaField( 'action', 'Action', 'string', description='Type of event that has occurred. The types that ' 'are known are: unstarted, ended, playing, paused, buffering, ' 'and video cued. If YouTube adds more types of events than ' 'these, they will be reported as a string version of the ' 'integer event code supplied by YouTube. Please see YouTube ' 'documentation for interpretation of unknown codes.')) youtube_event.add_property( schema_fields.SchemaField( 'timestamp', 'Timestamp', 'timestamp', description='Moment when event occurred.')) youtube_interaction = schema_fields.FieldRegistry('interaction') youtube_interaction.add_property( schema_fields.SchemaField( 'video_id', 'Video ID', 'string', description='The ID of the YouTube video. E.g., Kdg2drcUjYI ') ) youtube_interaction.add_property( schema_fields.FieldArray( 'events', 'YouTube Events', item_type=youtube_event, description='A list of events describing an interaction with ' 'a video. Note that these are grouped sequentially by ' 'video ID from the raw stream. It is technically possible, ' 'though unlikely, to get confusing results if multiple ' 'videos are viewed simultaneously by one student.')) youtube_interactions = schema_fields.FieldArray( 'youtube', 'YouTube Interactions', item_type=youtube_interaction, description='A list of interactions with individual YouTube ' 'video. These are ordered by the first interaction with a ' 'given video ID, and group together multiple actions ' 'within the same interaction.') return youtube_interactions
def options_schema_provider(unused_course): extra_tab_type = schema_fields.FieldRegistry( 'Extra Tab', extra_schema_dict_values={'className': 'settings-list-item'}) extra_tab_type.add_property(schema_fields.SchemaField( LABEL_KEY, 'Label', 'string', optional=True, description='The tab to be shown on the navbar.')) extra_tab_type.add_property(schema_fields.SchemaField( POSITION_KEY, 'Tab Position', 'string', optional=True, i18n=False, select_data=[(POS_LEFT, 'Left'), (POS_RIGHT, 'Right')])) extra_tab_type.add_property(schema_fields.SchemaField( VISIBILITY_KEY, 'Visibility', 'string', optional=True, i18n=False, select_data=[ (VIS_ALL, 'Everyone'), (VIS_STUDENT, 'Registered students')])) extra_tab_type.add_property(schema_fields.SchemaField( URL_KEY, 'Tab URL', 'string', optional=True, description='If a URL is provided, the tab will link to that URL. ' 'Otherwise it will display the "tab content" in a page.')) extra_tab_type.add_property(schema_fields.SchemaField( CONTENT_KEY, 'Tab Content', 'html', optional=True)) return schema_fields.FieldArray( 'course:' + EXTRA_TABS_KEY, 'Extra tabs', item_type=extra_tab_type, description=( 'Extra tabs to appear on the course navbar.'), extra_schema_dict_values={ 'className': 'settings-list wide', 'listAddLabel': 'Add a tab', 'listRemoveLabel': 'Delete tab'})
def test_field_array_with_object_members(self): object_type = schema_fields.FieldRegistry('object_title') object_type.add_property( schema_fields.SchemaField('prop_name', 'prop_label', 'prop_type')) field = schema_fields.FieldArray('aName', 'aLabel', item_type=object_type) expected = """ { "items": { "type": "object", "id": "object_title", "properties": { "prop_name": {"type":"prop_type"} } }, "type":"array"} """ self.assert_json_schema_value(expected, field) expected = """ [ [["_inputex"],{"label":"aLabel"}], [["items","title"],"object_title"], [["items","properties","prop_name","_inputex"],{"label":"prop_label"}] ] """ self.assert_schema_dict_value(expected, field)
def test_object_with_array_property(self): reg = schema_fields.FieldRegistry('registry_name', 'registry_description') reg.add_property( schema_fields.SchemaField('field_name', 'field_label', 'field_type', description='field_description')) reg.add_property( schema_fields.FieldArray('array_name', 'array_label', item_type=schema_fields.SchemaField( 'unusedName', 'unusedLabel', 'aType'))) expected = """ { "properties": { "field_name": { "type": "field_type", "description": "field_description" }, "array_name": { "items": {"type": "aType"}, "type":"array" } }, "type": "object", "id": "registry_name", "description": "registry_description" } """ self.assert_json_schema_value(expected, reg)
def get_schema(cls): user_agent_frequency = schema_fields.FieldRegistry( 'user_agent_frequency') user_agent_frequency.add_property( schema_fields.SchemaField( 'user_agent', 'User Agent', 'string', description='User-Agent string as reported by a browser.')) user_agent_frequency.add_property( schema_fields.SchemaField( 'frequency', 'Frequency', 'number', description='A floating point number greater than zero and less ' 'than or equal to 1.0. Indicates the relative frequency of the ' 'user_agent in responses from this user. The sum of all the ' 'frequency values should add up to 1.0. The most-frequent ' 'user_agent is listed first in the array.')) return schema_fields.FieldArray( 'user_agent_frequencies', 'User Agent Frequencies', item_type=user_agent_frequency, description='List of all User-Agents for this user, in ' 'descending order by proportion of responses.')
def get_milestone_array_schema(cls, milestone, desc_fmt, trigger_cls=None, scope_css=None, avail_select=None): title = availability_options.option_to_title(milestone) if trigger_cls is None: trigger_cls = triggers.MilestoneTrigger if avail_select is None: avail_select = availability_options.COURSE_WITH_NONE_SELECT_DATA item_type = cls.get_milestone_trigger_schema(milestone, avail_select, trigger_cls) if scope_css is None: scope_css = cls._COURSE_WIDE_SCOPE_CSS extra_css = (scope_css + ' ' + availability_options.option_to_css(milestone)) classname = trigger_cls.array_css(extra_css=extra_css) wrapper_classname = trigger_cls.array_wrapper_css(extra_css=extra_css) ms_text = availability_options.option_to_text(milestone) desc = desc_fmt.format(milestone=ms_text) return schema_fields.FieldArray(milestone, title, desc, item_type=item_type, optional=True, extra_schema_dict_values={ 'className': classname, 'wrapperClassName': wrapper_classname })
def get_schema(cls): """Provide schema; override default schema generated from DB type.""" locale_frequency = schema_fields.FieldRegistry('locale_frequency') locale_frequency.add_property( schema_fields.SchemaField( 'locale', 'Locale', 'string', description='A string indicating language and possibly regional ' 'variation. Always starts with an ISO-639-1 two-character ' 'lanaguage code. If the language is used in multiple countries, ' 'this is followed with an underscore ("_") character, and then ' 'an ISO-3166-1 two-character country code. E.g., "en_US"')) locale_frequency.add_property( schema_fields.SchemaField( 'frequency', 'Frequency', 'number', description='A floating point number greater than zero and less ' 'than or equal to 1.0. Indicates the relative frequency of the ' 'locale in responses from this user. The sum of all the ' 'frequency values should add up to 1.0. The most-frequent ' 'locale is listed first in the array.')) return schema_fields.FieldArray( 'locale_frequencies', 'Locale Frequencies', item_type=locale_frequency, description='List of all locales seen for this user, in ' 'descending order by proportion of responses.')
def _add_module_permissions_schema(cls, subschema, module_name): item_type = schema_fields.FieldRegistry( 'Permission', extra_schema_dict_values={'className': 'permission-item'}) item_type.add_property( schema_fields.SchemaField( 'assigned', 'Assigned', 'boolean', optional=True, extra_schema_dict_values={'className': 'permission-assigned'})) item_type.add_property( schema_fields.SchemaField( 'name', 'Name', 'string', editable=False, optional=True, extra_schema_dict_values={'className': 'permission-name'})) item_type.add_property( schema_fields.SchemaField( 'description', 'Description', 'string', optional=True, editable=False, extra_schema_dict_values={'className': 'inputEx-description'})) item_array = schema_fields.FieldArray( module_name, module_name, item_type=item_type, extra_schema_dict_values={'className': 'permission-module'}) subschema.add_property(item_array)
def _get_schema_field(property_type): name = property_type.name if property_type.data_type == list: # Shallow evaluation here is OK; Python DB API does not permit # array-of-array; when declaring a ListProperty, the item type # must be a Type instance (and thus cannot be a class, and thus # cannot be a Property class) item_type = schema_fields.SchemaField( name=name + ':item', label=name + ':item', optional=True, property_type=PYTHON_TYPE_TO_JSON_TYPE[property_type.item_type]) ret = schema_fields.FieldArray(name=name, label=name, description=property_type.verbose_name, item_type=item_type) else: type_name = PYTHON_TYPE_TO_JSON_TYPE.get(property_type.data_type) if not type_name: if issubclass(property_type.data_type, entities.BaseEntity): type_name = 'string' else: raise ValueError('Unsupported entity type for schema: %s' % str(property_type.data_type)) ret = schema_fields.SchemaField(name=name, label=name, property_type=type_name, description=property_type.verbose_name, optional=not property_type.required) return ret
def get_schema(cls, course, key): """Get the InputEx schema for the multiple choice question editor.""" mc_question = schema_fields.FieldRegistry( 'Multiple Choice Question', description='multiple choice question', extra_schema_dict_values={'className': 'mc-container'}) mc_question.add_property(schema_fields.SchemaField( 'description', 'Description', 'string', optional=True, extra_schema_dict_values={'className': 'mc-description'}, description=messages.QUESTION_DESCRIPTION)) mc_question.add_property(schema_fields.SchemaField( 'version', '', 'string', optional=True, hidden=True)) mc_question.add_property(schema_fields.SchemaField( 'question', 'Question', 'html', optional=True, extra_schema_dict_values={ 'supportCustomTags': tags.CAN_USE_DYNAMIC_TAGS.value, 'excludedCustomTags': TAGS_EXCLUDED_FROM_QUESTIONS, 'className': 'mc-question'})) mc_question.add_property(schema_fields.SchemaField( 'multiple_selections', 'Selection', 'boolean', optional=True, select_data=[ ('false', 'Allow only one selection'), ('true', 'Allow multiple selections')], extra_schema_dict_values={ '_type': 'radio', 'className': 'mc-selection'})) choice_type = schema_fields.FieldRegistry( 'Choice', extra_schema_dict_values={'className': 'mc-choice'}) choice_type.add_property(schema_fields.SchemaField( 'score', 'Score', 'string', optional=True, i18n=False, extra_schema_dict_values={ 'className': 'mc-choice-score', 'value': '0'})) choice_type.add_property(schema_fields.SchemaField( 'text', 'Text', 'html', optional=True, extra_schema_dict_values={ 'supportCustomTags': tags.CAN_USE_DYNAMIC_TAGS.value, 'excludedCustomTags': TAGS_EXCLUDED_FROM_QUESTIONS, 'className': 'mc-choice-text'})) choice_type.add_property(schema_fields.SchemaField( 'feedback', 'Feedback', 'html', optional=True, extra_schema_dict_values={ 'supportCustomTags': tags.CAN_USE_DYNAMIC_TAGS.value, 'excludedCustomTags': TAGS_EXCLUDED_FROM_QUESTIONS, 'className': 'mc-choice-feedback'})) choices_array = schema_fields.FieldArray( 'choices', '', item_type=choice_type, extra_schema_dict_values={ 'className': 'mc-choice-container', 'listAddLabel': 'Add a choice', 'listRemoveLabel': 'Delete choice'}) mc_question.add_property(choices_array) return mc_question
def options_schema_provider(unused_course): extra_tab_type = schema_fields.FieldRegistry( 'Extra Tab', extra_schema_dict_values={'className': 'settings-list-item'}) extra_tab_type.add_property( schema_fields.SchemaField( LABEL_KEY, 'Title', 'string', description=messages.EXTRA_TABS_TITLE_DESCRIPTION)) extra_tab_type.add_property( schema_fields.SchemaField( POSITION_KEY, 'Tab Position', 'string', description=messages.EXTRA_TAB_POSITION_DESCRIPTION, i18n=False, optional=True, select_data=[(POS_LEFT, 'Left'), (POS_RIGHT, 'Right')])) extra_tab_type.add_property( schema_fields.SchemaField( VISIBILITY_KEY, 'Visibility', 'string', optional=True, i18n=False, description=messages.EXTRA_TABS_VISIBILITY_DESCRIPTION, select_data=[(VIS_ALL, 'Everyone'), (VIS_STUDENT, 'Registered students')])) extra_tab_type.add_property( schema_fields.SchemaField( URL_KEY, 'Tab URL', 'string', optional=True, description=messages.EXTRA_TABS_URL_DESCRIPTION, extra_schema_dict_values={ '_type': 'url', 'showMsg': True })) extra_tab_type.add_property( schema_fields.SchemaField( CONTENT_KEY, 'Tab Content', 'html', optional=True, description=messages.EXTRA_TABS_CONTENT_DESCRIPTION)) return schema_fields.FieldArray( 'course:' + EXTRA_TABS_KEY, 'Extra Tabs', item_type=extra_tab_type, description=messages.EXTRA_TABS_DESCRIPTION, extra_schema_dict_values={ 'className': 'settings-list wide', 'listAddLabel': 'Add a tab', 'listRemoveLabel': 'Delete tab' }, optional=True)
def test_simple_field_array(self): self.assertEquals( set( schema_fields.FieldArray('x', 'x', item_type=schema_fields.SchemaField( None, None, 'string')).get_display_types()), set(['array', 'string']))
def get_element_array_schema(cls, item_type, scope_css=None): if scope_css is None: scope_css = cls._COURSE_WIDE_SCOPE_CSS wrapper_classname = ' '.join([scope_css, cls._ELEM_WRAPPER_CSS]) return schema_fields.FieldArray( cls.ELEMENT_SETTINGS, 'Content Availability', item_type=item_type, optional=True, extra_schema_dict_values={ 'className': cls._ELEM_ARRAY_CSS, 'wrapperClassName': wrapper_classname})
def get_schema(cls, app_context=None): cluster_schema = schema_fields.FieldRegistry( 'Cluster Definition', description='cluster definition', extra_schema_dict_values={'className': 'cluster-container'}) cluster_schema.add_property(schema_fields.SchemaField( 'version', '', 'string', optional=True, hidden=True)) cluster_schema.add_property(schema_fields.SchemaField( 'name', 'Name', 'string', optional=False, extra_schema_dict_values={'className': 'cluster-name'})) cluster_schema.add_property(schema_fields.SchemaField( 'description', 'Description', 'string', optional=True, extra_schema_dict_values={'className': 'cluster-description'})) dimension = schema_fields.FieldRegistry('Dimension', extra_schema_dict_values={'className': 'cluster-dim'}) to_select = [] dim_types = {} if app_context: dimensions = get_possible_dimensions(app_context) for dim in dimensions: select_id = cls.pack_id(dim[DIM_ID], dim[DIM_TYPE]) to_select.append((select_id, dim['name'])) dim_types[select_id] = dim[DIM_TYPE] dimension.add_property(schema_fields.SchemaField( DIM_ID, 'Dimension Name', 'string', i18n=False, extra_schema_dict_values={'className': 'dim-name'}, select_data=to_select)) # Only description for the first dimension. All the descriptions # are in the cluster_rest.js file. dimension.add_property(schema_fields.SchemaField( DIM_LOW, 'Minimum number of visits to the page', 'string', i18n=False, optional=True, extra_schema_dict_values={'className': 'dim-range-low'})) dimension.add_property(schema_fields.SchemaField( DIM_HIGH, 'Maximum number of visits to the page', 'string', i18n=False, optional=True, extra_schema_dict_values={'className': 'dim-range-high'})) dimension_array = schema_fields.FieldArray( 'vector', '', item_type=dimension, description='Dimensions of the cluster. Add a new dimension ' 'for each criteria the student has to acomplish to be ' 'included in the cluster', extra_schema_dict_values={ 'className': 'cluster-dim-container', 'listAddLabel': 'Add a dimension', 'listRemoveLabel': 'Delete dimension', 'dim_types': dim_types, 'types_info': cls.TYPES_INFO}) cluster_schema.add_property(dimension_array) return cluster_schema
def get_schema(cls): ret = schema_fields.FieldRegistry( 'Availability', 'Course Availability Settings', extra_schema_dict_values={ 'className': ( 'inputEx-Group new-form-layout hidden-header ' 'availability-manager')}) ret.add_property(schema_fields.SchemaField( 'course_availability', 'Course Availability', 'string', description='This sets the availability of the course for ' 'registered and unregistered students.', i18n=False, optional=True, select_data=[ (p, p.replace('_', ' ').title()) for p in courses.COURSE_AVAILABILITY_POLICIES])) element_settings = schema_fields.FieldRegistry( 'Element Settings', 'Availability settings for course elements', extra_schema_dict_values={'className': 'content-element'}) element_settings.add_property(schema_fields.SchemaField( 'type', 'Element Kind', 'string', i18n=False, optional=True, editable=False, hidden=True)) element_settings.add_property(schema_fields.SchemaField( 'id', 'Element Key', 'string', i18n=False, optional=True, editable=False, hidden=True)) element_settings.add_property(schema_fields.SchemaField( 'indent', 'Indent', 'boolean', i18n=False, optional=True, editable=False, hidden=True)) element_settings.add_property(schema_fields.SchemaField( 'name', 'Course Outline', 'string', i18n=False, optional=True, editable=False, extra_schema_dict_values={'className': 'title'})) element_settings.add_property(schema_fields.SchemaField( 'shown_when_unavailable', 'Shown When Private', 'boolean', description=services.help_urls.make_learn_more_message( messages.AVAILABILITY_SHOWN_WHEN_UNAVAILABLE_DESCRIPTION, 'course:availability:shown_when_unavailable'), i18n=False, optional=True, extra_schema_dict_values={'className': 'shown'})) element_settings.add_property(schema_fields.SchemaField( 'availability', 'Availability', 'string', description=services.help_urls.make_learn_more_message( messages.AVAILABILITY_AVAILABILITY_DESCRIPTION, 'course:availability:availability'), i18n=False, optional=True, select_data=[(a, a.title()) for a in courses.AVAILABILITY_VALUES], extra_schema_dict_values={'className': 'availability'})) ret.add_property(schema_fields.FieldArray( 'element_settings', 'Content Availability', item_type=element_settings, optional=True, extra_schema_dict_values={'className': 'content-availability'})) ret.add_property(schema_fields.SchemaField( 'whitelist', 'Students Allowed to Register', 'text', description='Only students with email addresses in this list may ' 'register for the course. Separate addresses with any combination ' 'of commas, spaces, or separate lines.', i18n=False, optional=True)) return ret
def get_schema(cls, unused_app_context, unused_catch_and_log, unused_source_context): reg = schema_fields.FieldRegistry( 'Raw Student Answers', description='Raw data of answers to all uses of all graded ' 'questions (excludes self-check non-graded questions in lessons) ' 'in the course.') reg.add_property(schema_fields.SchemaField( 'user_id', 'User ID', 'string', description='ID of the student providing this answer.')) reg.add_property(schema_fields.SchemaField( 'user_name', 'User Name', 'string', description='Name of the student providing this answer.')) reg.add_property(schema_fields.SchemaField( 'user_email', 'User Email', 'string', description='Email address of the student providing this answer.')) reg.add_property(schema_fields.SchemaField( 'unit_id', 'Unit ID', 'string', description='ID of unit or assessment for this score.')) reg.add_property(schema_fields.SchemaField( 'lesson_id', 'Lesson ID', 'string', optional=True, description='ID of lesson for this score.')) reg.add_property(schema_fields.SchemaField( 'sequence', 'Sequence', 'integer', description='0-based order within containing assessment/lesson.')) reg.add_property(schema_fields.SchemaField( 'question_id', 'Question ID', 'string', description='ID of question. Key to models.QuestionDAO')) reg.add_property(schema_fields.SchemaField( 'question_type', 'Question Type', 'string', description='Kind of question. E.g., "SaQuestion" or "McQuestion" ' 'for single-answer and multiple-choice, respectively.')) reg.add_property(schema_fields.SchemaField( 'timestamp', 'Question ID', 'integer', description='Seconds since 1970-01-01 in GMT when answer given.')) choice_type = schema_fields.SchemaField( 'answer', 'Answer', 'string', description='An answer to the question') reg.add_property(schema_fields.FieldArray( 'answers', 'Answers', item_type=choice_type, description='The answer from the student. Note that ' 'this may be an array for questions permitting multiple answers.')) reg.add_property(schema_fields.SchemaField( 'score', 'Score', 'number', description='Value from the Question indicating the score for ' 'this answer or set of answers.')) reg.add_property(schema_fields.SchemaField( 'weighted_score', 'Weighted Score', 'number', description='Question score, multiplied by weights in ' 'containing Question Group, Assessment, etc.')) reg.add_property(schema_fields.SchemaField( 'tallied', 'Tallied', 'boolean', description='Whether the score counts towards the overall grade. ' 'Lessons by default do not contribute to course score, but may ' 'be marked as graded.')) return reg.get_json_schema_dict()['properties']
def test_array_with_valid_content(self): reg = schema_fields.FieldRegistry('Test') reg.add_property(schema_fields.FieldArray( 'scalar_array', 'Scalar Array', item_type=schema_fields.SchemaField( 'a_string', 'A String', 'string'))) complaints = transforms.validate_object_matches_json_schema( {'scalar_array': ['foo', 'bar', 'baz']}, reg.get_json_schema_dict()) self.assertEqual(complaints, [])
def test_complex_field_array(self): item_type = schema_fields.FieldRegistry(None, '') item_type.add_property(schema_fields.SchemaField('x', 'x', 'string')) item_type.add_property(schema_fields.SchemaField('y', 'y', 'datetime')) self.assertEquals( set( schema_fields.FieldArray( 'x', 'x', item_type=item_type).get_display_types()), set(['array', 'string', 'datetime', 'group']))
def test_arrays_are_implicitly_optional(self): reg = schema_fields.FieldRegistry('Test') reg.add_property(schema_fields.FieldArray( 'scalar_array', 'Scalar Array', item_type=schema_fields.SchemaField( 'a_string', 'A String', 'string'))) complaints = transforms.validate_object_matches_json_schema( {}, reg.get_json_schema_dict()) self.assertEqual(complaints, [])
def get_criteria_editor_schema(course): criterion_type = schema_fields.FieldRegistry( 'Criterion', extra_schema_dict_values={'className': 'settings-list-item'}) select_data = [('default', '-- Select requirement --'), ( '', '-- Custom criterion --')] for unit in course.get_assessment_list(): select_data.append((unit.unit_id, unit.title + ( ' [Peer Graded]' if course.needs_human_grader(unit) else ''))) criterion_type.add_property(schema_fields.SchemaField( 'assessment_id', 'Requirement', 'string', # The JS will only reveal the following description # for peer-graded assessments description='When specifying a peer graded assessment as criterion, ' 'the student should complete both the assessment ' 'and the minimum of peer reviews.', extra_schema_dict_values={ 'className': 'inputEx-Field assessment-dropdown' }, i18n=False, optional=True, select_data=select_data)) criterion_type.add_property(schema_fields.SchemaField( 'pass_percent', 'Passing Percentage', 'string', extra_schema_dict_values={ 'className': 'pass-percent' }, i18n=False, optional=True)) select_data = [('', '-- Select criterion method--')] + [( x, x) for x in custom_criteria.registration_table] criterion_type.add_property(schema_fields.SchemaField( 'custom_criteria', 'Custom Criterion', 'string', extra_schema_dict_values={ 'className': 'custom-criteria' }, i18n=False, optional=True, select_data=select_data)) is_peer_assessment_table = {} for unit in course.get_assessment_list(): is_peer_assessment_table[unit.unit_id] = ( True if course.needs_human_grader(unit) else False) return schema_fields.FieldArray( 'certificate_criteria', 'Certificate Criteria', item_type=criterion_type, description=services.help_urls.make_learn_more_message( messages.CERTIFICATE_CRITERIA_DESCRIPTION, 'certificate:certificate_criteria'), extra_schema_dict_values={ 'is_peer_assessment_table': is_peer_assessment_table, 'className': 'settings-list', 'listAddLabel': 'Add a criterion', 'listRemoveLabel': 'Delete criterion'}, optional=True)
def test_extra_schema_dict(self): array = schema_fields.FieldArray( 'aName', 'aLabel', item_type=schema_fields.SchemaField( 'unusedName', 'field_label', 'aType'), extra_schema_dict_values={'a': 'A', 'b': 'B'}) expected = """ [ [["_inputex"],{"a":"A","b":"B","label":"aLabel"}], [["items","_inputex"],{"label":"field_label"}]] """ self.assert_schema_dict_value(expected, array)
def test_array_of_string(self): reg = schema_fields.FieldRegistry('Test') reg.add_property(schema_fields.FieldArray( 'string_array', 'String Array', item_type=schema_fields.SchemaField(None, None, 'string'), select_data=(('one', 'One'), ('two', 'Two'), ('three', 'Three')))) json_schema = reg.get_json_schema_dict() source = {'string_array': ['one', 'two']} self.assertEqual(transforms.validate_object_matches_json_schema( source, json_schema), []) self.assertEqual(transforms.json_to_dict(source, json_schema), source)
def test_array_with_bad_members(self): reg = schema_fields.FieldRegistry('Test') reg.add_property(schema_fields.FieldArray( 'scalar_array', 'Scalar Array', item_type=schema_fields.SchemaField( 'a_string', 'A String', 'string'))) complaints = transforms.validate_object_matches_json_schema( {'scalar_array': ['foo', 123, 'bar', 456, 'baz']}, reg.get_json_schema_dict()) self.assertEqual( complaints, ['Expected <type \'basestring\'> at Test.scalar_array[1], ' 'but instead had <type \'int\'>', 'Expected <type \'basestring\'> at Test.scalar_array[3], ' 'but instead had <type \'int\'>'])
def get_schema(cls): location_frequency = schema_fields.FieldRegistry('location_frequency') location_frequency.add_property( schema_fields.SchemaField( 'country', 'Country', 'string', optional=True, description='An ISO-3166-1 two-character country code.')) location_frequency.add_property( schema_fields.SchemaField( 'region', 'Region', 'string', optional=True, description='A string describing a region within a country. ' 'The format and content of this string may vary widely depending ' 'on the specific country\'s customs, but this will generally ' 'correspond to a top-level political division within the country.' )) location_frequency.add_property( schema_fields.SchemaField( 'city', 'City', 'string', optional=True, description= 'A string describing a town or city. As with region, ' 'local usage and custom will dictate the values here. This is ' 'not necessarily the lowest-level political division - e.g., ' 'this would be "New York", rather than "The Bronx"')) location_frequency.add_property( schema_fields.SchemaField( 'frequency', 'Frequency', 'number', description='A floating point number greater than zero and less ' 'than or equal to 1.0. Indicates the relative frequency of the ' 'location in responses from this user. The sum of all the ' 'frequency values should add up to 1.0. The most-frequent ' 'location is listed first in the array.')) return schema_fields.FieldArray( 'location_frequencies', 'Location Frequencies', item_type=location_frequency, description='List of all locations seen for this user, in ' 'descending order by proportion of responses.')
def lesson_rest_handler_schema_load_hook(lesson_field_registry): skill_type = schema_fields.FieldRegistry('Skill') skill_type.add_property( schema_fields.SchemaField('skill', 'Skill', 'number', optional=True, i18n=False)) lesson_field_registry.add_property( schema_fields.FieldArray( 'skills', 'Skills', optional=True, item_type=skill_type, extra_schema_dict_values={ 'className': 'skill-panel inputEx-Field inputEx-ListField' }))
def test_field_array_with_simple_members(self): array = schema_fields.FieldArray( 'aName', 'aLabel', item_type=schema_fields.SchemaField( 'unusedName', 'field_label', 'aType')) expected = """ { "items": {"type": "aType"}, "type": "array" }""" self.assert_json_schema_value(expected, array) expected = """ [ [["_inputex"],{"label":"aLabel"}], [["items","_inputex"],{"label":"field_label"}] ] """ self.assert_schema_dict_value(expected, array)
def get_content_trigger_array_schema(cls, trigger_cls, item_type, content_triggers_description, scope_css=None): if scope_css is None: scope_css = cls._COURSE_WIDE_SCOPE_CSS wrapper_classname = trigger_cls.array_wrapper_css(extra_css=scope_css) return schema_fields.FieldArray( trigger_cls.SETTINGS_NAME, 'Change Course Content Availability at Date/Time', item_type=item_type, optional=True, description=services.help_urls.make_learn_more_message( content_triggers_description, messages.CONTENT_TRIGGERS_LEARN_MORE), extra_schema_dict_values={ 'className': trigger_cls.array_css(), 'wrapperClassName': wrapper_classname, 'listAddLabel': cls.ADD_TRIGGER_BUTTON_TEXT, 'listRemoveLabel': 'Delete'})
def get_schema(cls, course, key): """Return the InputEx schema for the question group editor.""" question_group = schema_fields.FieldRegistry( 'Question Group', description='question_group') question_group.add_property(schema_fields.SchemaField( 'version', '', 'string', optional=True, hidden=True)) question_group.add_property(schema_fields.SchemaField( 'description', 'Description', 'string', optional=True)) question_group.add_property(schema_fields.SchemaField( 'introduction', 'Introduction', 'html', optional=True)) item_type = schema_fields.FieldRegistry( 'Item', extra_schema_dict_values={'className': 'question-group-item'}) item_type.add_property(schema_fields.SchemaField( 'weight', 'Weight', 'number', optional=True, i18n=False, extra_schema_dict_values={'className': 'question-group-weight'})) question_select_data = [(q.id, q.description) for q in sorted( models.QuestionDAO.get_all(), key=lambda x: x.description)] item_type.add_property(schema_fields.SchemaField( 'question', 'Question', 'string', optional=True, i18n=False, select_data=question_select_data, extra_schema_dict_values={'className': 'question-group-question'})) item_array_classes = 'question-group-items' if not question_select_data: item_array_classes += ' empty-question-list' item_array = schema_fields.FieldArray( 'items', '', item_type=item_type, extra_schema_dict_values={ 'className': item_array_classes, 'sortable': 'true', 'listAddLabel': 'Add a question', 'listRemoveLabel': 'Remove'}) question_group.add_property(item_array) return question_group
def get_schema(cls): event = schema_fields.FieldRegistry('event') event.add_property( schema_fields.SchemaField('href', 'URL', 'string', description='URL the link points to.')) event.add_property( schema_fields.SchemaField('timestamp', 'Timestamp', 'timestamp', description='When it was clicked.')) field = schema_fields.FieldArray( 'click_link', 'Clicked Links', item_type=event, description='A list of external links the student has clicked,' 'sorted by time. The same link will appear once for each click.') return field