quiz/management/commands/seed_que.py
author nishanth
Tue, 06 Jul 2010 23:31:33 +0530
changeset 89 f8e25dea5222
parent 61 3b8b0ce51566
permissions -rw-r--r--
updated timing for each question. increased timing actually
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
60
8ca2734bdc99 modified the seed_que to suit the new design
nishanth
parents: 50
diff changeset
     7
topic_name2num = dict( [ (_[1], _[0]) for _ in TOPIC_CHOICES ] )
8ca2734bdc99 modified the seed_que to suit the new design
nishanth
parents: 50
diff changeset
     8
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
     9
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
    10
    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
    11
        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
    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
    13
    q_bank = parse("question_bank.xml").getElementsByTagName("question")
61
3b8b0ce51566 removed junk code
nishanth
parents: 60
diff changeset
    14
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
    15
    for question in q_bank:
60
8ca2734bdc99 modified the seed_que to suit the new design
nishanth
parents: 50
diff changeset
    16
8ca2734bdc99 modified the seed_que to suit the new design
nishanth
parents: 50
diff changeset
    17
        topic_name_node = question.getElementsByTagName("topic")[0]
8ca2734bdc99 modified the seed_que to suit the new design
nishanth
parents: 50
diff changeset
    18
        topic_name = (topic_name_node.childNodes[0].data).strip()
8ca2734bdc99 modified the seed_que to suit the new design
nishanth
parents: 50
diff changeset
    19
        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
    20
456b7b9e3d13 created a question bank xml file and created a seed_que command for adding questions to db from xml
nishanth
parents:
diff changeset
    21
        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
    22
        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
    23
456b7b9e3d13 created a question bank xml file and created a seed_que command for adding questions to db from xml
nishanth
parents:
diff changeset
    24
        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
    25
        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
    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
        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
    28
        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
    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
        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
    31
        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
    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
        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
    34
        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
    35
60
8ca2734bdc99 modified the seed_que to suit the new design
nishanth
parents: 50
diff changeset
    36
        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
    37
                                    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
    38
                                    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
    39
                                    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
    40
                                    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
    41
                                    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
    42
                                   )
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
        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
    44
    
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
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
    46
    
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
    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
    48
        """ 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
    49
        
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
        seed_que()