#!/usr/bin/env python
# -*- coding:utf8 -*-
# Copyright (C) 2014 FinalsClub Foundation
+from argparse import ArgumentError
from django.core.management import BaseCommand
-from karmaworld.apps.quizzes.xml_import import quiz_from_xml
+from karmaworld.apps.quizzes.xml_import import quiz_from_xml, keywords_from_xml
class Command(BaseCommand):
+ args = '<IQ-Metrics XML file> <note ID>'
help = """
Import a quiz from an IQ Metrics XML file
"""
def handle(self, *args, **kwargs):
- quiz_from_xml(args[0])
-
+ if len(args) != 2:
+ raise ArgumentError("Incorrect arguments, see usage")
+ quiz_from_xml(args[0], args[1])
+ keywords_from_xml(args[0], args[1])
from StringIO import StringIO
+from karmaworld.apps.notes.models import Note
import re
from bs4 import BeautifulSoup
-from karmaworld.apps.quizzes.models import MultipleChoiceQuestion, Quiz, TrueFalseQuestion, MultipleChoiceOption
+from karmaworld.apps.quizzes.models import MultipleChoiceQuestion, Quiz, TrueFalseQuestion, MultipleChoiceOption, \
+ Keyword
from pyth.plugins.plaintext.writer import PlaintextWriter
from pyth.plugins.rtf15.reader import Rtf15Reader
question=question_object)
-def quiz_from_xml(filename):
+def quiz_from_xml(filename, note_id):
with open(filename, 'r') as file:
soup = BeautifulSoup(file.read(), "xml")
+ note_object = Note.objects.get(id=note_id)
quiz_name = soup.find('EChapterTitle').string
- quiz_object = Quiz.objects.create(name=quiz_name)
+ quiz_object = Quiz.objects.create(name=quiz_name, note=note_object)
questions = soup.find_all('TestBank')
for question in questions:
elif type_string == 'True/False':
_true_false(question, quiz_object)
+
+
+def keywords_from_xml(filename, note_id):
+ with open(filename, 'r') as file:
+ soup = BeautifulSoup(file.read(), "xml")
+
+ note_object = Note.objects.get(id=note_id)
+
+ keywords = soup.find_all('WordPhrase')
+ for word in keywords:
+ Keyword.objects.create(word=word.string, note=note_object)