created a question bank xml file and created a seed_que command for adding questions to db from xml
authornishanth
Wed, 21 Apr 2010 11:08:44 +0530
changeset 28 456b7b9e3d13
parent 27 6233cf13fc12
child 29 ea1c0110e989
created a question bank xml file and created a seed_que command for adding questions to db from xml
question_bank.xml
quiz/management/commands/seed_que.py
quiz/models.py
templates/display_question.html
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/question_bank.xml	Wed Apr 21 11:08:44 2010 +0530
@@ -0,0 +1,74 @@
+<questionbank>
+	<day1quiz1>
+
+		<question>
+			<description>
+				How will you set the x and y axis limits so that the region of interest is in the rectangle $(0, -1.5)$ (left bottom coordinate) and $(2\pi, 1.5)$ (right top coordinate)?
+			</description>
+			<time_limit>
+				30
+			</time_limit>
+			<expected_answer>
+				xlim\( 0 , 2 \* pi \) [;,\n+] ylim\( -1.5 , 1.5\)
+		       </expected_answer>
+		</question>
+
+		<question>
+			<description>
+				If a = [1, 2, 5, 9] then what is the value of a[0:-1]?
+			</description>
+			<time_limit>
+				30
+			</time_limit>
+			<expected_answer>
+				\[ 1 , 2 , 5 \]
+		       </expected_answer>
+		</question>
+
+		<question>
+			<description>
+			A sample line from a Comma Separated Values (CSV) file:
+			  line = "Rossum, Guido, 42, 56, 34, 54"
+			  What code would you use to separate the line into fields?
+		  	</description>
+			<time_limit>
+				30
+			</time_limit>
+			<expected_answer>
+				(line\.split\( ([",']) , \2 \))
+			</expected_answer>
+		</question>
+
+		<question>
+			<description>
+				If a = [1, 2, 5, 9] how do you find the length of this list?
+			</description>
+			<time_limit>
+				30
+			</time_limit>
+			<expected_answer>
+				len\( a \) 
+			</expected_answer>
+		</question>
+
+	</day1quiz1>
+
+	<day1quiz2>
+		<question>
+			<description>
+				What is the maximum number that can be represented natively in Python.
+			</description>
+			<time_limit>
+				30
+			</time_limit>
+			<options>
+				Infinite
+				2**32
+			</options>
+			<expected_answer>
+				1
+			</expected_answer>
+		</question>
+	</day1quiz2>
+
+</questionbank>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/quiz/management/commands/seed_que.py	Wed Apr 21 11:08:44 2010 +0530
@@ -0,0 +1,53 @@
+from xml.dom.minidom import parse, Node
+
+from django.core.management.base import NoArgsCommand
+
+from offline.quiz.models import QuestionBank
+
+name2num = {"day1quiz1" : "11",
+            "day1quiz2" : "12",
+            "day2quiz1" : "21",
+           }
+
+def seed_que():
+    for question in QuestionBank.objects.all():
+        question.delete()
+
+    q_bank = parse("question_bank.xml").getElementsByTagName("question")
+    for question in q_bank:
+        quiz_name = question.parentNode.tagName
+        quiz_num = name2num[quiz_name]
+
+        description_node = question.getElementsByTagName("description")[0]
+        description = (description_node.childNodes[0].data).strip()
+
+        time_limit_node = question.getElementsByTagName("time_limit")[0]
+        time_limit = time_limit_node.childNodes[0].data
+
+
+        options_nodes = question.getElementsByTagName("options")
+        options = (options_nodes[0].childNodes[0].data).strip() if options_nodes else ""
+
+        code_nodes = question.getElementsByTagName("code")
+        code = (code_nodes[0].childNodes[0].data).strip() if code_nodes else ""
+
+        expected_ans_node = question.getElementsByTagName("expected_answer")[0]
+        expected_ans = (expected_ans_node.childNodes[0].data).strip()
+
+        new_question = QuestionBank(quiz_num = quiz_num, 
+                                    description = description, 
+                                    time_limit = time_limit,
+                                    options = options,
+                                    code = code,
+                                    expected_ans = expected_ans,
+                                   )
+        new_question.save()
+
+
+    
+class Command(NoArgsCommand):
+    
+    def handle_noargs(self, **options):
+        """ Just copied the code from seed_db.py """
+        
+        seed_que()
--- a/quiz/models.py	Wed Apr 21 00:51:20 2010 +0530
+++ b/quiz/models.py	Wed Apr 21 11:08:44 2010 +0530
@@ -4,6 +4,8 @@
 from offline.event.models import Event
 
 class Profile(models.Model):
+    """ A profile for quiz takers.
+    """
 
     user = models.ForeignKey(User)
     profession = models.CharField(max_length=20,help_text="(Ex: Faculty, Student etc.)")
--- a/templates/display_question.html	Wed Apr 21 00:51:20 2010 +0530
+++ b/templates/display_question.html	Wed Apr 21 11:08:44 2010 +0530
@@ -19,8 +19,13 @@
     </script>
 {% endblock %}
 {% block content %}
+{{question.description}}	<br />
+{% if question.code %}
+<fieldset><legend>Code</legend>
+<pre>{{question.code}}</pre></fieldset>
+{% endif %}
+
 <form action="" method="post" id="queform">
-		{{question.description}}	<br />
 		<textarea name="answer"></textarea><br />
 		<input type="button" value="Time left: {{question.time_limit}} secs" id="time_left">
 </form>