quiz/management/commands/seed_que.py
author nishanth
Thu, 22 Apr 2010 06:23:53 +0530
changeset 49 3643f59f141e
parent 46 b45bb982ae24
child 50 0842b3439c3e
permissions -rw-r--r--
added questions from day2 quiz1
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
46
b45bb982ae24 added few more questions
nishanth
parents: 28
diff changeset
     5
from offline.quiz.models import QuestionBank, Quiz
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
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()
49
3643f59f141e added questions from day2 quiz1
nishanth
parents: 46
diff changeset
    23
        print description
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
    24
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_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
    26
        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
    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
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_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
    30
        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
    31
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_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
    33
        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
    34
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_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
    36
        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
    37
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
        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
    39
                                    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
    40
                                    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
    41
                                    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
    42
                                    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
    43
                                    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
    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
        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
    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
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
    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
    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
    50
        """ 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
    51
        
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
        seed_que()