Commit b67bed62 by Alexander Kryklia

improve format, send poll divs via json

parent d597ac4c
...@@ -54,8 +54,7 @@ class PollModule(XModule): ...@@ -54,8 +54,7 @@ class PollModule(XModule):
downvotes = Integer(help="Number of downvotes this poll has recieved", scope=Scope.content, default=0) downvotes = Integer(help="Number of downvotes this poll has recieved", scope=Scope.content, default=0)
# voted = Boolean(help="Whether this student has voted on the poll", scope=Scope.student_state, default=False) # voted = Boolean(help="Whether this student has voted on the poll", scope=Scope.student_state, default=False)
html_render = String(scope=Scope.content) xml_object = String(scope=Scope.content)
sequence = String(scope=Scope.content)
def handle_ajax(self, dispatch, get): def handle_ajax(self, dispatch, get):
''' '''
...@@ -80,35 +79,24 @@ class PollModule(XModule): ...@@ -80,35 +79,24 @@ class PollModule(XModule):
def get_html(self): def get_html(self):
""" Renders parameters to template. """ """ Renders parameters to template. """
self.html_id = self.location.html_id()
self.html_class = self.location.category
self.poll_units_list = []
# import ipdb; ipdb.set_trace()
params = { params = {
'element_html': self.html_render, 'element_id': self.location.html_id(),
'element_id': self.html_id, 'element_class': self.location.category,
'element_class': self.html_class,
'ajax_url': self.system.ajax_url, 'ajax_url': self.system.ajax_url,
'poll_chain': self.parse_sequence(self.sequence), 'configuration_json': self.dump_poll(),
'configuration_json': json.dumps({}),
'poll_units': self.poll_units_list
} }
self.content = self.system.render_template( self.content = self.system.render_template('poll.html', params)
'poll.html', params)
return self.content return self.content
def get_poll_unit_html(self, i, question, css_class): def dump_poll(self):
""" """ """ """
return """ return json.dumps({'poll_chain':
""".format(poll_number=i, question=question, css_class=css_class) [{'question': stringify_children(q),
'id': q.get('id'),
def parse_sequence(self, html_string): 'upvote_id': q.get('upvote', ""),
""" substitute sequence """ 'downvote_id': q.get('downvote', ""),
return [(stringify_children(pu.xpath('//question')[0]), 'show_stats': q.get('show_stats', "yes")}
(pu.get('plot', "no"), pu.get('next_yes', "end"), for q in self.xml_object.xpath('question')]})
pu.get('next_no', "end"), pu.get('id')))
for pu in html.fromstring(html_string).xpath('//unit')]
class PollDescriptor(MakoModuleDescriptor, XmlDescriptor): class PollDescriptor(MakoModuleDescriptor, XmlDescriptor):
...@@ -127,21 +115,12 @@ class PollDescriptor(MakoModuleDescriptor, XmlDescriptor): ...@@ -127,21 +115,12 @@ class PollDescriptor(MakoModuleDescriptor, XmlDescriptor):
Returns: Returns:
dict dict
""" """
# import ipdb; ipdb.set_trace()
# check for presense of required tags in xml # check for presense of required tags in xml
expected_children_level_0 = ['render', 'sequence'] if len(xml_object.xpath('question')) == 0:
for child in expected_children_level_0: raise ValueError("Poll definition must include \
if len(xml_object.xpath(child)) != 1: at least one 'question' tag")
raise ValueError("Poll definition must include \
exactly one '{0}' tag".format(child)) return {'xml_object': xml_object}, []
def parse(k):
"""Assumes that xml_object has child k"""
return stringify_children(xml_object.xpath(k)[0])
return {
'html_render': parse('render'),
'sequence': parse('sequence')
}, []
def definition_to_xml(self, resource_fs): def definition_to_xml(self, resource_fs):
'''Return an xml element representing this definition.''' '''Return an xml element representing this definition.'''
......
<section <section
id="poll_${element_id}" id="poll_${element_id}"
class="${element_class}" class="${element_class}"
data-ajax-url="${ajax_url}" data-ajax-url="${ajax_url}">
>
<!-- Hidden field to read configuration JSON from. --> <!-- Hidden field to read configuration JSON from. -->
<div <div
class="${element_class}_div" class="${element_class}_div"
id="${element_id}_json" id="${element_id}_json"
style="display: none;" style="display: none;">
>${configuration_json}</div> ${configuration_json}</div>
<!-- Main HTML. -->
${element_html}
% for question, controls in poll_chain:
<div id="poll_unit_${loop.index}"
class="${'' if loop.index == 0 else 'hidden'} polls">
${question}
<div class="vote_and_submit">
<div id="vote_block-${loop.index}" class="vote">
<a class="upvote">Yes</a> <a class="downvote">No</a>
</div>
</div>
<div class="graph_answer"></div>
</div>
% endfor
</section> </section>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment