Skip to content

Commit

Permalink
Modify widgets to use value generators for their customization args. …
Browse files Browse the repository at this point in the history
…This commit removes the widget customization functionality in the frontend, which will be added back in the next commit or two.
  • Loading branch information
seanlip committed Aug 31, 2013
1 parent 221d409 commit 4caf778
Show file tree
Hide file tree
Showing 29 changed files with 533 additions and 296 deletions.
46 changes: 15 additions & 31 deletions core/controllers/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,30 +63,27 @@ def get(self, exploration_id):
except Exception as e:
raise self.PageNotFoundException(e)

# TODO: get params from exploration specification instead
params = exp_services.update_with_state_params(
reader_params = exp_services.update_with_state_params(
exploration_id,
exploration.init_state_id,
reader_params={}
)

init_state = exploration.init_state
init_html, init_widgets = exp_services.export_content_to_html(
init_state.content, 0, params)
init_state.content, 0, reader_params)

interactive_widget = widget_domain.Registry.get_widget_by_id(
feconf.INTERACTIVE_PREFIX, init_state.widget.widget_id)
interactive_html = interactive_widget.get_raw_code(
params=utils.parse_dict_with_params(
init_state.widget.params, params)
)
init_state.widget.customization_args, reader_params)

self.values.update({
'block_number': 0,
'interactive_html': interactive_html,
'interactive_params': init_state.widget.params,
'interactive_params': init_state.widget.customization_args,
'oppia_html': init_html,
'params': params,
'params': reader_params,
'state_history': [exploration.init_state_id],
'state_id': exploration.init_state_id,
'title': exploration.title,
Expand All @@ -102,21 +99,15 @@ class FeedbackHandler(base.BaseHandler):
"""Handles feedback to readers."""

def _append_answer_to_stats_log(
self, old_state, answer, exploration_id, old_state_id, handler,
rule):
self, old_state, answer, exploration_id, old_state_id,
old_params, handler, rule):
"""Append the reader's answer to the statistics log."""
widget = widget_domain.Registry.get_widget_by_id(
feconf.INTERACTIVE_PREFIX, old_state.widget.widget_id
)

# TODO(sll): Parse this using old_params, but do not convert into
# a JSON string.
recorded_answer_params = old_state.widget.params
recorded_answer_params.update({
'answer': answer,
})
recorded_answer = widget.get_stats_log_html(
params=recorded_answer_params)
old_state.widget.customization_args, old_params, answer)

stats_services.EventHandler.record_answer_submitted(
exploration_id, old_state_id, handler, str(rule), recorded_answer)
Expand Down Expand Up @@ -156,10 +147,7 @@ def _append_content(self, exploration_id, sticky, finished, old_params,
interactive_html = '' if sticky else (
widget_domain.Registry.get_widget_by_id(
feconf.INTERACTIVE_PREFIX, new_state.widget.widget_id
).get_raw_code(
params=utils.parse_dict_with_params(
new_state.widget.params, new_params)
)
).get_raw_code(new_state.widget.customization_args, new_params)
)

return (new_params, html_output, iframe_output, interactive_html)
Expand Down Expand Up @@ -209,22 +197,18 @@ def post(self, exploration_id, state_id):
)

self._append_answer_to_stats_log(
old_state, answer, exploration_id, state_id, handler, rule)
old_state, answer, exploration_id, state_id, old_params,
handler, rule)

# Append the reader's answer to the response HTML.
reader_response_html = ''
reader_response_iframe = ''
if not sticky:
reader_response_params = utils.parse_dict_with_params(
old_state.widget.params, old_params)
# TODO(sll): This is a hack to solve an issue with unnecessary
# conversion of the answer to a JavaScript-safe string. Fix it by
# using a custom filter.
reader_response_params['answer'] = answer
old_widget = widget_domain.Registry.get_widget_by_id(
feconf.INTERACTIVE_PREFIX, old_state.widget.widget_id)
reader_response_html, reader_response_iframe = (
widget_domain.Registry.get_widget_by_id(
feconf.INTERACTIVE_PREFIX, old_state.widget.widget_id
).get_reader_response_html(reader_response_params)
old_widget.get_reader_response_html(
old_state.widget.customization_args, old_params, answer)
)
values['reader_response_html'] = reader_response_html
values['reader_response_iframe'] = reader_response_iframe
Expand Down
17 changes: 8 additions & 9 deletions core/controllers/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ def get_widgets(self, widget_type):

response = collections.defaultdict(list)
for widget in widgets:
response[widget.category].append(widget.get_with_params({}))
response[widget.category].append(
widget.get_widget_instance_dict({}, {}))

for category in response:
response[category].sort()
Expand Down Expand Up @@ -87,15 +88,15 @@ def get(self, widget_type, widget_id):
widget = widget_domain.Registry.get_widget_by_id(
widget_type, widget_id)
self.render_json({
'widget': widget.get_with_params({}),
'widget': widget.get_widget_instance_dict({}, {}),
})
except:
raise self.PageNotFoundException

def post(self, widget_type, widget_id):
"""Handles POST requests, for parameterized widgets."""
# Key-value mapping of parameters for the widget.
params = self.payload.get('params', {})
customization_args = self.payload.get('params', {})

state_params_dict = {}
state_params_given = self.payload.get('state_params')
Expand All @@ -105,19 +106,17 @@ def post(self, widget_type, widget_id):
state_params_dict[param['name']] = (
utils.get_random_choice(param['values']))

# TODO(sll): We need a better convention for which params must be
# JSONified and which should not. Fix this.
widget = widget_domain.Registry.get_widget_by_id(
widget_type, widget_id)

if widget_type == feconf.NONINTERACTIVE_PREFIX:
self.render_json({
'widget': widget.get_with_params(params, kvps_only=True),
'widget': widget.get_widget_instance_dict(
customization_args, state_params_dict, kvps_only=True),
'parent_index': self.request.get('parent_index'),
})
else:
response = widget.get_with_params(
utils.parse_dict_with_params(params, state_params_dict),
kvps_only=True
response = widget.get_widget_instance_dict(
customization_args, state_params_dict, kvps_only=True
)
self.render_json({'widget': response})
24 changes: 8 additions & 16 deletions core/domain/exp_domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ class WidgetInstance(object):
def to_dict(self):
return {
'widget_id': self.widget_id,
'params': self.params,
'customization_args': self.customization_args,
'handlers': [handler.to_dict() for handler in self.handlers],
'sticky': self.sticky
}
Expand All @@ -175,24 +175,23 @@ def to_dict(self):
def from_dict(cls, widget_dict):
return cls(
widget_dict['widget_id'],
widget_dict['params'],
widget_dict['customization_args'],
[AnswerHandlerInstance.from_dict(h)
for h in widget_dict['handlers']],
widget_dict['sticky'],
)

def __init__(self, widget_id, params, handlers, sticky=False):
def __init__(self, widget_id, customization_args, handlers, sticky=False):
if not widget_id:
raise ValueError('No id specified for widget instance')
# TODO(sll): Check whether the widget_id is valid.
if not handlers:
raise ValueError('No handlers specified for widget instance')

self.widget_id = widget_id
# Parameters for the interactive widget view, stored as key-value
# pairs. Each parameter is single-valued. The values may be Jinja
# templates that refer to state parameters.
self.params = params
# Customization args for the interactive widget view. Parts of these
# args may be Jinja templates that refer to state parameters.
self.customization_args = customization_args
# Answer handlers and rule specs.
self.handlers = [AnswerHandlerInstance(
handler.name, handler.rule_specs
Expand All @@ -203,14 +202,7 @@ def __init__(self, widget_id, params, handlers, sticky=False):

@classmethod
def create_default_widget(cls, state_id):
continue_widget = widget_domain.Registry.get_widget_by_id(
feconf.INTERACTIVE_PREFIX, 'Continue')

continue_params = {}
for param in continue_widget.params:
continue_params[param.name] = param.value

return cls('Continue', continue_params,
return cls('Continue', {},
[AnswerHandlerInstance.get_default_handler(state_id)])


Expand Down Expand Up @@ -274,7 +266,7 @@ def __init__(self, id, name, content, param_changes, widget):
self.widget = WidgetInstance.create_default_widget(self.id)
else:
self.widget = WidgetInstance(
widget.widget_id, widget.params, widget.handlers,
widget.widget_id, widget.customization_args, widget.handlers,
widget.sticky)


Expand Down
7 changes: 4 additions & 3 deletions core/domain/exp_services.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,8 @@ def export_content_to_html(content_array, block_number, params=None):
widget_array.append({
'blockIndex': block_number,
'index': widget_array_len,
'raw': widget.get_raw_code(widget_dict['params']),
'raw': widget.get_raw_code(
widget_dict['customization_args'], params),
})
else:
raise utils.InvalidInputException(
Expand Down Expand Up @@ -795,8 +796,8 @@ def create_from_yaml(
}) for handler in wdict['handlers']]

state.widget = exp_domain.WidgetInstance(
wdict['widget_id'], wdict['params'], widget_handlers,
wdict['sticky'])
wdict['widget_id'], wdict['customization_args'],
widget_handlers, wdict['sticky'])

save_state(user_id, exploration_id, state)

Expand Down
18 changes: 5 additions & 13 deletions core/domain/exp_services_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ def test_export_to_yaml(self):
name: (untitled state)
param_changes: []
widget:
customization_args: {}
handlers:
- name: submit
rule_specs:
Expand All @@ -236,14 +237,13 @@ def test_export_to_yaml(self):
inputs: {}
name: Default
param_changes: []
params:
buttonText: Continue
sticky: false
widget_id: Continue
- content: []
name: New state
param_changes: []
widget:
customization_args: {}
handlers:
- name: submit
rule_specs:
Expand All @@ -252,8 +252,6 @@ def test_export_to_yaml(self):
inputs: {}
name: Default
param_changes: []
params:
buttonText: Continue
sticky: false
widget_id: Continue
""")
Expand Down Expand Up @@ -282,9 +280,7 @@ def test_export_to_versionable_dict(self):
'param_changes': []
}]
}],
'params': {
u'buttonText': u'Continue'
},
'customization_args': {},
'sticky': False,
'widget_id': u'Continue'
}
Expand All @@ -303,9 +299,7 @@ def test_export_to_versionable_dict(self):
'param_changes': []
}]
}],
'params': {
u'buttonText': u'Continue'
},
'customization_args': {},
'sticky': False,
'widget_id': u'Continue'
}
Expand Down Expand Up @@ -336,9 +330,7 @@ def test_export_state_to_dict(self):
'param_changes': [],
'widget': {
'widget_id': u'Continue',
'params': {
u'buttonText': u'Continue',
},
'customization_args': {},
'sticky': False,
'handlers': [{
'name': u'submit',
Expand Down
Loading

0 comments on commit 4caf778

Please sign in to comment.