From c1dc32ce456f78a5b9456e06d4c81d11da684cee Mon Sep 17 00:00:00 2001 From: Charles Connell Date: Mon, 17 Feb 2014 09:55:16 -0500 Subject: [PATCH] Import keywords from XML --- .../management/commands/import_quiz.py | 10 +++++++--- karmaworld/apps/quizzes/xml_import.py | 20 ++++++++++++++++--- .../templates/quizzes/keyword_edit.html | 2 +- 3 files changed, 25 insertions(+), 7 deletions(-) diff --git a/karmaworld/apps/quizzes/management/commands/import_quiz.py b/karmaworld/apps/quizzes/management/commands/import_quiz.py index 4f2bb67..dd6886f 100644 --- a/karmaworld/apps/quizzes/management/commands/import_quiz.py +++ b/karmaworld/apps/quizzes/management/commands/import_quiz.py @@ -1,16 +1,20 @@ #!/usr/bin/env python # -*- coding:utf8 -*- # Copyright (C) 2014 FinalsClub Foundation +from argparse import ArgumentError from django.core.management import BaseCommand -from karmaworld.apps.quizzes.xml_import import quiz_from_xml +from karmaworld.apps.quizzes.xml_import import quiz_from_xml, keywords_from_xml class Command(BaseCommand): + args = ' ' help = """ Import a quiz from an IQ Metrics XML file """ def handle(self, *args, **kwargs): - quiz_from_xml(args[0]) - + if len(args) != 2: + raise ArgumentError("Incorrect arguments, see usage") + quiz_from_xml(args[0], args[1]) + keywords_from_xml(args[0], args[1]) diff --git a/karmaworld/apps/quizzes/xml_import.py b/karmaworld/apps/quizzes/xml_import.py index 73a7727..cb37238 100644 --- a/karmaworld/apps/quizzes/xml_import.py +++ b/karmaworld/apps/quizzes/xml_import.py @@ -1,7 +1,9 @@ from StringIO import StringIO +from karmaworld.apps.notes.models import Note import re from bs4 import BeautifulSoup -from karmaworld.apps.quizzes.models import MultipleChoiceQuestion, Quiz, TrueFalseQuestion, MultipleChoiceOption +from karmaworld.apps.quizzes.models import MultipleChoiceQuestion, Quiz, TrueFalseQuestion, MultipleChoiceOption, \ + Keyword from pyth.plugins.plaintext.writer import PlaintextWriter from pyth.plugins.rtf15.reader import Rtf15Reader @@ -115,12 +117,13 @@ def _multiple_choice(question, quiz_object): question=question_object) -def quiz_from_xml(filename): +def quiz_from_xml(filename, note_id): with open(filename, 'r') as file: soup = BeautifulSoup(file.read(), "xml") + note_object = Note.objects.get(id=note_id) quiz_name = soup.find('EChapterTitle').string - quiz_object = Quiz.objects.create(name=quiz_name) + quiz_object = Quiz.objects.create(name=quiz_name, note=note_object) questions = soup.find_all('TestBank') for question in questions: @@ -130,3 +133,14 @@ def quiz_from_xml(filename): elif type_string == 'True/False': _true_false(question, quiz_object) + + +def keywords_from_xml(filename, note_id): + with open(filename, 'r') as file: + soup = BeautifulSoup(file.read(), "xml") + + note_object = Note.objects.get(id=note_id) + + keywords = soup.find_all('WordPhrase') + for word in keywords: + Keyword.objects.create(word=word.string, note=note_object) diff --git a/karmaworld/templates/quizzes/keyword_edit.html b/karmaworld/templates/quizzes/keyword_edit.html index 91b0b99..cfddcc2 100644 --- a/karmaworld/templates/quizzes/keyword_edit.html +++ b/karmaworld/templates/quizzes/keyword_edit.html @@ -35,7 +35,7 @@
-

(Press tab in the last definition for more rows)

+

(Press tab in the last definition box for more rows)

-- 2.25.1