Import keywords from XML
authorCharles Connell <charles@connells.org>
Mon, 17 Feb 2014 14:55:16 +0000 (09:55 -0500)
committerCharles Connell <charles@connells.org>
Mon, 17 Feb 2014 14:55:16 +0000 (09:55 -0500)
karmaworld/apps/quizzes/management/commands/import_quiz.py
karmaworld/apps/quizzes/xml_import.py
karmaworld/templates/quizzes/keyword_edit.html

index 4f2bb67fc0859fc16291572bf21c2a7defc481de..dd6886fed32892a32a5c0577cbd0b56e97c669fe 100644 (file)
@@ -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 = '<IQ-Metrics XML file> <note ID>'
     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])
 
index 73a77272d914d0370980a8159b19219bf5cb70ba..cb37238545065acf32775fc51235dfc721f95c51 100644 (file)
@@ -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)
index 91b0b99457087090eb12712e5e114269e54b3f0b..cfddcc2bb60f8fba2325bc9a363a0fb4901b633b 100644 (file)
@@ -35,7 +35,7 @@
 
       <div class="row">
         <div class="small-10 columns small-centered center">
-          <p>(Press tab in the last definition for more rows)</p>
+          <p>(Press tab in the last definition box for more rows)</p>
         </div>
       </div>