Skip to content

Commit

Permalink
Fix oppia#5177: Added migration function to ensure caption in images (o…
Browse files Browse the repository at this point in the history
…ppia#5179)

* added migration function to ensure caption in images

* moved convert_state function to State class, updated escaping of empty html

* Improved function names, made test strings informative
  • Loading branch information
bansalnitish authored and vojtechjelinek committed Jul 5, 2018
1 parent 49c37b3 commit 05f85ff
Show file tree
Hide file tree
Showing 6 changed files with 628 additions and 64 deletions.
142 changes: 95 additions & 47 deletions core/domain/exp_domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -1776,6 +1776,63 @@ def create_default_state(
default_dest_state_name),
feconf.DEFAULT_CONTENT_IDS_TO_AUDIO_TRANSLATIONS)

@classmethod
def convert_html_fields_in_state(cls, state_dict, conversion_fn):
"""Applies a conversion function on all the html strings in a state
to migrate them to a desired state.
Args:
state_dict: dict. The dict representation of State object.
conversion_fn: function. The conversion function to be applied on
the states_dict.
Returns:
dict. The converted state_dict.
"""
state_dict['content']['html'] = (
conversion_fn(state_dict['content']['html']))
if state_dict['interaction']['default_outcome']:
interaction_feedback_html = state_dict[
'interaction']['default_outcome']['feedback']['html']
state_dict['interaction']['default_outcome']['feedback'][
'html'] = conversion_fn(interaction_feedback_html)

for answer_group_index, answer_group in enumerate(
state_dict['interaction']['answer_groups']):
answer_group_html = answer_group['outcome']['feedback']['html']
state_dict['interaction']['answer_groups'][
answer_group_index]['outcome']['feedback']['html'] = (
conversion_fn(answer_group_html))
if state_dict['interaction']['id'] == 'ItemSelectionInput':
for rule_spec_index, rule_spec in enumerate(
answer_group['rule_specs']):
for x_index, x in enumerate(rule_spec['inputs']['x']):
state_dict['interaction']['answer_groups'][
answer_group_index]['rule_specs'][
rule_spec_index]['inputs']['x'][x_index] = (
conversion_fn(x))
for hint_index, hint in enumerate(
state_dict['interaction']['hints']):
hint_html = hint['hint_content']['html']
state_dict['interaction']['hints'][hint_index][
'hint_content']['html'] = conversion_fn(hint_html)

if state_dict['interaction']['solution']:
solution_html = state_dict[
'interaction']['solution']['explanation']['html']
state_dict['interaction']['solution']['explanation']['html'] = (
conversion_fn(solution_html))

if state_dict['interaction']['id'] in (
'ItemSelectionInput', 'MultipleChoiceInput'):
for value_index, value in enumerate(
state_dict['interaction']['customization_args'][
'choices']['value']):
state_dict['interaction']['customization_args'][
'choices']['value'][value_index] = conversion_fn(value)

return state_dict


class ExplorationVersionsDiff(object):
"""Domain object for the difference between two versions of an Oppia
Expand Down Expand Up @@ -3467,54 +3524,26 @@ def _convert_states_v21_dict_to_v22_dict(cls, states_dict):
Returns:
dict. The converted states_dict.
"""
for state_dict in states_dict.values():
state_dict['content']['html'] = (
html_cleaner.convert_to_textangular(
state_dict['content']['html']))
if state_dict['interaction']['default_outcome']:
interaction_feedback_html = state_dict[
'interaction']['default_outcome']['feedback']['html']
state_dict['interaction']['default_outcome']['feedback'][
'html'] = html_cleaner.convert_to_textangular(
interaction_feedback_html)

for answer_group_index, answer_group in enumerate(
state_dict['interaction']['answer_groups']):
answer_group_html = answer_group['outcome']['feedback']['html']
state_dict['interaction']['answer_groups'][
answer_group_index]['outcome']['feedback']['html'] = (
html_cleaner.convert_to_textangular(
answer_group_html))
if state_dict['interaction']['id'] == 'ItemSelectionInput':
for rule_spec_index, rule_spec in enumerate(
answer_group['rule_specs']):
for x_index, x in enumerate(rule_spec['inputs']['x']):
state_dict['interaction']['answer_groups'][
answer_group_index]['rule_specs'][
rule_spec_index]['inputs']['x'][x_index] = (
html_cleaner.convert_to_textangular(x))
for hint_index, hint in enumerate(
state_dict['interaction']['hints']):
hint_html = hint['hint_content']['html']
state_dict['interaction']['hints'][hint_index][
'hint_content']['html'] = (
html_cleaner.convert_to_textangular(hint_html))
for key, state_dict in states_dict.iteritems():
states_dict[key] = State.convert_html_fields_in_state(
state_dict, html_cleaner.convert_to_textangular)
return states_dict

if state_dict['interaction']['solution']:
solution_html = state_dict[
'interaction']['solution']['explanation']['html']
state_dict['interaction']['solution']['explanation']['html'] = (
html_cleaner.convert_to_textangular(solution_html))

if state_dict['interaction']['id'] in (
'ItemSelectionInput', 'MultipleChoiceInput'):
for value_index, value in enumerate(
state_dict['interaction']['customization_args'][
'choices']['value']):
state_dict['interaction']['customization_args'][
'choices']['value'][value_index] = (
html_cleaner.convert_to_textangular(value))
@classmethod
def _convert_states_v22_dict_to_v23_dict(cls, states_dict):
"""Converts from version 22 to 23. Version 23 ensures that all
all oppia-noninteractive-image tags have caption attribute.
Args:
states_dict: dict. A dict where each key-value pair represents,
respectively, a state name and a dict used to initialize a
State domain object.
Returns:
dict. The converted states_dict.
"""
for key, state_dict in states_dict.iteritems():
states_dict[key] = State.convert_html_fields_in_state(
state_dict, html_cleaner.add_caption_attr_to_image)
return states_dict

@classmethod
Expand Down Expand Up @@ -3548,7 +3577,7 @@ def update_states_from_model(
# incompatible changes are made to the exploration schema in the YAML
# definitions, this version number must be changed and a migration process
# put in place.
CURRENT_EXP_SCHEMA_VERSION = 27
CURRENT_EXP_SCHEMA_VERSION = 28
LAST_UNTITLED_SCHEMA_VERSION = 9

@classmethod
Expand Down Expand Up @@ -4053,6 +4082,20 @@ def _convert_v26_dict_to_v27_dict(cls, exploration_dict):

return exploration_dict

@classmethod
def _convert_v27_dict_to_v28_dict(cls, exploration_dict):
"""Converts a v27 exploration dict into a v28 exploration dict.
Adds caption attribute to all oppia-noninteractive-image tags.
"""
exploration_dict['schema_version'] = 28

exploration_dict['states'] = cls._convert_states_v22_dict_to_v23_dict(
exploration_dict['states'])
exploration_dict['states_schema_version'] = 23

return exploration_dict

@classmethod
def _migrate_to_latest_yaml_version(
cls, yaml_content, title=None, category=None):
Expand Down Expand Up @@ -4220,6 +4263,11 @@ def _migrate_to_latest_yaml_version(
exploration_dict)
exploration_schema_version = 27

if exploration_schema_version == 27:
exploration_dict = cls._convert_v27_dict_to_v28_dict(
exploration_dict)
exploration_schema_version = 28

return (exploration_dict, initial_schema_version)

@classmethod
Expand Down
Loading

0 comments on commit 05f85ff

Please sign in to comment.