quiz/management/commands/seed_que.py
author nishanth
Wed, 21 Apr 2010 12:11:19 +0530
changeset 29 ea1c0110e989
parent 28 456b7b9e3d13
child 46 b45bb982ae24
permissions -rw-r--r--
fixed the display of question
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
456b7b9e3d13 created a question bank xml file and created a seed_que command for adding questions to db from xml
nishanth
parents:
diff changeset
     5
from offline.quiz.models import QuestionBank
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
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
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
    13
    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
    14
        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
    15
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
    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
    17
    for question in q_bank:
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
        quiz_name = question.parentNode.tagName
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
        quiz_num = name2num[quiz_name]
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
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_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
    29
        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
    30
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_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
    32
        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
    33
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_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
    35
        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
    36
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
        new_question = QuestionBank(quiz_num = quiz_num, 
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
                                    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
    39
                                    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
    40
                                    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
    41
                                    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
    42
                                    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
    43
                                   )
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
        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
    45
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
    
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
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
    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
    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
    51
        """ 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
    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
        seed_que()