Commit 468e4c75 by Will Daly

Generate JavaScript fixtures from Django templates

parent 6a7d036f
......@@ -58,3 +58,4 @@ coverage
ora2db
testdb
fixture_err.log
apps/openassessment/xblock/static/js/fixtures/*.html
<div id="openassessment-base">
<div id="openassessment__response"></div>
<div id="openassessment__peer-assessment"></div>
<div id="openassessment__self-assessment"></div>
<div id="openassessment__self-assessment"></div>
<div id="openassessment__grade"></div>
</div>
<div id="openassessment-edit">
<textarea class="openassessment-editor"></textarea>
<input type="button" class="openassessment-save-button" value="Save"/>
<input type="button" class="openassessment-cancel-button" value="Cancel"/>
</div>
<div id='openassessment-base'>
<form id="submission__feedback" class="submission__feedback" method="post">
<h3 class="submission__feedback__title">Give Feedback On Peer Evaluations</h3>
<div class="submission__feedback__content">
<span class="transition__status">
<span class="wrapper--anim">
<i class="ico icon-refresh icon-spin"></i>
<span class="copy">Submitting Feedback</span>
</span>
</span>
<div class="message message--complete">
<h3 class="message__title">Your Feedback Has Been Submitted</h3>
<div class="message__content">
<p>Your feedback will be sent to this course's staff for use when they review course records.</p>
</div>
</div>
<div class="submission__feedback__instructions">
<p>Course staff will be able to see any feedback that you provide here when they review course records.</p>
</div>
<ol class="list list--fields submission__feedback__fields">
<li class="field field--radio feedback__overall" id="feedback__overall">
<h4>Please select the statements below that reflect what you think of this peer grading experience:</h4>
<ol class="list--options">
<li class="option option--useful">
<input type="checkbox"
name="feedback__overall__value"
id="feedback__overall__value--useful"
class="feedback__overall__value"
value="These assessments were useful." />
<label for="feedback__overall__value--useful">These assessments were useful.</label>
</li>
<li class="option option--notuseful">
<input type="checkbox"
name="feedback__overall__value"
id="feedback__overall__value--notuseful"
class="feedback__overall__value"
value="These assessments were not useful." />
<label for="feedback__overall__value--notuseful">These assessments were not useful.</label>
</li>
<li class="option option--disagree">
<input type="checkbox"
name="feedback__overall__value"
id="feedback__overall__value--disagree"
class="feedback__overall__value"
value="I disagree with the ways that my peers assessed me." />
<label for="feedback__overall__value--notuseful">I disagree with the ways that my peers assessed me.</label>
</li>
<li class="option option--inappropriate">
<input type="checkbox"
name="feedback__overall__value"
id="feedback__overall__value--inappropriate"
class="feedback__overall__value"
value="I received some inappropriate comments." />
<label for="feedback__overall__value--notuseful">I received some inappropriate comments.</label>
</li>
</ol>
</li>
<li class="field field--textarea feedback__remarks" id="feedback__remarks">
<label for="feedback__remarks__value">Please provide any thoughts or comments on the feedback you received from your peers here.</label>
<textarea id="feedback__remarks__value" placeholder="I feel the feedback I received was...">{{ feedback_text }}</textarea>
</li>
</ol>
<div class="submission__feedback__actions">
<div class="message message--inline message--error message--error-server">
<h3 class="message__title">We could not submit your feedback</h3>
</div>
<ul class="list list--actions submission__feedback__actions">
<li class="list--actions__item">
<button type="submit" id="feedback__submit" class="action action--submit feedback__submit">Submit Feedback On Peer Evaluations</button>
</li>
</ul>
</div>
</div>
</form>
</div>
<div id="openassessment-base">
<ol>
<li id="openassessment__response" class="openassessment__steps__step step--response ui-toggle-visibility">
<div class="ui-toggle-visibility__content">
<div class="wrapper--step__content">
<div class="step__content">
<form id="response__submission" class="response__submission">
<ol class="list list--fields response__submission__content">
<li class="field field--textarea submission__answer" id="submission__answer">
<label class="sr" for="submission__answer__value">Provide your response to the question.</label>
<textarea id="submission__answer__value" placeholder=""></textarea>
<span class="tip">You may continue to work on your response until you submit it.</span>
</li>
</ol>
<div class="response__submission__actions">
<div class="message message--inline message--error message--error-server">
<h3 class="message__title">We could not save your progress</h3>
</div>
<ul class="list list--actions">
<li class="list--actions__item">
<button type="submit" id="submission__save" class="action action--save submission__save is--disabled">Save Your Progress</button>
<div id="response__save_status" class="response__submission__status">
<h3 class="response__submission__status__title">
<span class="sr">Your Working Submission Status:</span>
Unsaved draft
</h3>
</div>
</li>
</ul>
</div>
</form>
</div>
<div class="step__actions">
<div class="message message--inline message--error message--error-server">
<h3 class="message__title">We could not submit your response</h3>
</div>
<ul class="list list--actions">
<li class="list--actions__item">
<a aria-role="button" href="#" id="step--response__submit" class="action action--submit step--response__submit is--disabled">
<span class="copy">Submit your response and move to the next step</span>
<i class="ico icon-caret-right"></i>
</a>
</li>
</ul>
</div>
</div>
</div>
</li>
</ol>
</div>
<span id="peer_submission_uuid">abc1234</span>
<div id="peer-assessment--001__assessment">
<input name="option 1" type="radio" />
<input name="option 2" type="radio" />
<input name="option 3" type="radio" />
</div>
<div id="assessment__rubric__question--feedback__value">test feedback</div>
<span id="self_submission_uuid">abc1234</span>
<div id="self-assessment--001__assessment">
<input name="option 1" type="radio" />
<input name="option 2" type="radio" />
<input name="option 3" type="radio" />
</div>
[
{
"template": "openassessmentblock/oa_base.html",
"context": {
"title": "Test title",
"question": "Test prompt",
"rubric_criteria": [],
"rubric_assessments": [
{
"name": "submission",
"class_id": "openassessment__response",
"navigation_text": "Your response to this problem",
"title": "Your Response"
},
{
"name": "peer-assessment",
"class_id": "openassessment__peer-assessment",
"navigation_text": "Your assessment(s) of peer responses",
"title": "Assess Peers' Responses"
},
{
"name": "self-assessment",
"class_id": "openassessment__self-assessment",
"navigation_text": "Your assessment of your response",
"title": "Assess Your Response"
},
{
"name": "grade",
"class_id": "openassessment__grade",
"navigation_text": "Your grade for this problem",
"title": "Your Grade:"
}
]
},
"output": "oa_base.html"
},
{
"template": "openassessmentblock/response/oa_response.html",
"context": {
"saved_response": "",
"save_status": "Unsaved draft",
"submit_enabled": false,
"submission_due": ""
},
"output": "oa_response.html"
},
{
"template": "openassessmentblock/self/oa_self_assessment.html",
"context": {
"rubric_criteria": [],
"self_submission": {}
},
"output": "oa_self_assessment.html"
},
{
"template": "openassessmentblock/peer/oa_peer_assessment.html",
"context": {
"rubric_criteria": [],
"peer_submission": {}
},
"output": "oa_peer_assessment.html"
},
{
"template": "openassessmentblock/grade/oa_grade_complete.html",
"context": {
"score": "",
"feedback_text": "",
"student_submission": "",
"peer_assessments": [],
"self_assessment": {},
"rubric_criteria": [],
"has_submitted_feedback": false
},
"output": "oa_grade_complete.html"
},
{
"template": "openassessmentblock/oa_edit.html",
"context": {},
"output": "oa_edit.html"
}
]
......@@ -9,10 +9,10 @@ describe("OpenAssessment.BaseView", function() {
// Dummy fragments to return from the render func
this.fragments = {
submission: "Test submission",
self_assessment: readFixtures("self_assessment_frag.html"),
peer_assessment: readFixtures("peer_assessment_frag.html"),
grade: "Test fragment"
submission: readFixtures("oa_response.html"),
self_assessment: readFixtures("oa_self_assessment.html"),
peer_assessment: readFixtures("oa_peer_assessment.html"),
grade: readFixtures("oa_grade_complete.html")
};
this.peerAssess = function(submissionId, optionsSelected, feedback) {
......@@ -49,10 +49,7 @@ describe("OpenAssessment.BaseView", function() {
});
waitsFor(function() {
var subviewHasHtml = $("#openassessment-base").children().map(
function(index, el) { return el.innerHTML !== ''; }
);
return Array(subviewHasHtml).every(function(hasHtml) { return hasHtml; });
return !$(".openassessment__steps__step").hasClass('is--loading');
});
runs(function() {
......@@ -69,7 +66,7 @@ describe("OpenAssessment.BaseView", function() {
server = new StubServer();
// Create the object under test
var el = $("#openassessment-base").get(0);
var el = $("#openassessment").get(0);
view = new OpenAssessment.BaseView(runtime, el, server);
});
......
......@@ -239,7 +239,7 @@ OpenAssessment.BaseView.prototype = {
*/
peerAssessRequest: function(successFunction) {
// Retrieve assessment info from the DOM
var submissionId = $("span#peer_submission_uuid", this.element)[0].innerHTML.trim();
var submissionId = $("#peer_submission_uuid", this.element)[0].innerHTML.trim();
var optionsSelected = {};
$("#peer-assessment--001__assessment input[type=radio]:checked", this.element).each(
function(index, sel) {
......@@ -263,7 +263,7 @@ OpenAssessment.BaseView.prototype = {
**/
selfAssess: function() {
// Retrieve self-assessment info from the DOM
var submissionId = $("span#self_submission_uuid", this.element)[0].innerHTML.trim();
var submissionId = $("#self_submission_uuid", this.element)[0].innerHTML.trim();
var optionsSelected = {};
$("#self-assessment--001__assessment input[type=radio]:checked", this.element).each(
function(index, sel) {
......
#!/usr/bin/env python
"""
Render Django templates.
Useful for generating fixtures for the JavaScript unit test suite.
Usage:
python render_templates.py path/to/templates.json
where "templates.json" is a JSON file of the form:
[
{
"template": "openassessmentblock/oa_base.html",
"context": {
"title": "Lorem",
"question": "Ipsum?"
},
"output": "oa_base.html"
},
...
]
The rendered templates are saved to "output" relative to the
templates.json file's directory.
"""
import sys
import os.path
import json
# This is a bit of a hack to ensure that the root repo directory
# is in the Python path, so Django can find the settings module.
sys.path.append(os.path.dirname(os.path.dirname(__file__)))
from django.template.context import Context
from django.template.loader import get_template
USAGE = u"{prog} TEMPLATE_DESC"
def render_templates(root_dir, template_json):
"""
Create rendered templates.
Args:
root_dir (str): The directory in which to write the rendered templates.
template_json (dict): Description of which templates to render. Must be a list
of dicts, each containing keys "template" (str), "context" (dict), and "output" (str).
Returns:
None
"""
for template_dict in template_json:
template = get_template(template_dict['template'])
rendered = template.render(Context(template_dict['context']))
output_path = os.path.join(root_dir, template_dict['output'])
try:
with open(output_path, 'w') as output_file:
output_file.write(rendered.encode('utf-8'))
except IOError:
print "Could not write rendered template to file: {}".format(output_path)
sys.exit(1)
def main():
"""
Main entry point for the script.
"""
if len(sys.argv) < 2:
print USAGE.format(sys.argv[0])
sys.exit(1)
try:
with open(sys.argv[1]) as template_json:
root_dir = os.path.dirname(sys.argv[1])
render_templates(root_dir, json.load(template_json))
except IOError as ex:
print u"Could not open template description file: {}".format(sys.argv[1])
print(ex)
sys.exit(1)
except ValueError as ex:
print u"Could not parse template description as JSON: {}".format(sys.argv[1])
print(ex)
sys.exit(1)
if __name__ == '__main__':
main()
......@@ -10,6 +10,9 @@ echo "Running Python tests..."
export DJANGO_SETTINGS_MODULE="settings.test"
python manage.py test
echo "Generating HTML fixtures for JavaScript tests..."
./scripts/render_templates.py apps/openassessment/xblock/static/js/fixtures/templates.json
echo "Running JavaScript tests..."
npm test
......
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