def importer(self, node, zipfile): children = node.getchildren() assert len(children) == 1 and children[0].tag == "quiz" rhetorical = asbool(children[0].get("rhetorical")) path = children[0].get("description_src") description = zipfile.read(path) q = quizblock.Quiz(rhetorical=rhetorical, description=description) q.save() for child in children[0].iterchildren(): assert child.tag == "question" type = child.get("type") text, explanation, introtext, answers = \ child.getchildren()[:3] + [child.getchildren()[3:]] path = text.get("src") text = zipfile.read(path) path = explanation.get("src") explanation = zipfile.read(path) path = introtext.get("src") introtext = zipfile.read(path) question = quizblock.Question( quiz=q, text=text, question_type=type, explanation=explanation, intro_text=introtext) question.save() for answer in answers: label = answer.get("label") value = answer.get("value") correct = asbool(answer.get("correct")) answer = quizblock.Answer( question=question, value=value, label=label, correct=correct) answer.save() return q
def import_node(hierarchy, section, zipfile, parent=None): slug = section.get("slug") label = section.get("label") is_root = asbool(section.get("is_root")) assert (parent and not is_root) or (is_root and not parent) if parent is None: s = hierarchy.get_root() s.slug = slug s.label = label s.save() else: s = parent.append_child(label, slug) s.save() for child in section.iterchildren(): if child.tag == "pageblock": import_pageblock(hierarchy, s, child, zipfile) elif child.tag == "section": import_node(hierarchy, child, zipfile, parent=s) else: raise TypeError("Badly formatted import file") return s