quiz/management/commands/seed_que.py
changeset 28 456b7b9e3d13
child 46 b45bb982ae24
equal deleted inserted replaced
27:6233cf13fc12 28:456b7b9e3d13
       
     1 from xml.dom.minidom import parse, Node
       
     2 
       
     3 from django.core.management.base import NoArgsCommand
       
     4 
       
     5 from offline.quiz.models import QuestionBank
       
     6 
       
     7 name2num = {"day1quiz1" : "11",
       
     8             "day1quiz2" : "12",
       
     9             "day2quiz1" : "21",
       
    10            }
       
    11 
       
    12 def seed_que():
       
    13     for question in QuestionBank.objects.all():
       
    14         question.delete()
       
    15 
       
    16     q_bank = parse("question_bank.xml").getElementsByTagName("question")
       
    17     for question in q_bank:
       
    18         quiz_name = question.parentNode.tagName
       
    19         quiz_num = name2num[quiz_name]
       
    20 
       
    21         description_node = question.getElementsByTagName("description")[0]
       
    22         description = (description_node.childNodes[0].data).strip()
       
    23 
       
    24         time_limit_node = question.getElementsByTagName("time_limit")[0]
       
    25         time_limit = time_limit_node.childNodes[0].data
       
    26 
       
    27 
       
    28         options_nodes = question.getElementsByTagName("options")
       
    29         options = (options_nodes[0].childNodes[0].data).strip() if options_nodes else ""
       
    30 
       
    31         code_nodes = question.getElementsByTagName("code")
       
    32         code = (code_nodes[0].childNodes[0].data).strip() if code_nodes else ""
       
    33 
       
    34         expected_ans_node = question.getElementsByTagName("expected_answer")[0]
       
    35         expected_ans = (expected_ans_node.childNodes[0].data).strip()
       
    36 
       
    37         new_question = QuestionBank(quiz_num = quiz_num, 
       
    38                                     description = description, 
       
    39                                     time_limit = time_limit,
       
    40                                     options = options,
       
    41                                     code = code,
       
    42                                     expected_ans = expected_ans,
       
    43                                    )
       
    44         new_question.save()
       
    45 
       
    46 
       
    47     
       
    48 class Command(NoArgsCommand):
       
    49     
       
    50     def handle_noargs(self, **options):
       
    51         """ Just copied the code from seed_db.py """
       
    52         
       
    53         seed_que()