quiz/management/commands/seed_que.py
author nishanth
Tue, 29 Jun 2010 11:47:53 +0530
changeset 60 8ca2734bdc99
parent 50 0842b3439c3e
child 61 3b8b0ce51566
permissions -rw-r--r--
modified the seed_que to suit the new design
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
28
456b7b9e3d13 created a question bank xml file and created a seed_que command for adding questions to db from xml
nishanth
parents:
diff changeset
     1
from xml.dom.minidom import parse, Node
456b7b9e3d13 created a question bank xml file and created a seed_que command for adding questions to db from xml
nishanth
parents:
diff changeset
     2
456b7b9e3d13 created a question bank xml file and created a seed_que command for adding questions to db from xml
nishanth
parents:
diff changeset
     3
from django.core.management.base import NoArgsCommand
456b7b9e3d13 created a question bank xml file and created a seed_que command for adding questions to db from xml
nishanth
parents:
diff changeset
     4
60
8ca2734bdc99 modified the seed_que to suit the new design
nishanth
parents: 50
diff changeset
     5
from offline.quiz.models import QuestionBank, Quiz, TOPIC_CHOICES
28
456b7b9e3d13 created a question bank xml file and created a seed_que command for adding questions to db from xml
nishanth
parents:
diff changeset
     6
456b7b9e3d13 created a question bank xml file and created a seed_que command for adding questions to db from xml
nishanth
parents:
diff changeset
     7
name2num = {"day1quiz1" : "11",
456b7b9e3d13 created a question bank xml file and created a seed_que command for adding questions to db from xml
nishanth
parents:
diff changeset
     8
            "day1quiz2" : "12",
456b7b9e3d13 created a question bank xml file and created a seed_que command for adding questions to db from xml
nishanth
parents:
diff changeset
     9
            "day2quiz1" : "21",
456b7b9e3d13 created a question bank xml file and created a seed_que command for adding questions to db from xml
nishanth
parents:
diff changeset
    10
           }
456b7b9e3d13 created a question bank xml file and created a seed_que command for adding questions to db from xml
nishanth
parents:
diff changeset
    11
60
8ca2734bdc99 modified the seed_que to suit the new design
nishanth
parents: 50
diff changeset
    12
topic_name2num = dict( [ (_[1], _[0]) for _ in TOPIC_CHOICES ] )
8ca2734bdc99 modified the seed_que to suit the new design
nishanth
parents: 50
diff changeset
    13
28
456b7b9e3d13 created a question bank xml file and created a seed_que command for adding questions to db from xml
nishanth
parents:
diff changeset
    14
def seed_que():
456b7b9e3d13 created a question bank xml file and created a seed_que command for adding questions to db from xml
nishanth
parents:
diff changeset
    15
    for question in QuestionBank.objects.all():
456b7b9e3d13 created a question bank xml file and created a seed_que command for adding questions to db from xml
nishanth
parents:
diff changeset
    16
        question.delete()
456b7b9e3d13 created a question bank xml file and created a seed_que command for adding questions to db from xml
nishanth
parents:
diff changeset
    17
456b7b9e3d13 created a question bank xml file and created a seed_que command for adding questions to db from xml
nishanth
parents:
diff changeset
    18
    q_bank = parse("question_bank.xml").getElementsByTagName("question")
456b7b9e3d13 created a question bank xml file and created a seed_que command for adding questions to db from xml
nishanth
parents:
diff changeset
    19
    for question in q_bank:
60
8ca2734bdc99 modified the seed_que to suit the new design
nishanth
parents: 50
diff changeset
    20
        #quiz_name = question.parentNode.tagName
8ca2734bdc99 modified the seed_que to suit the new design
nishanth
parents: 50
diff changeset
    21
        #quiz_num = name2num[quiz_name]
8ca2734bdc99 modified the seed_que to suit the new design
nishanth
parents: 50
diff changeset
    22
8ca2734bdc99 modified the seed_que to suit the new design
nishanth
parents: 50
diff changeset
    23
        topic_name_node = question.getElementsByTagName("topic")[0]
8ca2734bdc99 modified the seed_que to suit the new design
nishanth
parents: 50
diff changeset
    24
        topic_name = (topic_name_node.childNodes[0].data).strip()
8ca2734bdc99 modified the seed_que to suit the new design
nishanth
parents: 50
diff changeset
    25
        topic = topic_name2num[topic_name]
28
456b7b9e3d13 created a question bank xml file and created a seed_que command for adding questions to db from xml
nishanth
parents:
diff changeset
    26
456b7b9e3d13 created a question bank xml file and created a seed_que command for adding questions to db from xml
nishanth
parents:
diff changeset
    27
        description_node = question.getElementsByTagName("description")[0]
456b7b9e3d13 created a question bank xml file and created a seed_que command for adding questions to db from xml
nishanth
parents:
diff changeset
    28
        description = (description_node.childNodes[0].data).strip()
456b7b9e3d13 created a question bank xml file and created a seed_que command for adding questions to db from xml
nishanth
parents:
diff changeset
    29
456b7b9e3d13 created a question bank xml file and created a seed_que command for adding questions to db from xml
nishanth
parents:
diff changeset
    30
        time_limit_node = question.getElementsByTagName("time_limit")[0]
456b7b9e3d13 created a question bank xml file and created a seed_que command for adding questions to db from xml
nishanth
parents:
diff changeset
    31
        time_limit = time_limit_node.childNodes[0].data
456b7b9e3d13 created a question bank xml file and created a seed_que command for adding questions to db from xml
nishanth
parents:
diff changeset
    32
456b7b9e3d13 created a question bank xml file and created a seed_que command for adding questions to db from xml
nishanth
parents:
diff changeset
    33
        options_nodes = question.getElementsByTagName("options")
456b7b9e3d13 created a question bank xml file and created a seed_que command for adding questions to db from xml
nishanth
parents:
diff changeset
    34
        options = (options_nodes[0].childNodes[0].data).strip() if options_nodes else ""
456b7b9e3d13 created a question bank xml file and created a seed_que command for adding questions to db from xml
nishanth
parents:
diff changeset
    35
456b7b9e3d13 created a question bank xml file and created a seed_que command for adding questions to db from xml
nishanth
parents:
diff changeset
    36
        code_nodes = question.getElementsByTagName("code")
456b7b9e3d13 created a question bank xml file and created a seed_que command for adding questions to db from xml
nishanth
parents:
diff changeset
    37
        code = (code_nodes[0].childNodes[0].data).strip() if code_nodes else ""
456b7b9e3d13 created a question bank xml file and created a seed_que command for adding questions to db from xml
nishanth
parents:
diff changeset
    38
456b7b9e3d13 created a question bank xml file and created a seed_que command for adding questions to db from xml
nishanth
parents:
diff changeset
    39
        expected_ans_node = question.getElementsByTagName("expected_answer")[0]
456b7b9e3d13 created a question bank xml file and created a seed_que command for adding questions to db from xml
nishanth
parents:
diff changeset
    40
        expected_ans = (expected_ans_node.childNodes[0].data).strip()
456b7b9e3d13 created a question bank xml file and created a seed_que command for adding questions to db from xml
nishanth
parents:
diff changeset
    41
60
8ca2734bdc99 modified the seed_que to suit the new design
nishanth
parents: 50
diff changeset
    42
        new_question = QuestionBank(topic = topic,
28
456b7b9e3d13 created a question bank xml file and created a seed_que command for adding questions to db from xml
nishanth
parents:
diff changeset
    43
                                    description = description, 
456b7b9e3d13 created a question bank xml file and created a seed_que command for adding questions to db from xml
nishanth
parents:
diff changeset
    44
                                    time_limit = time_limit,
456b7b9e3d13 created a question bank xml file and created a seed_que command for adding questions to db from xml
nishanth
parents:
diff changeset
    45
                                    options = options,
456b7b9e3d13 created a question bank xml file and created a seed_que command for adding questions to db from xml
nishanth
parents:
diff changeset
    46
                                    code = code,
456b7b9e3d13 created a question bank xml file and created a seed_que command for adding questions to db from xml
nishanth
parents:
diff changeset
    47
                                    expected_ans = expected_ans,
456b7b9e3d13 created a question bank xml file and created a seed_que command for adding questions to db from xml
nishanth
parents:
diff changeset
    48
                                   )
456b7b9e3d13 created a question bank xml file and created a seed_que command for adding questions to db from xml
nishanth
parents:
diff changeset
    49
        new_question.save()
456b7b9e3d13 created a question bank xml file and created a seed_que command for adding questions to db from xml
nishanth
parents:
diff changeset
    50
    
456b7b9e3d13 created a question bank xml file and created a seed_que command for adding questions to db from xml
nishanth
parents:
diff changeset
    51
class Command(NoArgsCommand):
456b7b9e3d13 created a question bank xml file and created a seed_que command for adding questions to db from xml
nishanth
parents:
diff changeset
    52
    
456b7b9e3d13 created a question bank xml file and created a seed_que command for adding questions to db from xml
nishanth
parents:
diff changeset
    53
    def handle_noargs(self, **options):
456b7b9e3d13 created a question bank xml file and created a seed_que command for adding questions to db from xml
nishanth
parents:
diff changeset
    54
        """ Just copied the code from seed_db.py """
456b7b9e3d13 created a question bank xml file and created a seed_que command for adding questions to db from xml
nishanth
parents:
diff changeset
    55
        
456b7b9e3d13 created a question bank xml file and created a seed_que command for adding questions to db from xml
nishanth
parents:
diff changeset
    56
        seed_que()