summaryrefslogtreecommitdiff
path: root/exam
diff options
context:
space:
mode:
authorPrabhu Ramachandran2011-11-08 15:40:08 +0530
committerPrabhu Ramachandran2011-11-08 15:40:08 +0530
commit4694d217dd7d5659afdad7ba5937adb85c15371c (patch)
tree2dd78e791414abb6dbd6cf597892942dee0fce8f /exam
parentbed96635b1b0aac8548b6df85e96ee256e5e5bcb (diff)
downloadonline_test-4694d217dd7d5659afdad7ba5937adb85c15371c.tar.gz
online_test-4694d217dd7d5659afdad7ba5937adb85c15371c.tar.bz2
online_test-4694d217dd7d5659afdad7ba5937adb85c15371c.zip
ENH: Command to load questions from XML file.
Added manage command to load questions from XML file. Updated README and including a simple set of sample questions for testing.
Diffstat (limited to 'exam')
-rw-r--r--exam/management/__init__.py0
-rw-r--r--exam/management/commands/__init__.py0
-rw-r--r--exam/management/commands/load_questions.py64
3 files changed, 64 insertions, 0 deletions
diff --git a/exam/management/__init__.py b/exam/management/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/exam/management/__init__.py
diff --git a/exam/management/commands/__init__.py b/exam/management/commands/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/exam/management/commands/__init__.py
diff --git a/exam/management/commands/load_questions.py b/exam/management/commands/load_questions.py
new file mode 100644
index 0000000..336ed92
--- /dev/null
+++ b/exam/management/commands/load_questions.py
@@ -0,0 +1,64 @@
+# System library imports.
+from os.path import basename
+from xml.dom.minidom import parse
+from htmlentitydefs import name2codepoint
+import re
+
+# Django imports.
+from django.core.management.base import BaseCommand
+
+# Local imports.
+from exam.models import Question
+
+def decode_html(html_str):
+ """Un-escape or decode HTML strings to more usable Python strings.
+ From here: http://wiki.python.org/moin/EscapingHtml
+ """
+ return re.sub('&(%s);' % '|'.join(name2codepoint),
+ lambda m: unichr(name2codepoint[m.group(1)]), html_str)
+
+def clear_questions():
+ """Delete all questions from the database."""
+ for question in Question.objects.all():
+ question.delete()
+
+def load_questions(filename):
+ """Load questions from the given XML file."""
+ q_bank = parse(filename).getElementsByTagName("question")
+
+ for question in q_bank:
+
+ summary_node = question.getElementsByTagName("summary")[0]
+ summary = (summary_node.childNodes[0].data).strip()
+
+ desc_node = question.getElementsByTagName("description")[0]
+ description = (desc_node.childNodes[0].data).strip()
+
+ points_node = question.getElementsByTagName("points")[0]
+ points = int((points_node.childNodes[0].data).strip()) \
+ if points_node else 1
+
+ test_node = question.getElementsByTagName("test")[0]
+ test = decode_html((test_node.childNodes[0].data).strip())
+
+ new_question = Question(summary=summary,
+ description=description,
+ points=points,
+ test=test)
+ new_question.save()
+
+class Command(BaseCommand):
+ args = '<q_file1.xml q_file2.xml>'
+ help = 'loads the questions from given XML files'
+
+ def handle(self, *args, **options):
+ """Handle the command."""
+ # Delete existing stuff.
+ clear_questions()
+
+ # Load from files.
+ for fname in args:
+ self.stdout.write('Importing from {0} ... '.format(basename(fname)))
+ load_questions(fname)
+ self.stdout.write('Done\n')
+